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