1. /*
  2. * @(#)Adler32.java 1.20 01/11/29
  3. *
  4. * Copyright 2002 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.20, 11/29/01
  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 class.
  29. */
  30. public Adler32() {
  31. }
  32. /**
  33. * Updates checksum with specified byte.
  34. */
  35. public void update(int b) {
  36. adler = update(adler, b);
  37. }
  38. /**
  39. * Updates checksum with specified array of bytes.
  40. */
  41. public void update(byte[] b, int off, int len) {
  42. if (b == null) {
  43. throw new NullPointerException();
  44. }
  45. if (off < 0 || len < 0 || off + len > b.length) {
  46. throw new ArrayIndexOutOfBoundsException();
  47. }
  48. adler = updateBytes(adler, b, off, len);
  49. }
  50. /**
  51. * Updates checksum with specified array of bytes.
  52. */
  53. public void update(byte[] b) {
  54. adler = updateBytes(adler, b, 0, b.length);
  55. }
  56. /**
  57. * Resets checksum to initial value.
  58. */
  59. public void reset() {
  60. adler = 1;
  61. }
  62. /**
  63. * Returns checksum value.
  64. */
  65. public long getValue() {
  66. return (long)adler & 0xffffffffL;
  67. }
  68. private native static int update(int adler, int b);
  69. private native static int updateBytes(int adler, byte[] b, int off,
  70. int len);
  71. }