CheckedInputStreampublic class CheckedInputStream extends FilterInputStream The {@code CheckedInputStream} class is used to maintain a checksum at the
same time as the data, on which the checksum is computed, is read from a
stream. The purpose of this checksum is to establish data integrity,
comparing the computed checksum against a published checksum value. |
Fields Summary |
---|
private final Checksum | check |
Constructors Summary |
---|
public CheckedInputStream(InputStream is, Checksum csum)Constructs a new {@code CheckedInputStream} on {@code InputStream}
{@code is}. The checksum will be calculated using the algorithm
implemented by {@code csum}.
super(is);
check = csum;
|
Methods Summary |
---|
public java.util.zip.Checksum | getChecksum()Returns the checksum calculated on the stream read so far.
return check;
| public int | read()Reads one byte of data from the underlying input stream and updates the
checksum with the byte data.
int x = in.read();
if (x != -1) {
check.update(x);
}
return x;
| public int | read(byte[] buf, int off, int nbytes)Reads up to n bytes of data from the underlying input stream, storing it
into {@code buf}, starting at offset {@code off}. The checksum is
updated with the bytes read.
int x = in.read(buf, off, nbytes);
if (x != -1) {
check.update(buf, off, x);
}
return x;
| public long | skip(long nbytes)Skip up to n bytes of data on the underlying input stream. Any skipped
bytes are added to the running checksum value.
if (nbytes < 1) {
return 0;
}
long skipped = 0;
byte[] b = new byte[2048];
int x, v;
while (skipped != nbytes) {
x = in.read(b, 0,
(v = (int) (nbytes - skipped)) > b.length ? b.length : v);
if (x == -1) {
return skipped;
}
check.update(b, 0, x);
skipped += x;
}
return skipped;
|
|