1. /*
  2. * @(#)CRC32.java 1.30 03/12/19
  3. *
  4. * Copyright 2004 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.30, 12/19/03
  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 object.
  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 > b.length - len) {
  44. throw new ArrayIndexOutOfBoundsException();
  45. }
  46. crc = updateBytes(crc, b, off, len);
  47. }
  48. /**
  49. * Updates checksum with specified array of bytes.
  50. *
  51. * @param b the array of bytes to update the checksum with
  52. */
  53. public void update(byte[] b) {
  54. crc = updateBytes(crc, b, 0, b.length);
  55. }
  56. /**
  57. * Resets CRC-32 to initial value.
  58. */
  59. public void reset() {
  60. crc = 0;
  61. }
  62. /**
  63. * Returns CRC-32 value.
  64. */
  65. public long getValue() {
  66. return (long)crc & 0xffffffffL;
  67. }
  68. private native static int update(int crc, int b);
  69. private native static int updateBytes(int crc, byte[] b, int off, int len);
  70. }