1 package org.bouncycastle.asn1;
3 import java.math.BigInteger;
7 public class BERInputStream
10 private DERObject END_OF_STREAM = new DERObject() {
15 throw new IOException("Eeek!");
19 public BERInputStream(
26 * read a string of bytes representing an indefinite length object.
28 private byte[] readIndefiniteLengthFully()
31 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
36 while ((b = read()) >= 0)
38 if (b1 == 0 && b == 0)
47 return bOut.toByteArray();
50 private BERConstructedOctetString buildConstructedOctetString(
55 Vector octs = new Vector();
65 DERObject o = readObject();
67 if (o == END_OF_STREAM)
75 return new BERConstructedOctetString(octs);
78 public DERObject readObject()
84 throw new EOFException();
87 int length = readLength();
89 if (length < 0) // indefinite length method
97 case SEQUENCE | CONSTRUCTED:
98 BERConstructedSequence seq = new BERConstructedSequence();
102 DERObject obj = readObject();
104 if (obj == END_OF_STREAM)
112 case OCTET_STRING | CONSTRUCTED:
113 return buildConstructedOctetString(null, null);
115 if ((tag & (TAGGED | CONSTRUCTED)) != 0)
117 // with tagged object tag number is bottom 4 bits
118 BERTaggedObject tagObj = new BERTaggedObject(tag & 0x0f, readObject());
119 DERObject o = readObject();
121 if (o == END_OF_STREAM)
125 else if (o instanceof DEROctetString
126 && tagObj.getObject() instanceof DEROctetString)
129 // it's an implicit object - mark it as so...
131 tagObj = new BERTaggedObject(false, tag & 0x0f,
132 buildConstructedOctetString((DEROctetString)tagObj.getObject(), (DEROctetString)o));
137 throw new IOException("truncated tagged object");
140 bytes = readIndefiniteLengthFully();
142 return buildObject(tag, bytes);
147 if (tag == 0 && length == 0) // end of contents marker.
149 return END_OF_STREAM;
152 byte[] bytes = new byte[length];
156 return buildObject(tag, bytes);