1 package org.bouncycastle.asn1;
6 public class BERInputStream
9 private DERObject END_OF_STREAM = new DERObject() {
14 throw new IOException("Eeek!");
18 public BERInputStream(
25 * read a string of bytes representing an indefinite length object.
27 private byte[] readIndefiniteLengthFully()
30 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
35 while ((b = read()) >= 0)
37 if (b1 == 0 && b == 0)
46 return bOut.toByteArray();
49 private BERConstructedOctetString buildConstructedOctetString()
52 Vector octs = new Vector();
56 DERObject o = readObject();
58 if (o == END_OF_STREAM)
66 return new BERConstructedOctetString(octs);
69 public DERObject readObject()
75 throw new EOFException();
78 int length = readLength();
80 if (length < 0) // indefinite length method
86 case SEQUENCE | CONSTRUCTED:
87 BERConstructedSequence seq = new BERConstructedSequence();
91 DERObject obj = readObject();
93 if (obj == END_OF_STREAM)
101 case OCTET_STRING | CONSTRUCTED:
102 return buildConstructedOctetString();
103 case SET | CONSTRUCTED:
104 DEREncodableVector v = new DEREncodableVector();
108 DERObject obj = readObject();
110 if (obj == END_OF_STREAM)
117 return new BERSet(v);
120 // with tagged object tag number is bottom 5 bits
122 if ((tag & TAGGED) != 0)
124 if ((tag & 0x1f) == 0x1f)
126 throw new IOException("unsupported high tag encountered");
130 // simple type - implicit... return an octet string
132 if ((tag & CONSTRUCTED) == 0)
134 byte[] bytes = readIndefiniteLengthFully();
136 return new BERTaggedObject(false, tag & 0x1f, new DEROctetString(bytes));
140 // either constructed or explicitly tagged
142 DERObject dObj = readObject();
144 if (dObj == END_OF_STREAM) // empty tag!
146 return new DERTaggedObject(tag & 0x1f);
149 DERObject next = readObject();
152 // explicitly tagged (probably!) - if it isn't we'd have to
153 // tell from the context
155 if (next == END_OF_STREAM)
157 return new BERTaggedObject(tag & 0x1f, dObj);
161 // another implicit object, we'll create a sequence...
163 seq = new BERConstructedSequence();
172 while (next != END_OF_STREAM);
174 return new BERTaggedObject(false, tag & 0x1f, seq);
177 throw new IOException("unknown BER object encountered");
182 if (tag == 0 && length == 0) // end of contents marker.
184 return END_OF_STREAM;
187 byte[] bytes = new byte[length];
191 return buildObject(tag, bytes);