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 / Adler32.java next >
Encoding:
Java Source  |  1998-03-20  |  2.0 KB  |  87 lines

  1. /*
  2.  * @(#)Adler32.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 Adler-32 checksum of a data
  19.  * stream. An Adler-32 checksum is almost as reliable as a CRC-32 but
  20.  * can be computed much faster.
  21.  *
  22.  * @see        Checksum
  23.  * @version     1.16, 03/18/98
  24.  * @author     David Connelly
  25.  */
  26. public
  27. class Adler32 implements Checksum {
  28.     private int adler = 1;
  29.  
  30.     /*
  31.      * Loads the ZLIB library.
  32.      */
  33.     static {
  34.     try {
  35.         java.security.AccessController.beginPrivileged();
  36.         System.loadLibrary("zip");
  37.     } finally {
  38.         java.security.AccessController.endPrivileged();
  39.     }
  40.     }
  41.  
  42.     /**
  43.      * Updates checksum with specified byte.
  44.      */
  45.     public void update(int b) {
  46.     adler = update(adler, b);
  47.     }
  48.  
  49.     /**
  50.      * Updates checksum with specified array of bytes.
  51.      */
  52.     public void update(byte[] b, int off, int len) {
  53.     if (b == null) {
  54.         throw new NullPointerException();
  55.     }
  56.     if (off < 0 || len < 0 || off + len > b.length) {
  57.         throw new ArrayIndexOutOfBoundsException();
  58.     }
  59.     adler = updateBytes(adler, b, off, len);
  60.     }
  61.  
  62.     /**
  63.      * Updates checksum with specified array of bytes.
  64.      */
  65.     public void update(byte[] b) {
  66.     adler = updateBytes(adler, b, 0, b.length);
  67.     }
  68.  
  69.     /**
  70.      * Resets checksum to initial value.
  71.      */
  72.     public void reset() {
  73.     adler = 1;
  74.     }
  75.  
  76.     /**
  77.      * Returns checksum value.
  78.      */
  79.     public long getValue() {
  80.     return (long)adler & 0xffffffffL;
  81.     }
  82.  
  83.     private native static int update(int adler, int b);
  84.     private native static int updateBytes(int adler, byte[] b, int off,
  85.                       int len);
  86. }
  87.