Abstract Interface Checksum in Java

digitization, transformation, binary

Abstract Interface Checksum in Java

Checksum interface defines the methods required to compute a checksum on a stream of data. The checksum is computed based on the bytes of data supplied by the update() methods, and the current value of the checksum can be obtained at any time with the getValue() method. reset() resets the checksum to its default value–use this method before beginning a new stream of data.

The Checksum interface defines the methods that are needed to compute a checksum value for a stream of data. The checksum value can be used for error checking purposes. Note, however, that the checksum value must fit into a long value, so this interface is not suitable for cryptographic checksum algorithms.

Ads code goes here

The Adler32 and CRC32 classes implement the Checksum interface, using the Adler-32 and CRC-32 algorithms, respectively. The CheckedInputStream and CheckedOutputStream classes provide a higher-level mechanism for computing checksums on data streams.

Note that the checksum value computed by a Checksum object and returned through the getValue() method must fit into a long value. Therefore, this interface is not suitable for the cryptographic checksum algorithms used in cryptography and security. The classes CheckedInputStream and CheckedOutputStream provide a higher-level API for computing a checksum on a stream of data.

The interface structure for Checksum is given as

public abstract interface java.util.zip.Checksum{
// Public Instance Methods
public abstract long getValue();
public abstract void reset();
public abstract void update(int b);
public abstract void update(byte[] b, int off, int len);
}

The details of the class structure are given as follows:

public abstract long getValue();

public abstract long getValue() method returns the current value of this checksum.

READ  Class FileNotFoundException in Java

This method returns the current checksum value.

public abstract void reset();

public abstract void reset() method resets the checksum to its initial value, making it appear as though it has not been updated by any data.

public abstract void update(int b);

public abstract void update(int b) method adds the specified value to the data stream and updates the checksum value. The method uses only the lowest eight bits of the given int.

Parameter
b – The value to be added to the data stream for the checksum calculation.

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

public abstract void update(byte[] b, int off, int len) method adds len bytes from the specified array, starting at off, to the data stream and updates the checksum value.

Parameter
b – An array of bytes to be added to the data stream for the checksum calculation.
off – An offset into the byte array.
len – The number of bytes to use.

Share and Enjoy !

Leave a Comment

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