How to Calculate the Checks of a String Using Adler32 in Java

A programmer calculates a checksum for a string or a file to guarantee that file corruption has not occurred. An algorithm is used to generate a specific block of data from the content of the string or the file. Later, other programmers or users recalculate the checksum using the same method and compare the result to the original checksum. It the checksums match, it means the file has not been changed. In Java, "Adler32" modules provide the methods for calculating a checksum from a string.

Things You'll Need

  • Java Development Environment
Show More

Instructions

    • 1

      Import the "Adler32" and "CheckedInputStream" modules, which calculate and store the checksum of a file or string. Use the following code:

      import java.util.zip.CheckedInputStream;
      import java.util.zip.Adler32;

    • 2

      Define a string value and convert it into a byte stream. Adler32 will only calculate checksums on incoming data streams:

      String s = "This is the String";
      byte buff[] = s.getBytes()'
      ByteArrayInputStream ba = new ByteArrayInputStream(buff);

    • 3

      Calculate the checksum using an Adler32 object:

      CheckedInputStream checked = new CheckedInputStream(ba, new Adler32());

Related Searches:

References

Comments

Related Ads

Featured