home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / util / zip / CRC32.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  1.8 KB  |  84 lines

  1. /*
  2.  * @(#)CRC32.java    1.16 98/03/18
  3.  *
  4.  * Copyright 1996, 1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.util.zip;
  16.  
  17. /**
  18.  * A class that can be used to compute the CRC-32 of a data stream.
  19.  *
  20.  * @see        Checksum
  21.  * @version     1.16, 03/18/98
  22.  * @author     David Connelly
  23.  */
  24. public
  25. class CRC32 implements Checksum {
  26.     private int crc;
  27.  
  28.     /*
  29.      * Loads the ZLIB library.
  30.      */
  31.     static {
  32.     try {
  33.         java.security.AccessController.beginPrivileged();
  34.         System.loadLibrary("zip");
  35.     } finally {
  36.         java.security.AccessController.endPrivileged();
  37.     }
  38.     }
  39.  
  40.     /**
  41.      * Updates CRC-32 with specified byte.
  42.      */
  43.     public void update(int b) {
  44.     crc = update(crc, b);
  45.     }
  46.  
  47.     /**
  48.      * Updates CRC-32 with specified array of bytes.
  49.      */
  50.     public void update(byte[] b, int off, int len) {
  51.     if (b == null) {
  52.         throw new NullPointerException();
  53.     }
  54.     if (off < 0 || len < 0 || off + len > b.length) {
  55.         throw new ArrayIndexOutOfBoundsException();
  56.     }
  57.     crc = updateBytes(crc, b, off, len);
  58.     }
  59.  
  60.     /**
  61.      * Updates checksum with specified array of bytes.
  62.      */
  63.     public void update(byte[] b) {
  64.     crc = updateBytes(crc, b, 0, b.length);
  65.     }
  66.  
  67.     /**
  68.      * Resets CRC-32 to initial value.
  69.      */
  70.     public void reset() {
  71.     crc = 0;
  72.     }
  73.  
  74.     /**
  75.      * Returns CRC-32 value.
  76.      */
  77.     public long getValue() {
  78.     return (long)crc & 0xffffffffL;
  79.     }
  80.  
  81.     private native static int update(int crc, int b);
  82.     private native static int updateBytes(int crc, byte[] b, int off, int len);
  83. }
  84.