public class DerValueInputStream
extends java.io.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.
|
Constructor and Description |
---|
DerValueInputStream(java.io.InputStream in)
Constructs a fixed length input stream of a DER encoded stream.
|
Modifier and Type | Method and Description |
---|---|
int |
available() |
protected void |
checkStreamEnd()
Checks if the end of the stream is reached.
|
void |
close() |
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() |
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.
|
protected int remainingBytes
protected final int tag
public DerValueInputStream(java.io.InputStream in) throws java.io.IOException, DerFormatException
in
- the input stream to limit.java.io.IOException
- if an I/O error occurs.DerFormatException
- if an error occurs while parsing.public void close()
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.FilterInputStream
protected void onStreamEnd() throws java.io.IOException, java.lang.RuntimeException
java.io.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.java.lang.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 java.lang.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.java.lang.RuntimeException
- As this method can be user-defined, it may throw only IOException
, Error
or
RuntimeException
.protected void onDataRead(byte b) throws java.lang.RuntimeException
b
- the byte to use for the update.java.lang.RuntimeException
- As this method can be user-defined, it may throw only IOException
, Error
or
RuntimeException
.public int available() throws java.io.IOException
available
in class java.io.FilterInputStream
java.io.IOException
public int read(byte[] b, int off, int len) throws java.io.IOException
read
in class java.io.FilterInputStream
java.io.IOException
public int read() throws java.io.IOException
read
in class java.io.FilterInputStream
java.io.IOException
protected void checkStreamEnd() throws java.io.IOException, java.lang.RuntimeException
java.io.IOException
- if an I/O error occurs.java.lang.RuntimeException
- Any RuntimeException
could be thrown by this process.public long skip(long n) throws java.io.IOException
skip
in class java.io.FilterInputStream
n
- the number of bytes to skip.java.io.IOException
- if an I/O error occurs.public int remainingLength()
public int getTag()