src/java.base/share/classes/java/util/zip/Adler32.java
author jboes
Thu, 21 Nov 2019 09:10:21 +0000
changeset 59201 b24f4caa1411
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234335: Remove line break in class declaration in java.base Summary: Remove line break in class declarations where applicable Reviewed-by: rriggs, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
32637
ac873eaac862 8132081: C2 support for Adler32 on SPARC
kvn
parents: 27737
diff changeset
     2
 * Copyright (c) 1996, 2015, 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
32637
ac873eaac862 8132081: C2 support for Adler32 on SPARC
kvn
parents: 27737
diff changeset
    31
import jdk.internal.HotSpotIntrinsicCandidate;
ac873eaac862 8132081: C2 support for Adler32 on SPARC
kvn
parents: 27737
diff changeset
    32
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * A class that can be used to compute the Adler-32 checksum of a data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * stream. An Adler-32 checksum is almost as reliable as a CRC-32 but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * can be computed much faster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
11113
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    38
 * <p> Passing a {@code null} argument to a method in this class will cause
27737
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents: 25859
diff changeset
    39
 * a {@link NullPointerException} to be thrown.</p>
11113
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    40
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author      David Connelly
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 32649
diff changeset
    42
 * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
59201
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 47216
diff changeset
    44
public class Adler32 implements Checksum {
11113
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    45
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private int adler = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Creates a new Adler32 object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public Adler32() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    55
     * Updates the checksum with the specified byte (the low eight
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    56
     * bits of the argument b).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
27737
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents: 25859
diff changeset
    58
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public void update(int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        adler = update(adler, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    64
     * Updates the 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
    65
     *
27737
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents: 25859
diff changeset
    66
     * @throws ArrayIndexOutOfBoundsException
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents: 25859
diff changeset
    67
     *         if {@code off} is negative, or {@code len} is negative, or
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents: 25859
diff changeset
    68
     *         {@code off+len} is negative or greater than the length of
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents: 25859
diff changeset
    69
     *         the array {@code b}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
27737
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents: 25859
diff changeset
    71
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public void update(byte[] b, int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        adler = updateBytes(adler, b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
11113
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    83
     * 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
    84
     *
27737
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents: 25859
diff changeset
    85
     * The checksum is updated with the remaining bytes in the buffer, starting
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents: 25859
diff changeset
    86
     * at the buffer's position. Upon return, the buffer's position will be
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents: 25859
diff changeset
    87
     * updated to its limit; its limit will not have been changed.
11113
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    88
     *
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    89
     * @since 1.8
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    90
     */
27737
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents: 25859
diff changeset
    91
    @Override
11113
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    92
    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
    93
        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
    94
        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
    95
        assert (pos <= limit);
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    96
        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
    97
        if (rem <= 0)
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    98
            return;
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
    99
        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
   100
            adler = updateByteBuffer(adler, ((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
   101
        } 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
   102
            adler = updateBytes(adler, 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
   103
        } else {
27737
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents: 25859
diff changeset
   104
            byte[] b = new byte[Math.min(buffer.remaining(), 4096)];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents: 25859
diff changeset
   105
            while (buffer.hasRemaining()) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents: 25859
diff changeset
   106
                int length = Math.min(buffer.remaining(), b.length);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents: 25859
diff changeset
   107
                buffer.get(b, 0, length);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents: 25859
diff changeset
   108
                update(b, 0, length);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents: 25859
diff changeset
   109
            }
11113
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   110
        }
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   111
        buffer.position(limit);
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   112
    }
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   113
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   115
     * Resets the checksum to initial value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
27737
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents: 25859
diff changeset
   117
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        adler = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   123
     * Returns the checksum value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
27737
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents: 25859
diff changeset
   125
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public long getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return (long)adler & 0xffffffffL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32637
diff changeset
   130
    private static native int update(int adler, int b);
32637
ac873eaac862 8132081: C2 support for Adler32 on SPARC
kvn
parents: 27737
diff changeset
   131
ac873eaac862 8132081: C2 support for Adler32 on SPARC
kvn
parents: 27737
diff changeset
   132
    @HotSpotIntrinsicCandidate
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32637
diff changeset
   133
    private static native int updateBytes(int adler, byte[] b, int off,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                                          int len);
32637
ac873eaac862 8132081: C2 support for Adler32 on SPARC
kvn
parents: 27737
diff changeset
   135
    @HotSpotIntrinsicCandidate
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32637
diff changeset
   136
    private static native int updateByteBuffer(int adler, long addr,
11113
f7248a4db8db 7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents: 5506
diff changeset
   137
                                               int off, int len);
45924
8bbd04c0791e 8184917: System.initPhase1 does not need to pre-load libzip
alanb
parents: 45434
diff changeset
   138
8bbd04c0791e 8184917: System.initPhase1 does not need to pre-load libzip
alanb
parents: 45434
diff changeset
   139
    static {
8bbd04c0791e 8184917: System.initPhase1 does not need to pre-load libzip
alanb
parents: 45434
diff changeset
   140
        ZipUtils.loadLibrary();
8bbd04c0791e 8184917: System.initPhase1 does not need to pre-load libzip
alanb
parents: 45434
diff changeset
   141
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
}