1 package org.bouncycastle.asn1;
2 
3 import java.io.IOException;
4 import java.io.InputStream;
5 
6 import org.bouncycastle.util.io.Streams;
7 
8 /**
9  * A parser for indefinite-length OCTET STRINGs.
10  */
11 public class BEROctetStringParser
12     implements ASN1OctetStringParser
13 {
14     private ASN1StreamParser _parser;
15 
BEROctetStringParser( ASN1StreamParser parser)16     BEROctetStringParser(
17         ASN1StreamParser parser)
18     {
19         _parser = parser;
20     }
21 
22     /**
23      * Return an InputStream representing the contents of the OCTET STRING.
24      *
25      * @return an InputStream with its source as the OCTET STRING content.
26      */
getOctetStream()27     public InputStream getOctetStream()
28     {
29         return new ConstructedOctetStream(_parser);
30     }
31 
32     /**
33      * Return an in-memory, encodable, representation of the OCTET STRING.
34      *
35      * @return a BEROctetString.
36      * @throws IOException if there is an issue loading the data.
37      */
getLoadedObject()38     public ASN1Primitive getLoadedObject()
39         throws IOException
40     {
41         return new BEROctetString(Streams.readAll(getOctetStream()));
42     }
43 
44     /**
45      * Return an BEROctetString representing this parser and its contents.
46      *
47      * @return an BEROctetString
48      */
toASN1Primitive()49     public ASN1Primitive toASN1Primitive()
50     {
51         try
52         {
53             return getLoadedObject();
54         }
55         catch (IOException e)
56         {
57             throw new ASN1ParsingException("IOException converting stream to byte array: " + e.getMessage(), e);
58         }
59     }
60 }
61