author | mchung |
Fri, 22 May 2015 16:43:39 -0700 | |
changeset 30789 | 9eca83469588 |
parent 27737 | 0a3fb0529425 |
child 32637 | ac873eaac862 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
27737 | 2 |
* Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.util.zip; |
|
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 | 31 |
/** |
32 |
* A class that can be used to compute the Adler-32 checksum of a data |
|
33 |
* stream. An Adler-32 checksum is almost as reliable as a CRC-32 but |
|
34 |
* can be computed much faster. |
|
35 |
* |
|
11113
f7248a4db8db
7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents:
5506
diff
changeset
|
36 |
* <p> Passing a {@code null} argument to a method in this class will cause |
27737 | 37 |
* 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
|
38 |
* |
2 | 39 |
* @author David Connelly |
40 |
*/ |
|
41 |
public |
|
42 |
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
|
43 |
|
2 | 44 |
private int adler = 1; |
45 |
||
46 |
/** |
|
47 |
* Creates a new Adler32 object. |
|
48 |
*/ |
|
49 |
public Adler32() { |
|
50 |
} |
|
51 |
||
52 |
/** |
|
3078 | 53 |
* Updates the checksum with the specified byte (the low eight |
54 |
* bits of the argument b). |
|
2 | 55 |
*/ |
27737 | 56 |
@Override |
2 | 57 |
public void update(int b) { |
58 |
adler = update(adler, b); |
|
59 |
} |
|
60 |
||
61 |
/** |
|
3078 | 62 |
* 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
|
63 |
* |
27737 | 64 |
* @throws ArrayIndexOutOfBoundsException |
65 |
* if {@code off} is negative, or {@code len} is negative, or |
|
66 |
* {@code off+len} is negative or greater than the length of |
|
67 |
* the array {@code b}. |
|
2 | 68 |
*/ |
27737 | 69 |
@Override |
2 | 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 |
adler = updateBytes(adler, b, off, len); |
|
78 |
} |
|
79 |
||
80 |
/** |
|
11113
f7248a4db8db
7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents:
5506
diff
changeset
|
81 |
* 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
|
82 |
* |
27737 | 83 |
* The checksum is updated with the remaining bytes in the buffer, starting |
84 |
* at the buffer's position. Upon return, the buffer's position will be |
|
85 |
* 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
|
86 |
* |
f7248a4db8db
7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents:
5506
diff
changeset
|
87 |
* @since 1.8 |
f7248a4db8db
7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents:
5506
diff
changeset
|
88 |
*/ |
27737 | 89 |
@Override |
11113
f7248a4db8db
7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents:
5506
diff
changeset
|
90 |
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
|
91 |
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
|
92 |
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
|
93 |
assert (pos <= limit); |
f7248a4db8db
7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents:
5506
diff
changeset
|
94 |
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
|
95 |
if (rem <= 0) |
f7248a4db8db
7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents:
5506
diff
changeset
|
96 |
return; |
f7248a4db8db
7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents:
5506
diff
changeset
|
97 |
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
|
98 |
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
|
99 |
} 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
|
100 |
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
|
101 |
} else { |
27737 | 102 |
byte[] b = new byte[Math.min(buffer.remaining(), 4096)]; |
103 |
while (buffer.hasRemaining()) { |
|
104 |
int length = Math.min(buffer.remaining(), b.length); |
|
105 |
buffer.get(b, 0, length); |
|
106 |
update(b, 0, length); |
|
107 |
} |
|
11113
f7248a4db8db
7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents:
5506
diff
changeset
|
108 |
} |
f7248a4db8db
7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents:
5506
diff
changeset
|
109 |
buffer.position(limit); |
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 |
|
2 | 112 |
/** |
3078 | 113 |
* Resets the checksum to initial value. |
2 | 114 |
*/ |
27737 | 115 |
@Override |
2 | 116 |
public void reset() { |
117 |
adler = 1; |
|
118 |
} |
|
119 |
||
120 |
/** |
|
3078 | 121 |
* Returns the checksum value. |
2 | 122 |
*/ |
27737 | 123 |
@Override |
2 | 124 |
public long getValue() { |
125 |
return (long)adler & 0xffffffffL; |
|
126 |
} |
|
127 |
||
128 |
private native static int update(int adler, int b); |
|
129 |
private native static int updateBytes(int adler, byte[] b, int off, |
|
130 |
int len); |
|
11113
f7248a4db8db
7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents:
5506
diff
changeset
|
131 |
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
|
132 |
int off, int len); |
2 | 133 |
} |