int tag = _in.read();
if (tag == -1)
{
if (_eofFound)
{
throw new EOFException("attempt to read past end of file.");
}
_eofFound = true;
return null;
}
//
// turn of looking for "00" while we resolve the tag
//
if (_in instanceof IndefiniteLengthInputStream)
{
((IndefiniteLengthInputStream)_in).setEofOn00(false);
}
//
// calculate tag number
//
int baseTagNo = tag & ~BerTag.CONSTRUCTED;
int tagNo = baseTagNo;
if ((tag & BerTag.TAGGED) != 0)
{
tagNo = tag & 0x1f;
//
// with tagged object tag number is bottom 5 bits, or stored at the start of the content
//
if (tagNo == 0x1f)
{
tagNo = 0;
int b = _in.read();
while ((b >= 0) && ((b & 0x80) != 0))
{
tagNo |= (b & 0x7f);
tagNo <<= 7;
b = _in.read();
}
if (b < 0)
{
_eofFound = true;
throw new EOFException("EOF encountered inside tag value.");
}
tagNo |= (b & 0x7f);
}
}
//
// calculate length
//
int length = readLength();
if (length < 0) // indefinite length
{
IndefiniteLengthInputStream indIn = new IndefiniteLengthInputStream(_in);
switch (baseTagNo)
{
case BerTag.NULL:
return new Asn1Null(tag);
case BerTag.OCTET_STRING:
return new BerOctetString(tag, indIn);
case BerTag.SEQUENCE:
return new BerSequence(tag, indIn);
case BerTag.SET:
return new BerSet(tag, indIn);
default:
return new Asn1TaggedObject(tag, tagNo, indIn);
}
}
else
{
DefiniteLengthInputStream defIn = new DefiniteLengthInputStream(_in, length);
switch (baseTagNo)
{
case BerTag.INTEGER:
return new Asn1Integer(tag, defIn.toByteArray());
case BerTag.NULL:
return new Asn1Null(tag);
case BerTag.OBJECT_IDENTIFIER:
return new Asn1ObjectIdentifier(tag, defIn.toByteArray());
case BerTag.OCTET_STRING:
return new DerOctetString(tag, defIn.toByteArray());
case BerTag.SEQUENCE:
return new DerSequence(tag, defIn.toByteArray());
case BerTag.SET:
return new DerSet(tag, defIn.toByteArray());
default:
return new Asn1TaggedObject(tag, tagNo, defIn);
}
}