jdk/src/share/classes/java/util/zip/CRC32.java
author sherman
Thu, 15 Aug 2013 10:41:59 -0700
changeset 19415 af60b1abf237
parent 14342 8435a30053c1
child 23010 6dadb192ad81
permissions -rw-r--r--
7154662: {CRC32, Adler32}.update(byte[] b, int off, int len): undocumented ArrayIndexOutOfBoundsException Summary: to add the throws clause Reviewed-by: alanb, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 11113
diff changeset
     2
 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3078
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3078
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3078
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3078
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3078
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util.zip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
11113
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    28
import java.nio.ByteBuffer;
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    29
import sun.nio.ch.DirectBuffer;
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    30
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A class that can be used to compute the CRC-32 of a data stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
11113
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    34
 * <p> Passing a {@code null} argument to a method in this class will cause
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    35
 * a {@link NullPointerException} to be thrown.
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    36
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @see         Checksum
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author      David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
class CRC32 implements Checksum {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private int crc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * Creates a new CRC32 object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public CRC32() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    52
     * Updates the CRC-32 checksum with the specified byte (the low
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    53
     * eight bits of the argument b).
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    54
     *
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    55
     * @param b the byte to update the checksum with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public void update(int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        crc = update(crc, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    62
     * Updates the CRC-32 checksum with the specified array of bytes.
19415
af60b1abf237 7154662: {CRC32, Adler32}.update(byte[] b, int off, int len): undocumented ArrayIndexOutOfBoundsException
sherman
parents: 14342
diff changeset
    63
     *
af60b1abf237 7154662: {CRC32, Adler32}.update(byte[] b, int off, int len): undocumented ArrayIndexOutOfBoundsException
sherman
parents: 14342
diff changeset
    64
     * @throws  ArrayIndexOutOfBoundsException
af60b1abf237 7154662: {CRC32, Adler32}.update(byte[] b, int off, int len): undocumented ArrayIndexOutOfBoundsException
sherman
parents: 14342
diff changeset
    65
     *          if {@code off} is negative, or {@code len} is negative,
af60b1abf237 7154662: {CRC32, Adler32}.update(byte[] b, int off, int len): undocumented ArrayIndexOutOfBoundsException
sherman
parents: 14342
diff changeset
    66
     *          or {@code off+len} is greater than the length of the
af60b1abf237 7154662: {CRC32, Adler32}.update(byte[] b, int off, int len): undocumented ArrayIndexOutOfBoundsException
sherman
parents: 14342
diff changeset
    67
     *          array {@code b}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public void update(byte[] b, int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        crc = updateBytes(crc, b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    80
     * Updates the CRC-32 checksum with the specified array of bytes.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param b the array of bytes to update the checksum with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public void update(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        crc = updateBytes(crc, b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
11113
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    89
     * Updates the checksum with the bytes from the specified buffer.
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    90
     *
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    91
     * The checksum is updated using
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    92
     * buffer.{@link java.nio.Buffer#remaining() remaining()}
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    93
     * bytes starting at
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    94
     * buffer.{@link java.nio.Buffer#position() position()}
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    95
     * Upon return, the buffer's position will
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    96
     * be updated to its limit; its limit will not have been changed.
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    97
     *
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    98
     * @param buffer the ByteBuffer to update the checksum with
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    99
     * @since 1.8
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   100
     */
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   101
    public void update(ByteBuffer buffer) {
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   102
        int pos = buffer.position();
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   103
        int limit = buffer.limit();
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   104
        assert (pos <= limit);
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   105
        int rem = limit - pos;
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   106
        if (rem <= 0)
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   107
            return;
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   108
        if (buffer instanceof DirectBuffer) {
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   109
            crc = updateByteBuffer(crc, ((DirectBuffer)buffer).address(), pos, rem);
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   110
        } else if (buffer.hasArray()) {
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   111
            crc = updateBytes(crc, buffer.array(), pos + buffer.arrayOffset(), rem);
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   112
        } else {
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   113
            byte[] b = new byte[rem];
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   114
            buffer.get(b);
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   115
            crc = updateBytes(crc, b, 0, b.length);
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   116
        }
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   117
        buffer.position(limit);
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   118
    }
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   119
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   120
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Resets CRC-32 to initial value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        crc = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Returns CRC-32 value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public long getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return (long)crc & 0xffffffffL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private native static int update(int crc, int b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private native static int updateBytes(int crc, byte[] b, int off, int len);
11113
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   136
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   137
    private native static int updateByteBuffer(int adler, long addr,
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   138
                                               int off, int len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
}