0N/A/*
2362N/A * Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.util.zip;
0N/A
4550N/Aimport java.nio.ByteBuffer;
4550N/Aimport sun.nio.ch.DirectBuffer;
4550N/A
0N/A/**
0N/A * A class that can be used to compute the Adler-32 checksum of a data
0N/A * stream. An Adler-32 checksum is almost as reliable as a CRC-32 but
0N/A * can be computed much faster.
0N/A *
0N/A * @see Checksum
0N/A * @author David Connelly
0N/A */
0N/Apublic
0N/Aclass Adler32 implements Checksum {
0N/A private int adler = 1;
0N/A
0N/A /**
0N/A * Creates a new Adler32 object.
0N/A */
0N/A public Adler32() {
0N/A }
0N/A
0N/A /**
1332N/A * Updates the checksum with the specified byte (the low eight
1332N/A * bits of the argument b).
0N/A *
1332N/A * @param b the byte to update the checksum with
0N/A */
0N/A public void update(int b) {
0N/A adler = update(adler, b);
0N/A }
0N/A
0N/A /**
1332N/A * Updates the checksum with the specified array of bytes.
0N/A */
0N/A public void update(byte[] b, int off, int len) {
0N/A if (b == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A if (off < 0 || len < 0 || off > b.length - len) {
0N/A throw new ArrayIndexOutOfBoundsException();
0N/A }
0N/A adler = updateBytes(adler, b, off, len);
0N/A }
0N/A
0N/A /**
1332N/A * Updates the checksum with the specified array of bytes.
1332N/A *
1332N/A * @param b the byte array to update the checksum with
0N/A */
0N/A public void update(byte[] b) {
0N/A adler = updateBytes(adler, b, 0, b.length);
0N/A }
0N/A
0N/A /**
4550N/A * Updates the checksum with the bytes from the specified buffer.
4550N/A *
4550N/A * The checksum is updated using
4550N/A * buffer.{@link java.nio.Buffer#remaining() remaining()}
4550N/A * bytes starting at
4550N/A * buffer.{@link java.nio.Buffer#position() position()}
4550N/A * Upon return, the buffer's position will be updated to its
4550N/A * limit; its limit will not have been changed.
4550N/A *
4550N/A * @param buffer the ByteBuffer to update the checksum with
4550N/A */
4550N/A private void update(ByteBuffer buffer) {
4550N/A int pos = buffer.position();
4550N/A int limit = buffer.limit();
4550N/A assert (pos <= limit);
4550N/A int rem = limit - pos;
4550N/A if (rem <= 0)
4550N/A return;
4550N/A if (buffer instanceof DirectBuffer) {
4550N/A adler = updateByteBuffer(adler, ((DirectBuffer)buffer).address(), pos, rem);
4550N/A } else if (buffer.hasArray()) {
4550N/A adler = updateBytes(adler, buffer.array(), pos + buffer.arrayOffset(), rem);
4550N/A } else {
4550N/A byte[] b = new byte[rem];
4550N/A buffer.get(b);
4550N/A adler = updateBytes(adler, b, 0, b.length);
4550N/A }
4550N/A buffer.position(limit);
4550N/A }
4550N/A
4550N/A /**
1332N/A * Resets the checksum to initial value.
0N/A */
0N/A public void reset() {
0N/A adler = 1;
0N/A }
0N/A
0N/A /**
1332N/A * Returns the checksum value.
0N/A */
0N/A public long getValue() {
0N/A return (long)adler & 0xffffffffL;
0N/A }
0N/A
4550N/A // Set up JavaUtilZipAccess in SharedSecrets
4550N/A static {
4550N/A sun.misc.SharedSecrets.setJavaUtilZipAccess(new sun.misc.JavaUtilZipAccess() {
4550N/A public void update(Adler32 adler32, ByteBuffer buf) {
4550N/A adler32.update(buf);
4550N/A }
4550N/A });
4550N/A }
4550N/A
0N/A private native static int update(int adler, int b);
0N/A private native static int updateBytes(int adler, byte[] b, int off,
0N/A int len);
4550N/A private native static int updateByteBuffer(int adler, long addr,
4550N/A int off, int len);
0N/A}