Home | History | Annotate | Download | only in zip
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one or more
      3  * contributor license agreements.  See the NOTICE file distributed with
      4  * this work for additional information regarding copyright ownership.
      5  * The ASF licenses this file to You under the Apache License, Version 2.0
      6  * (the "License"); you may not use this file except in compliance with
      7  * the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package java.util.zip;
     19 
     20 import java.util.Arrays;
     21 
     22 /**
     23  * The Adler-32 class is used to compute the {@code Adler32} checksum from a set
     24  * of data. Compared to {@link CRC32} it trades reliability for speed.
     25  * Refer to RFC 1950 for the specification.
     26  */
     27 public class Adler32 implements Checksum {
     28 
     29     private long adler = 1;
     30 
     31     /**
     32      * Returns the {@code Adler32} checksum for all input received.
     33      *
     34      * @return The checksum for this instance.
     35      */
     36     public long getValue() {
     37         return adler;
     38     }
     39 
     40     /**
     41      * Reset this instance to its initial checksum.
     42      */
     43     public void reset() {
     44         adler = 1;
     45     }
     46 
     47     /**
     48      * Update this {@code Adler32} checksum with the single byte provided as
     49      * argument.
     50      *
     51      * @param i
     52      *            the byte to update checksum with.
     53      */
     54     public void update(int i) {
     55         adler = updateByteImpl(i, adler);
     56     }
     57 
     58     /**
     59      * Update this {@code Adler32} checksum using the contents of {@code buf}.
     60      *
     61      * @param buf
     62      *            bytes to update checksum with.
     63      */
     64     public void update(byte[] buf) {
     65         update(buf, 0, buf.length);
     66     }
     67 
     68     /**
     69      * Update this {@code Adler32} checksum with the contents of {@code buf},
     70      * starting from {@code offset} and reading {@code byteCount} bytes of data.
     71      */
     72     public void update(byte[] buf, int offset, int byteCount) {
     73         Arrays.checkOffsetAndCount(buf.length, offset, byteCount);
     74         adler = updateImpl(buf, offset, byteCount, adler);
     75     }
     76 
     77     private native long updateImpl(byte[] buf, int offset, int byteCount, long adler1);
     78 
     79     private native long updateByteImpl(int val, long adler1);
     80 }
     81