1package org.bouncycastle.asn1;
2
3import java.io.IOException;
4import java.io.InputStream;
5
6import org.bouncycastle.util.io.Streams;
7
8/**
9 * A parser for indefinite-length OCTET STRINGs.
10 */
11public class BEROctetStringParser
12    implements ASN1OctetStringParser
13{
14    private ASN1StreamParser _parser;
15
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     */
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     */
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     */
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