1. /*
  2. * @(#)CRC32.java 1.24 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 CRC-32 of a data stream.
  13. *
  14. * @see Checksum
  15. * @version 1.24, 02/02/00
  16. * @author David Connelly
  17. */
  18. public
  19. class CRC32 implements Checksum {
  20. private int crc;
  21. /*
  22. * Loads the ZLIB library.
  23. */
  24. static {
  25. java.security.AccessController.doPrivileged(
  26. new sun.security.action.LoadLibraryAction("zip"));
  27. }
  28. /**
  29. * Creates a new CRC32 class.
  30. */
  31. public CRC32() {
  32. }
  33. /**
  34. * Updates CRC-32 with specified byte.
  35. */
  36. public void update(int b) {
  37. crc = update(crc, b);
  38. }
  39. /**
  40. * Updates CRC-32 with specified array of bytes.
  41. */
  42. public void update(byte[] b, int off, int len) {
  43. if (b == null) {
  44. throw new NullPointerException();
  45. }
  46. if (off < 0 || len < 0 || off + len > b.length) {
  47. throw new ArrayIndexOutOfBoundsException();
  48. }
  49. crc = updateBytes(crc, b, off, len);
  50. }
  51. /**
  52. * Updates checksum with specified array of bytes.
  53. *
  54. * @param the array of bytes to update the checksum with
  55. */
  56. public void update(byte[] b) {
  57. crc = updateBytes(crc, b, 0, b.length);
  58. }
  59. /**
  60. * Resets CRC-32 to initial value.
  61. */
  62. public void reset() {
  63. crc = 0;
  64. }
  65. /**
  66. * Returns CRC-32 value.
  67. */
  68. public long getValue() {
  69. return (long)crc & 0xffffffffL;
  70. }
  71. private native static int update(int crc, int b);
  72. private native static int updateBytes(int crc, byte[] b, int off, int len);
  73. }