public class DerValueInputStream extends FilterInputStream
DerValueInputStream
can be considered as a limited, narrowed, input stream. If this
DerValueInputStream
can only be read for the length of the DER value the InputStream
passed as the
parameter of the constructor may contain more data.
In addition, a method is called after each read: read()
will automatically call
onDataRead(byte)
, read(byte[], int, int)
will automatically
call onDataRead(byte[], int, int)
, and skip(long)
performs
read()
operations. Finally onStreamEnd()
is call right before
returning the last byte of the DER value.
A classic usage of this class is a signed DER Value, as shown in the following code example. A hash is updated in
onDataRead(byte[], int, int)
and onDataRead(byte)
and the
signature is verified in onStreamEnd()
.
final Signature signature; [...] DerFixedLengthInputStream filteredInputStream = new DerFixedLengthInputStream(signedCommand) { @Override protected void onStreamEnd() throws IOException { validateSignature(signature); } @Override protected void onDataRead(byte[] data, int off, int len) { try { signature.update(data, off, len); } catch (SignatureException e) { throw new InvalidSignatureException(e); } } @Override protected void onDataRead(byte b) { try { signature.update(b); } catch (SignatureException e) { throw new InvalidSignatureException(e); } } };
Modifier and Type | Field and Description |
---|---|
protected int |
remainingBytes
Number of remaining byte of the stream before reaching its end.
|
protected int |
tag
DER Tag of the stream, determined by the first byte of the stream.
|
in
Constructor and Description |
---|
DerValueInputStream(InputStream in)
Constructs a fixed length input stream of a DER encoded stream.
|
Modifier and Type | Method and Description |
---|---|
int |
available()
Returns an estimate of the number of bytes that can be read (or skipped over) from this input
stream without blocking by the next caller of a method for this input stream.
|
protected void |
checkStreamEnd()
Checks if the end of the stream is reached.
|
void |
close()
Closes this input stream and releases any system resources associated with the stream.
|
int |
getTag()
Returns the tag of the stream, e.g.
|
protected void |
onDataRead(byte b)
Called after a data read to apply a process on it, such as an update of hash.
|
protected void |
onDataRead(byte[] data,
int off,
int len)
Called after a data read to apply a process on it, such as an update of hash.
|
protected void |
onStreamEnd()
Called once the end of the stream is reached.
|
int |
read()
Reads the next byte of data from this input stream.
|
int |
read(byte[] b,
int off,
int len)
|
int |
remainingLength()
Returns the number of bytes that remains in the stream.
|
long |
skip(long n)
Consumes, and drops, a given number of bytes.
|
mark, markSupported, read, reset
protected int remainingBytes
protected final int tag
public DerValueInputStream(InputStream in) throws IOException, DerFormatException
in
- the input stream to limit.IOException
- if an I/O error occurs.DerFormatException
- if an error occurs while parsing.public int available() throws IOException
FilterInputStream
This method returns the result of in
.available().
available
in class FilterInputStream
IOException
- if an I/O error occurs.protected void checkStreamEnd() throws IOException, RuntimeException
IOException
- if an I/O error occurs.RuntimeException
- Any RuntimeException
could be thrown by this process.public void close()
FilterInputStream
in.close()
.close
in interface Closeable
close
in interface AutoCloseable
close
in class FilterInputStream
FilterInputStream.in
public int getTag()
protected void onDataRead(byte b) throws RuntimeException
b
- the byte to use for the update.RuntimeException
- As this method can be user-defined, it may throw only IOException
, Error
or
RuntimeException
.protected void onDataRead(byte[] data, int off, int len) throws RuntimeException
data
- the array of bytes.off
- the offset to start from in the array of bytes.len
- the number of bytes to use, starting at offset.RuntimeException
- As this method can be user-defined, it may throw only IOException
, Error
or
RuntimeException
.protected void onStreamEnd() throws IOException, RuntimeException
IOException
- This this method may read further in the input stream passed as parameter of the constructor, it may
throw IOException
on I/O errors.RuntimeException
- As this method can be user-defined, it may throw only IOException
, Error
or
RuntimeException
.public int read() throws IOException
FilterInputStream
int
in the range 0
to 255
. If no byte is available because
the end of the stream has been reached, the value -1
is returned. This method blocks
until input data is available, the end of the stream is detected, or an exception is thrown.
This method simply performs in.read()
and returns the result.
read
in class FilterInputStream
-1
if the end of the stream is reached.IOException
- if an I/O error occurs.FilterInputStream.in
public int read(byte[] b, int off, int len) throws IOException
read
in class FilterInputStream
b
- the buffer into which the data is read.off
- the start offset in the destination array b
len
- the maximum number of bytes read.-1
if there is no more
data because the end of the stream has been reached.IOException
- if an I/O error occurs.FilterInputStream.in
public int remainingLength()
public long skip(long n) throws IOException
skip
in class FilterInputStream
n
- the number of bytes to skip.IOException
- if an I/O error occurs.