Adler32.java revision a8ed084745590c5e4a0e8559b5821809d60fe242
1/* 2 * Copyright (c) 1996, 2005, 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 23 * questions. 24 */ 25 26package java.util.zip; 27 28/* ----- BEGIN android ----- 29import java.nio.ByteBuffer; 30import sun.nio.ch.DirectBuffer; 31----- END android ----- */ 32 33/** 34 * A class that can be used to compute the Adler-32 checksum of a data 35 * stream. An Adler-32 checksum is almost as reliable as a CRC-32 but 36 * can be computed much faster. 37 * 38 * @see Checksum 39 * @author David Connelly 40 */ 41public 42class Adler32 implements Checksum { 43 /* ----- BEGIN android ----- 44 private int adler = 1;*/ 45 private long adler = 1; 46 // ----- END android ----- 47 48 /** 49 * Creates a new Adler32 object. 50 */ 51 public Adler32() { 52 } 53 54 /** 55 * Updates the checksum with the specified byte (the low eight 56 * bits of the argument b). 57 * 58 * @param b the byte to update the checksum with 59 */ 60 public void update(int b) { 61 /* ----- BEGIN android ----- 62 adler = update(adler, b);*/ 63 adler = updateByteImpl(b, adler); 64 // ----- END android ----- 65 } 66 67 /** 68 * Updates the checksum with the specified array of bytes. 69 */ 70 public void update(byte[] b, int off, int len) { 71 if (b == null) { 72 throw new NullPointerException(); 73 } 74 if (off < 0 || len < 0 || off > b.length - len) { 75 throw new ArrayIndexOutOfBoundsException(); 76 } 77 /* ----- BEGIN android ----- 78 adler = updateBytes(adler, b, off, len);*/ 79 adler = updateImpl(b, off, len, adler); 80 // ----- END android ----- 81 } 82 83 /** 84 * Updates the checksum with the specified array of bytes. 85 * 86 * @param b the byte array to update the checksum with 87 */ 88 public void update(byte[] b) { 89 /* ----- BEGIN android ----- 90 adler = updateBytes(adler, b, 0, b.length);*/ 91 update(b, 0, b.length); 92 // ----- END android ----- 93 } 94 95 /** 96 * Updates the checksum with the bytes from the specified buffer. 97 * 98 * The checksum is updated using 99 * buffer.{@link java.nio.Buffer#remaining() remaining()} 100 * bytes starting at 101 * buffer.{@link java.nio.Buffer#position() position()} 102 * Upon return, the buffer's position will be updated to its 103 * limit; its limit will not have been changed. 104 * 105 * @param buffer the ByteBuffer to update the checksum with 106 */ 107 /* ----- BEGIN android ----- 108 private void update(ByteBuffer buffer) { 109 int pos = buffer.position(); 110 int limit = buffer.limit(); 111 assert (pos <= limit); 112 int rem = limit - pos; 113 if (rem <= 0) 114 return; 115 if (buffer instanceof DirectBuffer) { 116 adler = updateByteBuffer(adler, ((DirectBuffer)buffer).address(), pos, rem); 117 } else if (buffer.hasArray()) { 118 adler = updateBytes(adler, buffer.array(), pos + buffer.arrayOffset(), rem); 119 } else { 120 byte[] b = new byte[rem]; 121 buffer.get(b); 122 adler = updateBytes(adler, b, 0, b.length); 123 } 124 buffer.position(limit); 125 } 126 ----- END android ----- */ 127 128 /** 129 * Resets the checksum to initial value. 130 */ 131 public void reset() { 132 adler = 1; 133 } 134 135 /** 136 * Returns the checksum value. 137 */ 138 public long getValue() { 139 /* ----- BEGIN android ----- 140 return (long)adler & 0xffffffffL;*/ 141 return adler; 142 // ----- END android ----- 143 } 144 145 /* ----- BEGIN android ----- 146 // Set up JavaUtilZipAccess in SharedSecrets 147 static { 148 sun.misc.SharedSecrets.setJavaUtilZipAccess(new sun.misc.JavaUtilZipAccess() { 149 public void update(Adler32 adler32, ByteBuffer buf) { 150 adler32.update(buf); 151 } 152 }); 153 } 154 155 private native static int update(int adler, int b); 156 private native static int updateBytes(int adler, byte[] b, int off, 157 int len); 158 private native static int updateByteBuffer(int adler, long addr, 159 int off, int len);*/ 160 private native long updateImpl(byte[] buf, int offset, int byteCount, long adler1); 161 162 private native long updateByteImpl(int val, long adler1); 163 // ----- END android ----- 164 165} 166