Class CheckedOutputStream in Java

digitization, transformation, binary

Class CheckedOutputStream in Java

CheckedOutputStream class is a subclass of java.io.FilterOutputStream that allows data to be written to a stream and a checksum computed on that data at the same time.

The CheckOutputStream class represents an OutputStream with an associated checksum. In
other words, a CheckedOutputStream wraps a regular output stream and computes a checksum value as data is written to the stream. The checksum can verify the integrity of the data. When we create a CheckedOutputStream, we must specify an object that implements the Checksum interface that computes the checksum.

Ads code goes here

To create a CheckedOutputStream we specify the output stream that it is to write its data to, and we need to specify a Checksum object, such as an instance of Adler32, that implements the particular checksum algorithm you desire.

The write() methods are similar to those of other OutputStream classes.
The getChecksum() method returns the Checksum object.

Note
We must call getValue() on this object in order to obtain the actual checksum value.

The class structure for CheckedOutputStream is given as

public class java.util.zip.CheckedOutputStream extends java.io.FilterOutputStream{
// Public Constructor
public CheckedOutputStream(OutputStream out, Checksum cksum);
// Public Instance Methods
public Checksum getChecksum();
public void write(int b) throws IOException; // Overrides FilterOutputStream
public void write(byte[] b, int off, int len) throws IOException; //Overrides FilterOutputStream
}

The details of the class structure are given as follows:

public CheckedOutputStream(OutputStream out, Checksum cksum);

public CheckedOutputStream(OutputStream out, Checksum cksum) constructor creates a CheckedOutputStream that writes data to the given OutputStream and updates the given Checksum.

READ  CharArrayReader Class in Java

Parameter
out – The underlying output stream.
cksum – The checksum object.

public Checksum getChecksum();

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

This method returns the Checksum associated with this output stream.

public void write(int b);

public void write(int b) method writes a byte that contains the lowest eight bits of the given integer value to the
underlying OutputStream and then updates the checksum.

Parameter
b – The value to write.

public void write(byte[] b, int off, int len);

public void write(byte[] b, int off, int len) method writes len bytes to the underlying OutputStream from the given array, starting at off. The checksum is then updated with the data that has been written.

Parameter
b – An array of bytes to write to the stream.
off – An offset into the byte array.
len – The number of bytes to write.

Apart from these CheckedOutputStream 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)

CheckedOutputStream class also has inherited methods from class- Object. They are as follows:

  • flush()
  • close()
  • write(byte[])

Share and Enjoy !

Leave a Comment

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