| | |
| | | |
| | | package org.forgerock.opendj.asn1; |
| | | |
| | | import static org.forgerock.opendj.asn1.ASN1Constants.ELEMENT_READ_STATE_NEED_FIRST_LENGTH_BYTE; |
| | | import static org.forgerock.opendj.asn1.ASN1Constants.ELEMENT_READ_STATE_NEED_TYPE; |
| | | import static org.forgerock.opendj.asn1.ASN1Constants.ELEMENT_READ_STATE_NEED_VALUE_BYTES; |
| | | import static org.forgerock.opendj.ldap.CoreMessages.*; |
| | | |
| | | import java.io.IOException; |
| | |
| | | */ |
| | | final class ASN1ByteSequenceReader extends AbstractASN1Reader implements ASN1Reader { |
| | | |
| | | private int state = ELEMENT_READ_STATE_NEED_TYPE; |
| | | private int state = ASN1.ELEMENT_READ_STATE_NEED_TYPE; |
| | | |
| | | private byte peekType = 0; |
| | | |
| | |
| | | * {@inheritDoc} |
| | | */ |
| | | public boolean elementAvailable() throws IOException { |
| | | if ((state == ELEMENT_READ_STATE_NEED_TYPE) && !needTypeState(false)) { |
| | | if ((state == ASN1.ELEMENT_READ_STATE_NEED_TYPE) && !needTypeState(false)) { |
| | | return false; |
| | | } |
| | | if ((state == ELEMENT_READ_STATE_NEED_FIRST_LENGTH_BYTE) |
| | | if ((state == ASN1.ELEMENT_READ_STATE_NEED_FIRST_LENGTH_BYTE) |
| | | && !needFirstLengthByteState(false)) { |
| | | return false; |
| | | } |
| | |
| | | * {@inheritDoc} |
| | | */ |
| | | public boolean hasNextElement() throws IOException { |
| | | return (state != ELEMENT_READ_STATE_NEED_TYPE) || needTypeState(false); |
| | | return (state != ASN1.ELEMENT_READ_STATE_NEED_TYPE) || needTypeState(false); |
| | | } |
| | | |
| | | /** |
| | |
| | | public int peekLength() throws IOException { |
| | | peekType(); |
| | | |
| | | if (state == ELEMENT_READ_STATE_NEED_FIRST_LENGTH_BYTE) { |
| | | if (state == ASN1.ELEMENT_READ_STATE_NEED_FIRST_LENGTH_BYTE) { |
| | | needFirstLengthByteState(true); |
| | | } |
| | | |
| | |
| | | * {@inheritDoc} |
| | | */ |
| | | public byte peekType() throws IOException { |
| | | if (state == ELEMENT_READ_STATE_NEED_TYPE) { |
| | | if (state == ASN1.ELEMENT_READ_STATE_NEED_TYPE) { |
| | | // Read just the type. |
| | | if (reader.remaining() <= 0) { |
| | | final LocalizableMessage message = ERR_ASN1_TRUCATED_TYPE_BYTE.get(); |
| | |
| | | final int type = reader.get(); |
| | | |
| | | peekType = (byte) type; |
| | | state = ELEMENT_READ_STATE_NEED_FIRST_LENGTH_BYTE; |
| | | state = ASN1.ELEMENT_READ_STATE_NEED_FIRST_LENGTH_BYTE; |
| | | } |
| | | |
| | | return peekType; |
| | |
| | | } |
| | | final int readByte = reader.get(); |
| | | |
| | | state = ELEMENT_READ_STATE_NEED_TYPE; |
| | | state = ASN1.ELEMENT_READ_STATE_NEED_TYPE; |
| | | return readByte != 0x00; |
| | | } |
| | | |
| | |
| | | reader = readerStack.removeFirst(); |
| | | |
| | | // Reset the state |
| | | state = ELEMENT_READ_STATE_NEED_TYPE; |
| | | state = ASN1.ELEMENT_READ_STATE_NEED_TYPE; |
| | | } |
| | | |
| | | /** |
| | |
| | | longValue = (longValue << 8) | (readByte & 0xFF); |
| | | } |
| | | |
| | | state = ELEMENT_READ_STATE_NEED_TYPE; |
| | | state = ASN1.ELEMENT_READ_STATE_NEED_TYPE; |
| | | return longValue; |
| | | } else { |
| | | int intValue = 0; |
| | |
| | | intValue = (intValue << 8) | (readByte & 0xFF); |
| | | } |
| | | |
| | | state = ELEMENT_READ_STATE_NEED_TYPE; |
| | | state = ASN1.ELEMENT_READ_STATE_NEED_TYPE; |
| | | return intValue; |
| | | } |
| | | } |
| | |
| | | throw DecodeException.fatalError(message); |
| | | } |
| | | |
| | | state = ELEMENT_READ_STATE_NEED_TYPE; |
| | | state = ASN1.ELEMENT_READ_STATE_NEED_TYPE; |
| | | } |
| | | |
| | | /** |
| | |
| | | throw DecodeException.fatalError(message); |
| | | } |
| | | |
| | | state = ELEMENT_READ_STATE_NEED_TYPE; |
| | | state = ASN1.ELEMENT_READ_STATE_NEED_TYPE; |
| | | return reader.getByteString(peekLength); |
| | | } |
| | | |
| | |
| | | } |
| | | builder.append(reader, peekLength); |
| | | |
| | | state = ELEMENT_READ_STATE_NEED_TYPE; |
| | | state = ASN1.ELEMENT_READ_STATE_NEED_TYPE; |
| | | return builder; |
| | | } |
| | | |
| | |
| | | throw DecodeException.fatalError(message); |
| | | } |
| | | |
| | | state = ELEMENT_READ_STATE_NEED_TYPE; |
| | | state = ASN1.ELEMENT_READ_STATE_NEED_TYPE; |
| | | return reader.getString(peekLength); |
| | | } |
| | | |
| | |
| | | reader = subByteString; |
| | | |
| | | // Reset the state |
| | | state = ELEMENT_READ_STATE_NEED_TYPE; |
| | | state = ASN1.ELEMENT_READ_STATE_NEED_TYPE; |
| | | } |
| | | |
| | | /** |
| | |
| | | throw DecodeException.fatalError(message); |
| | | } |
| | | |
| | | state = ELEMENT_READ_STATE_NEED_TYPE; |
| | | state = ASN1.ELEMENT_READ_STATE_NEED_TYPE; |
| | | reader.skip(peekLength); |
| | | return this; |
| | | } |
| | |
| | | .get(peekLength, maxElementSize); |
| | | throw DecodeException.fatalError(message); |
| | | } |
| | | state = ELEMENT_READ_STATE_NEED_VALUE_BYTES; |
| | | state = ASN1.ELEMENT_READ_STATE_NEED_VALUE_BYTES; |
| | | return true; |
| | | } |
| | | |
| | |
| | | final int type = reader.get(); |
| | | |
| | | peekType = (byte) type; |
| | | state = ELEMENT_READ_STATE_NEED_FIRST_LENGTH_BYTE; |
| | | state = ASN1.ELEMENT_READ_STATE_NEED_FIRST_LENGTH_BYTE; |
| | | return true; |
| | | } |
| | | } |