emul/mini/src/main/java/java/util/zip/Adler32.java
brancharithmetic
changeset 774 42bc1e89134d
parent 755 5652acd48509
parent 773 406faa8bc64f
child 778 6f8683517f1f
     1.1 --- a/emul/mini/src/main/java/java/util/zip/Adler32.java	Mon Feb 25 19:00:08 2013 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,205 +0,0 @@
     1.4 -/* Adler32.java - Computes Adler32 data checksum of a data stream
     1.5 -   Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
     1.6 -
     1.7 -This file is part of GNU Classpath.
     1.8 -
     1.9 -GNU Classpath is free software; you can redistribute it and/or modify
    1.10 -it under the terms of the GNU General Public License as published by
    1.11 -the Free Software Foundation; either version 2, or (at your option)
    1.12 -any later version.
    1.13 -
    1.14 -GNU Classpath is distributed in the hope that it will be useful, but
    1.15 -WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.17 -General Public License for more details.
    1.18 -
    1.19 -You should have received a copy of the GNU General Public License
    1.20 -along with GNU Classpath; see the file COPYING.  If not, write to the
    1.21 -Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    1.22 -02111-1307 USA.
    1.23 -
    1.24 -Linking this library statically or dynamically with other modules is
    1.25 -making a combined work based on this library.  Thus, the terms and
    1.26 -conditions of the GNU General Public License cover the whole
    1.27 -combination.
    1.28 -
    1.29 -As a special exception, the copyright holders of this library give you
    1.30 -permission to link this library with independent modules to produce an
    1.31 -executable, regardless of the license terms of these independent
    1.32 -modules, and to copy and distribute the resulting executable under
    1.33 -terms of your choice, provided that you also meet, for each linked
    1.34 -independent module, the terms and conditions of the license of that
    1.35 -module.  An independent module is a module which is not derived from
    1.36 -or based on this library.  If you modify this library, you may extend
    1.37 -this exception to your version of the library, but you are not
    1.38 -obligated to do so.  If you do not wish to do so, delete this
    1.39 -exception statement from your version. */
    1.40 -
    1.41 -package java.util.zip;
    1.42 -
    1.43 -/*
    1.44 - * Written using on-line Java Platform 1.2 API Specification, as well
    1.45 - * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
    1.46 - * The actual Adler32 algorithm is taken from RFC 1950.
    1.47 - * Status:  Believed complete and correct.
    1.48 - */
    1.49 -
    1.50 -/**
    1.51 - * Computes Adler32 checksum for a stream of data. An Adler32 
    1.52 - * checksum is not as reliable as a CRC32 checksum, but a lot faster to 
    1.53 - * compute.
    1.54 - *<p>
    1.55 - * The specification for Adler32 may be found in RFC 1950.
    1.56 - * (ZLIB Compressed Data Format Specification version 3.3)
    1.57 - *<p>
    1.58 - *<p>
    1.59 - * From that document:
    1.60 - *<p>
    1.61 - *      "ADLER32 (Adler-32 checksum)
    1.62 - *       This contains a checksum value of the uncompressed data
    1.63 - *       (excluding any dictionary data) computed according to Adler-32
    1.64 - *       algorithm. This algorithm is a 32-bit extension and improvement
    1.65 - *       of the Fletcher algorithm, used in the ITU-T X.224 / ISO 8073
    1.66 - *       standard. 
    1.67 - *<p>
    1.68 - *       Adler-32 is composed of two sums accumulated per byte: s1 is
    1.69 - *       the sum of all bytes, s2 is the sum of all s1 values. Both sums
    1.70 - *       are done modulo 65521. s1 is initialized to 1, s2 to zero.  The
    1.71 - *       Adler-32 checksum is stored as s2*65536 + s1 in most-
    1.72 - *       significant-byte first (network) order."
    1.73 - *<p>
    1.74 - * "8.2. The Adler-32 algorithm
    1.75 - *<p>
    1.76 - *    The Adler-32 algorithm is much faster than the CRC32 algorithm yet
    1.77 - *    still provides an extremely low probability of undetected errors.
    1.78 - *<p>
    1.79 - *    The modulo on unsigned long accumulators can be delayed for 5552
    1.80 - *    bytes, so the modulo operation time is negligible.  If the bytes
    1.81 - *    are a, b, c, the second sum is 3a + 2b + c + 3, and so is position
    1.82 - *    and order sensitive, unlike the first sum, which is just a
    1.83 - *    checksum.  That 65521 is prime is important to avoid a possible
    1.84 - *    large class of two-byte errors that leave the check unchanged.
    1.85 - *    (The Fletcher checksum uses 255, which is not prime and which also
    1.86 - *    makes the Fletcher check insensitive to single byte changes 0 <->
    1.87 - *    255.)
    1.88 - *<p>
    1.89 - *    The sum s1 is initialized to 1 instead of zero to make the length
    1.90 - *    of the sequence part of s2, so that the length does not have to be
    1.91 - *   checked separately. (Any sequence of zeroes has a Fletcher
    1.92 - *    checksum of zero.)"
    1.93 - *
    1.94 - * @author John Leuner, Per Bothner
    1.95 - * @since JDK 1.1
    1.96 - *
    1.97 - * @see InflaterInputStream
    1.98 - * @see DeflaterOutputStream
    1.99 - */
   1.100 -public class Adler32 implements Checksum
   1.101 -{
   1.102 -
   1.103 -  /** largest prime smaller than 65536 */
   1.104 -  private static final int BASE = 65521;
   1.105 -
   1.106 -  private int checksum; //we do all in int.
   1.107 -
   1.108 -  //Note that java doesn't have unsigned integers,
   1.109 -  //so we have to be careful with what arithmetic 
   1.110 -  //we do. We return the checksum as a long to 
   1.111 -  //avoid sign confusion.
   1.112 -
   1.113 -  /**
   1.114 -   * Creates a new instance of the <code>Adler32</code> class. 
   1.115 -   * The checksum starts off with a value of 1. 
   1.116 -   */
   1.117 -  public Adler32 ()
   1.118 -  {
   1.119 -    reset();
   1.120 -  }
   1.121 -
   1.122 -  /**
   1.123 -   * Resets the Adler32 checksum to the initial value.
   1.124 -   */
   1.125 -  public void reset () 
   1.126 -  {
   1.127 -    checksum = 1; //Initialize to 1    
   1.128 -  }
   1.129 -
   1.130 -  /**
   1.131 -   * Updates the checksum with the byte b. 
   1.132 -   *
   1.133 -   * @param bval the data value to add. The high byte of the int is ignored.
   1.134 -   */
   1.135 -  public void update (int bval)
   1.136 -  {
   1.137 -    //We could make a length 1 byte array and call update again, but I
   1.138 -    //would rather not have that overhead
   1.139 -    int s1 = checksum & 0xffff;
   1.140 -    int s2 = checksum >>> 16;
   1.141 -    
   1.142 -    s1 = (s1 + (bval & 0xFF)) % BASE;
   1.143 -    s2 = (s1 + s2) % BASE;
   1.144 -    
   1.145 -    checksum = (s2 << 16) + s1;
   1.146 -  }
   1.147 -
   1.148 -  /**
   1.149 -   * Updates the checksum with the bytes taken from the array. 
   1.150 -   * 
   1.151 -   * @param buffer an array of bytes
   1.152 -   */
   1.153 -  public void update (byte[] buffer)
   1.154 -  {
   1.155 -    update(buffer, 0, buffer.length);
   1.156 -  }
   1.157 -
   1.158 -  /**
   1.159 -   * Updates the checksum with the bytes taken from the array. 
   1.160 -   * 
   1.161 -   * @param buf an array of bytes
   1.162 -   * @param off the start of the data used for this update
   1.163 -   * @param len the number of bytes to use for this update
   1.164 -   */
   1.165 -  public void update (byte[] buf, int off, int len)
   1.166 -  {
   1.167 -    //(By Per Bothner)
   1.168 -    int s1 = checksum & 0xffff;
   1.169 -    int s2 = checksum >>> 16;
   1.170 -
   1.171 -    while (len > 0)
   1.172 -      {
   1.173 -	// We can defer the modulo operation:
   1.174 -	// s1 maximally grows from 65521 to 65521 + 255 * 3800
   1.175 -	// s2 maximally grows by 3800 * median(s1) = 2090079800 < 2^31
   1.176 -	int n = 3800;
   1.177 -	if (n > len)
   1.178 -	  n = len;
   1.179 -	len -= n;
   1.180 -	while (--n >= 0)
   1.181 -	  {
   1.182 -	    s1 = s1 + (buf[off++] & 0xFF);
   1.183 -	    s2 = s2 + s1;
   1.184 -	  }
   1.185 -	s1 %= BASE;
   1.186 -	s2 %= BASE;
   1.187 -      }
   1.188 -
   1.189 -    /*Old implementation, borrowed from somewhere:
   1.190 -    int n;
   1.191 -    
   1.192 -    while (len-- > 0) {
   1.193 -
   1.194 -      s1 = (s1 + (bs[offset++] & 0xff)) % BASE; 
   1.195 -      s2 = (s2 + s1) % BASE;
   1.196 -    }*/
   1.197 -    
   1.198 -    checksum = (s2 << 16) | s1;
   1.199 -  }
   1.200 -
   1.201 -  /**
   1.202 -   * Returns the Adler32 data checksum computed so far.
   1.203 -   */
   1.204 -  public long getValue()
   1.205 -  {
   1.206 -    return (long) checksum & 0xffffffffL;
   1.207 -  }
   1.208 -}