src/share/classes/java/util/zip/CRC32.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File jdk Sdiff src/share/classes/java/util/zip

src/share/classes/java/util/zip/CRC32.java

Print this page


   1 /*
   2  * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  38  * @author      David Connelly
  39  */
  40 public
  41 class CRC32 implements Checksum {
  42     private int crc;
  43 
  44     /**
  45      * Creates a new CRC32 object.
  46      */
  47     public CRC32() {
  48     }
  49 
  50 
  51     /**
  52      * Updates the CRC-32 checksum with the specified byte (the low
  53      * eight bits of the argument b).
  54      *
  55      * @param b the byte to update the checksum with
  56      */
  57     public void update(int b) {
  58         crc = update(crc, b);



  59     }
  60 
  61     /**
  62      * Updates the CRC-32 checksum with the specified array of bytes.
  63      */
  64     public void update(byte[] b, int off, int len) {
  65         if (b == null) {
  66             throw new NullPointerException();
  67         }
  68         if (off < 0 || len < 0 || off > b.length - len) {
  69             throw new ArrayIndexOutOfBoundsException();
  70         }




  71         crc = updateBytes(crc, b, off, len);
  72     }

  73 
  74     /**
  75      * Updates the CRC-32 checksum with the specified array of bytes.
  76      *
  77      * @param b the array of bytes to update the checksum with
  78      */
  79     public void update(byte[] b) {
  80         crc = updateBytes(crc, b, 0, b.length);
  81     }
  82 
  83     /**
  84      * Updates the checksum with the bytes from the specified buffer.
  85      *
  86      * The checksum is updated using
  87      * buffer.{@link java.nio.Buffer#remaining() remaining()}
  88      * bytes starting at
  89      * buffer.{@link java.nio.Buffer#position() position()}
  90      * Upon return, the buffer's position will
  91      * be updated to its limit; its limit will not have been changed.
  92      *


 114 
 115     /**
 116      * Resets CRC-32 to initial value.
 117      */
 118     public void reset() {
 119         crc = 0;
 120     }
 121 
 122     /**
 123      * Returns CRC-32 value.
 124      */
 125     public long getValue() {
 126         return (long)crc & 0xffffffffL;
 127     }
 128 
 129     private native static int update(int crc, int b);
 130     private native static int updateBytes(int crc, byte[] b, int off, int len);
 131 
 132     private native static int updateByteBuffer(int adler, long addr,
 133                                                int off, int len);








































 134 }
   1 /*
   2  * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  38  * @author      David Connelly
  39  */
  40 public
  41 class CRC32 implements Checksum {
  42     private int crc;
  43 
  44     /**
  45      * Creates a new CRC32 object.
  46      */
  47     public CRC32() {
  48     }
  49 
  50 
  51     /**
  52      * Updates the CRC-32 checksum with the specified byte (the low
  53      * eight bits of the argument b).
  54      *
  55      * @param b the byte to update the checksum with
  56      */
  57     public void update(int b) {
  58         int c = ~ crc;
  59         b = timesXtoThe32[(b ^ c) & 0xFF];
  60         b = b ^ (c >>> 8);
  61         crc = ~b;
  62     }
  63 
  64     /**
  65      * Updates the CRC-32 checksum with the specified array of bytes.
  66      */
  67     public void update(byte[] b, int off, int len) {
  68         if (b == null) {
  69             throw new NullPointerException();
  70         }
  71         if (off < 0 || len < 0 || off > b.length - len) {
  72             throw new ArrayIndexOutOfBoundsException();
  73         }
  74 
  75         if (len < javaCRCIfSmallerThan) {
  76             crc = updateBytesSimple(crc, b, off, len);
  77         } else {
  78             crc = updateBytes(crc, b, off, len);
  79         }
  80     }
  81 
  82     /**
  83      * Updates the CRC-32 checksum with the specified array of bytes.
  84      *
  85      * @param b the array of bytes to update the checksum with
  86      */
  87     public void update(byte[] b) {
  88         crc = updateBytes(crc, b, 0, b.length);
  89     }
  90 
  91     /**
  92      * Updates the checksum with the bytes from the specified buffer.
  93      *
  94      * The checksum is updated using
  95      * buffer.{@link java.nio.Buffer#remaining() remaining()}
  96      * bytes starting at
  97      * buffer.{@link java.nio.Buffer#position() position()}
  98      * Upon return, the buffer's position will
  99      * be updated to its limit; its limit will not have been changed.
 100      *


 122 
 123     /**
 124      * Resets CRC-32 to initial value.
 125      */
 126     public void reset() {
 127         crc = 0;
 128     }
 129 
 130     /**
 131      * Returns CRC-32 value.
 132      */
 133     public long getValue() {
 134         return (long)crc & 0xffffffffL;
 135     }
 136 
 137     private native static int update(int crc, int b);
 138     private native static int updateBytes(int crc, byte[] b, int off, int len);
 139 
 140     private native static int updateByteBuffer(int adler, long addr,
 141                                                int off, int len);
 142 
 143     /**
 144      *  Simple byte-at-a-time CRC for avoiding the overhead of native call.
 145      *  Breakeven ranges between 60 and 100 bytes.
 146      */
 147     private static int updateBytesSimple(int crc, byte[] b, int off, int len) {
 148         int[] a = timesXtoThe32;
 149         if (a.length < 256)
 150             throw new ArrayIndexOutOfBoundsException();
 151         int c = ~crc;
 152         for (int i = 0; i < len; i++ ) {
 153             int x0 = b[i + off];
 154             x0 = a[(x0 ^ c) & 0xFF];
 155             c = x0 ^ (c >>> 8);
 156         }
 157         return ~c;
 158     }
 159 
 160     /**
 161      * Returns true if CLMUL is both requested and supported.
 162      */
 163     private native static boolean init(int[] timesXtoThe32, boolean try_use_clmul);
 164 
 165     /**
 166      * timesXtoThe32[a] = rep(poly(a)*x**32)
 167      */
 168     static int[] timesXtoThe32;
 169 
 170     /**
 171      * Estimated CRC size below which the JNI overhead is too large.
 172      * May be modified depending on platform properties.
 173      */
 174     static int javaCRCIfSmallerThan = 80;
 175 
 176     static {
 177       timesXtoThe32 = new int[256];
 178       boolean try_use_clmul =
 179            "true".equals(sun.misc.VM.getSavedProperty("sun.zip.clmulSupported"));
 180       init(timesXtoThe32, try_use_clmul);
 181     }
 182 }
src/share/classes/java/util/zip/CRC32.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File