1. /*
  2. * @(#)Adler32.java 1.26 03/02/07
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util.zip;
  8. /**
  9. * A class that can be used to compute the Adler-32 checksum of a data
  10. * stream. An Adler-32 checksum is almost as reliable as a CRC-32 but
  11. * can be computed much faster.
  12. *
  13. * @see Checksum
  14. * @version 1.26, 02/07/03
  15. * @author David Connelly
  16. */
  17. public
  18. class Adler32 implements Checksum {
  19. private int adler = 1;
  20. /*
  21. * Loads the ZLIB library.
  22. */
  23. static {
  24. java.security.AccessController.doPrivileged(
  25. new sun.security.action.LoadLibraryAction("zip"));
  26. }
  27. /**
  28. * Creates a new Adler32 object.
  29. */
  30. public Adler32() {
  31. }
  32. /**
  33. * Updates checksum with specified byte.
  34. *
  35. * @param b an array of bytes
  36. */
  37. public void update(int b) {
  38. adler = update(adler, b);
  39. }
  40. /**
  41. * Updates checksum with specified array of bytes.
  42. */
  43. public void update(byte[] b, int off, int len) {
  44. if (b == null) {
  45. throw new NullPointerException();
  46. }
  47. if (off < 0 || len < 0 || off > b.length - len) {
  48. throw new ArrayIndexOutOfBoundsException();
  49. }
  50. adler = updateBytes(adler, b, off, len);
  51. }
  52. /**
  53. * Updates checksum with specified array of bytes.
  54. */
  55. public void update(byte[] b) {
  56. adler = updateBytes(adler, b, 0, b.length);
  57. }
  58. /**
  59. * Resets checksum to initial value.
  60. */
  61. public void reset() {
  62. adler = 1;
  63. }
  64. /**
  65. * Returns checksum value.
  66. */
  67. public long getValue() {
  68. return (long)adler & 0xffffffffL;
  69. }
  70. private native static int update(int adler, int b);
  71. private native static int updateBytes(int adler, byte[] b, int off,
  72. int len);
  73. }