jut.io
Class HashInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by jut.io.InputCountStream
          extended by jut.io.HashInputStream
All Implemented Interfaces:
Closeable

public class HashInputStream
extends InputCountStream

InputStream calculating a hash for the bytes read. the methods InputCountStream.skip(long), InputCountStream.mark(int) and InputCountStream.reset() are not supported and throw an UnsupportedOperationException.


Field Summary
 
Fields inherited from class jut.io.InputCountStream
baseStream, counter
 
Constructor Summary
HashInputStream(File file, String algorithm)
          create a new InputStream calculating a hash for the bytes read. for creating the stream, the Streams.input(File) method is used.
HashInputStream(File file, String algorithm, int capacity)
          create a new InputStream with push back functionality calculating a hash for the bytes read. for creating the stream, the Streams.input(File) method is used.
HashInputStream(InputStream stream, String algorithm)
          create a new InputStream calculating a hash for the bytes read.
HashInputStream(InputStream stream, String algorithm, int capacity)
          create a new InputStream with push back functionality calculating a hash for the bytes read. if the hash algorithm is not provided, an appropriate NestedException is thrown.
 
Method Summary
 void close()
           the HashInputStream must be closed before the hash result can be read.
 String hash()
          returns the hash string for this HashInputStream. note that the HashInputStream must be closed before the hash result can be read.
 byte[] hashDigest()
          returns the hash digest for this HashInputStream. note that the HashInputStream must be closed before the hash result can be read.
 Long lastModified()
          returns the modification time stamp of the underlying file if known, else null.
 void unread(byte[] bytes)
          pushes back an array of bytes by copying it to the front of the pushback buffer. note that the hash is created from the data read originally, i.e., if data different from the data read is pushed back and read again, the hash does not change.
 void unread(byte[] bytes, int off, int len)
          pushes back a portion of an array of bytes by copying it to the front of the pushback buffer.
 void unread(int oneByte)
          pushes back a byte by copying it to the front of the pushback buffer. note that the hash is created from the data read originally, i.e., if data different from the data read is pushed back and read again, the hash does not change.
 
Methods inherited from class jut.io.InputCountStream
available, getCount, getCounter, mark, markSupported, read, read, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HashInputStream

public HashInputStream(File file,
                       String algorithm)
                throws FileNotFoundException
create a new InputStream calculating a hash for the bytes read. for creating the stream, the Streams.input(File) method is used.

Parameters:
file - - the file to open the InputStream for.
algorithm - - the hash algorithm to use as defined in MessageDigest.getAlgorithm()
Throws:
FileNotFoundException - if the file cannot be opened for reading.

HashInputStream

public HashInputStream(InputStream stream,
                       String algorithm)
create a new InputStream calculating a hash for the bytes read.

Parameters:
stream - - the stream to calculate a hash for.
algorithm - - the hash algorithm to use as defined in MessageDigest.getAlgorithm()

HashInputStream

public HashInputStream(File file,
                       String algorithm,
                       int capacity)
                throws FileNotFoundException
create a new InputStream with push back functionality calculating a hash for the bytes read. for creating the stream, the Streams.input(File) method is used.

Parameters:
file - - the file to open the InputStream for.
algorithm - - the hash algorithm to use as defined in MessageDigest.getAlgorithm()
capacity - - the capacity of the push back buffer as defined in PushbackInputStream.
Throws:
FileNotFoundException - if the file cannot be opened for reading.

HashInputStream

public HashInputStream(InputStream stream,
                       String algorithm,
                       int capacity)
create a new InputStream with push back functionality calculating a hash for the bytes read. if the hash algorithm is not provided, an appropriate NestedException is thrown.

Parameters:
stream - - the stream to calculate a hash for.
algorithm - - the hash algorithm to use as defined in MessageDigest.getAlgorithm()
capacity - - the capacity of the push back buffer as defined in PushbackInputStream.
Method Detail

unread

public void unread(byte[] bytes)
            throws IOException
pushes back an array of bytes by copying it to the front of the pushback buffer. note that the hash is created from the data read originally, i.e., if data different from the data read is pushed back and read again, the hash does not change.

Parameters:
bytes - - the byte array to push back.
Throws:
IOException - if there is not enough room in the pushback buffer for the specified number of bytes, or this input stream has been closed by invoking its close() method.

unread

public void unread(byte[] bytes,
                   int off,
                   int len)
            throws IOException
pushes back a portion of an array of bytes by copying it to the front of the pushback buffer. note that the hash is created from the data read originally, i.e., if data different from the data read is pushed back and read again, the hash does not change.

Parameters:
bytes - - the byte array to push back.
off - - the start offset of the data.
len - - the number of bytes to push back.
Throws:
IOException - if there is not enough room in the pushback buffer for the specified number of bytes, or this input stream has been closed by invoking its close() method.

unread

public void unread(int oneByte)
            throws IOException
pushes back a byte by copying it to the front of the pushback buffer. note that the hash is created from the data read originally, i.e., if data different from the data read is pushed back and read again, the hash does not change.

Parameters:
oneByte - - the int value whose low-order byte is to be pushed back.
Throws:
IOException - if there is not enough room in the pushback buffer for the byte, or this input stream has been closed by invoking its close() method.

close

public void close()
           throws IOException
the HashInputStream must be closed before the hash result can be read.

Specified by:
close in interface Closeable
Overrides:
close in class InputCountStream
Throws:
IOException

hashDigest

public byte[] hashDigest()
                  throws IllegalStateException
returns the hash digest for this HashInputStream. note that the HashInputStream must be closed before the hash result can be read.

Returns:
the hash digest for this HashInputStream.
Throws:
IllegalStateException - if trying to access the hash result while the stream is still open.

hash

public String hash()
            throws IllegalStateException
returns the hash string for this HashInputStream. note that the HashInputStream must be closed before the hash result can be read.

Returns:
the hash string for this HashInputStream.
Throws:
IllegalStateException - if trying to access the hash result while the stream is still open.

lastModified

public Long lastModified()
returns the modification time stamp of the underlying file if known, else null.

Returns:
the modification time stamp of the underlying file if known, else null.