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 /**
     21  * The CRC32 class is used to compute a CRC32 checksum from data provided as
     22  * input value.
     23  */
     24 public class CRC32 implements java.util.zip.Checksum {
     25 
     26     private long crc = 0L;
     27 
     28     long tbytes = 0L;
     29 
     30     /**
     31      * Returns the CRC32 checksum for all input received.
     32      *
     33      * @return The checksum for this instance.
     34      */
     35     public long getValue() {
     36         return crc;
     37     }
     38 
     39     /**
     40      * Resets the CRC32 checksum to it initial state.
     41      */
     42     public void reset() {
     43         tbytes = crc = 0;
     44 
     45     }
     46 
     47     /**
     48      * Updates this checksum with the byte value provided as integer.
     49      *
     50      * @param val
     51      *            represents the byte to update the checksum.
     52      */
     53     public void update(int val) {
     54         crc = updateByteImpl((byte) val, crc);
     55     }
     56 
     57     /**
     58      * Updates this checksum with the bytes contained in buffer {@code buf}.
     59      *
     60      * @param buf
     61      *            the buffer holding the data to update the checksum with.
     62      */
     63     public void update(byte[] buf) {
     64         update(buf, 0, buf.length);
     65     }
     66 
     67     /**
     68      * Updates this checksum with n bytes of data obtained from buffer {@code
     69      * buf}, starting at offset {@code off}.
     70      *
     71      * @param buf
     72      *            the buffer to update the checksum.
     73      * @param off
     74      *            the offset in {@code buf} to obtain data from.
     75      * @param nbytes
     76      *            the number of bytes to read from {@code buf}.
     77      */
     78     public void update(byte[] buf, int off, int nbytes) {
     79         // avoid int overflow, check null buf
     80         if (off <= buf.length && nbytes >= 0 && off >= 0
     81                 && buf.length - off >= nbytes) {
     82             tbytes += nbytes;
     83             crc = updateImpl(buf, off, nbytes, crc);
     84         } else {
     85             throw new ArrayIndexOutOfBoundsException();
     86         }
     87     }
     88 
     89     private native long updateImpl(byte[] buf, int off, int nbytes, long crc1);
     90 
     91     private native long updateByteImpl(byte val, long crc1);
     92 }
     93