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