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

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

Print this page




   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
  23  * questions.
  24  */
  25 
  26 package java.util.zip;
  27 
  28 import java.nio.ByteBuffer;


  29 import sun.nio.ch.DirectBuffer;

  30 
  31 /**
  32  * A class that can be used to compute the CRC-32 of a data stream.
  33  *
  34  * <p> Passing a {@code null} argument to a method in this class will cause
  35  * a {@link NullPointerException} to be thrown.</p>
  36  *
  37  * @author      David Connelly
  38  */
  39 public
  40 class CRC32 implements Checksum {
  41     private int crc;
  42 
  43     /**
  44      * Creates a new CRC32 object.
  45      */
  46     public CRC32() {
  47     }
  48 
  49 


 106         }
 107         buffer.position(limit);
 108     }
 109 
 110     /**
 111      * Resets CRC-32 to initial value.
 112      */
 113     @Override
 114     public void reset() {
 115         crc = 0;
 116     }
 117 
 118     /**
 119      * Returns CRC-32 value.
 120      */
 121     @Override
 122     public long getValue() {
 123         return (long)crc & 0xffffffffL;
 124     }
 125 

 126     private native static int update(int crc, int b);
 127     private native static int updateBytes(int crc, byte[] b, int off, int len);
 128 
 129     private native static int updateByteBuffer(int adler, long addr,
































 130                                                int off, int len);








 131 }


   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
  23  * questions.
  24  */
  25 
  26 package java.util.zip;
  27 
  28 import java.nio.ByteBuffer;
  29 import java.util.Objects;
  30 
  31 import sun.nio.ch.DirectBuffer;
  32 import jdk.internal.HotSpotIntrinsicCandidate;
  33 
  34 /**
  35  * A class that can be used to compute the CRC-32 of a data stream.
  36  *
  37  * <p> Passing a {@code null} argument to a method in this class will cause
  38  * a {@link NullPointerException} to be thrown.</p>
  39  *
  40  * @author      David Connelly
  41  */
  42 public
  43 class CRC32 implements Checksum {
  44     private int crc;
  45 
  46     /**
  47      * Creates a new CRC32 object.
  48      */
  49     public CRC32() {
  50     }
  51 
  52 


 109         }
 110         buffer.position(limit);
 111     }
 112 
 113     /**
 114      * Resets CRC-32 to initial value.
 115      */
 116     @Override
 117     public void reset() {
 118         crc = 0;
 119     }
 120 
 121     /**
 122      * Returns CRC-32 value.
 123      */
 124     @Override
 125     public long getValue() {
 126         return (long)crc & 0xffffffffL;
 127     }
 128 
 129     @HotSpotIntrinsicCandidate
 130     private native static int update(int crc, int b);

 131 
 132     private static int updateBytes(int crc, byte[] b, int off, int len) {
 133         updateBytesCheck(b, off, len);
 134         return updateBytes0(crc, b, off, len);
 135     }
 136 
 137     @HotSpotIntrinsicCandidate
 138     private native static int updateBytes0(int crc, byte[] b, int off, int len);
 139 
 140     private static void updateBytesCheck(byte[] b, int off, int len) {
 141         if (len <= 0) {
 142             return;  // not an error because updateBytesImpl won't execute if len <= 0
 143         }
 144 
 145         Objects.requireNonNull(b);
 146 
 147         if (off < 0 || off >= b.length) {
 148             throw new ArrayIndexOutOfBoundsException(off);
 149         }
 150 
 151         int endIndex = off + len - 1;
 152         if (endIndex < 0 || endIndex >= b.length) {
 153             throw new ArrayIndexOutOfBoundsException(endIndex);
 154         }
 155     }
 156 
 157     private static int updateByteBuffer(int alder, long addr,
 158                                         int off, int len) {
 159         updateByteBufferCheck(addr);
 160         return updateByteBuffer0(alder, addr, off, len);
 161     }
 162 
 163     @HotSpotIntrinsicCandidate
 164     private native static int updateByteBuffer0(int alder, long addr,
 165                                                 int off, int len);
 166 
 167     private static void updateByteBufferCheck(long addr) {
 168         // Performs only a null check because bounds checks
 169         // are not easy to do on raw addresses.
 170         if (addr == 0L) {
 171             throw new NullPointerException();
 172         }
 173     }
 174 }
src/java.base/share/classes/java/util/zip/CRC32.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File