Class CheckedInputStream in Java

digitization, transformation, binary

Class CheckedInputStream in Java

CheckedInputStream class is a subclass of java.io.FilterInputStream; it allows a stream to be read and a checksum computed on its contents at the same time. This is useful when we want to check the integrity of a stream of data against a published checksum value.

The CheckedInputStream class represents an InputStream with an associated checksum. In
other words, a CheckedInputStream wraps a regular input stream and computes a checksum value as data is read from the stream. The checksum can verify the integrity of the data. When we create a CheckedInputStream, we must specify an object that implements the Checksum interface that computes the checksum.

Ads code goes here

To create a CheckedInputStream, we must specify both the stream that it should read and also a Checksum object, such as CRC32, that implements the particular checksum algorithm you desire. The read() and skip() methods are the same as those of other input streams.

As bytes are read, they are incorporated into the checksum that is being computed. Note that the getChecksum() method does not return the checksum value itself, but rather the Checksum object. We must call the getValue() method of this object to obtain the checksum value.

The class structure for CheckedInputStream is given as

public class java.util.zip.CheckedInputStream extends java.io.FilterOutputStream{
// Public Constructor
public CheckedInputStream(InputStream in, Checksum cksum);
// Public Instance Methods
public Checksum getChecksum();
public int read() throws IOException; // Overrides FilterInputStream
public int read(byte[] buf, int off, int len) throws IOException; //Overrides FilterInputStream
public long skip(long n) throws IOException; // Overrides FilterInputStream
}

The details of the class structure are given as follows:

public CheckedInputStream(InputStream in, Checksum cksum);

public CheckedInputStream(InputStream in, Checksum cksum) constructor creates a CheckedInputStream that reads data from the given InputStream and updates the given Checksum.

READ  Class OutputStreamWriter in Java

Parameter
in – The underlying input stream to use as a data source.
cksum – The checksum object.

public Checksum getChecksum();

public Checksum getChecksum() method returns the Checksum object associated with this input stream.

This method returns the Checksum associated with this input stream.

public int read();

public int read() method reads the next byte from the underlying InputStream and then updates the checksum. The method blocks until some data are available, the end of the stream is detected, or an exception is thrown.

This method returns the next byte from the stream or -1 if the end of the stream has been reached.

public int read(byte[] buf, int off, int len);

public int read(byte[] buf, int off, int len) method reads up to len bytes from the underlying InputStream and places them into the given array starting at off. The checksum is then updated with the data that has been read. The method blocks until some data are available.

This method returns the number of bytes read or -1 if the end of the stream is encountered immediately.

Parameter
buf – An array of bytes to be filled from the stream.
off – An offset into the byte array.
len – The number of bytes to read.

public long skip(long n);

public long skip(long n) method skips over the specified number of bytes in the underlying InputStream. The skipped bytes are not calculated into the checksum.

This method returns the actual number of bytes skipped.

Parameter
n – The number of bytes to skip.

READ  Bean Customizer in Java

Apart from these CheckedInputStream class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

CheckedInputStream class also has inherited methods from FilterInputStream. They are as follows:

  • available()
  • close()
  • markSupported()
  • reset()
  • mark(int)
  • read(byte[])

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *