author | darcy |
Thu, 04 Dec 2014 12:59:43 -0800 | |
changeset 27804 | 4659e70271c4 |
parent 27737 | 0a3fb0529425 |
child 31671 | 362e0c0acece |
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 CRC-32 of a data stream. |
|
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 |
27737 | 35 |
* 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
|
36 |
* |
2 | 37 |
* @author David Connelly |
38 |
*/ |
|
39 |
public |
|
40 |
class CRC32 implements Checksum { |
|
41 |
private int crc; |
|
42 |
||
43 |
/** |
|
44 |
* Creates a new CRC32 object. |
|
45 |
*/ |
|
46 |
public CRC32() { |
|
47 |
} |
|
48 |
||
49 |
||
50 |
/** |
|
3078 | 51 |
* Updates the CRC-32 checksum with the specified byte (the low |
52 |
* eight bits of the argument b). |
|
2 | 53 |
*/ |
27737 | 54 |
@Override |
2 | 55 |
public void update(int b) { |
56 |
crc = update(crc, b); |
|
57 |
} |
|
58 |
||
59 |
/** |
|
3078 | 60 |
* 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
|
61 |
* |
27737 | 62 |
* @throws ArrayIndexOutOfBoundsException |
63 |
* if {@code off} is negative, or {@code len} is negative, or |
|
64 |
* {@code off+len} is negative or greater than the length of |
|
65 |
* the array {@code b}. |
|
2 | 66 |
*/ |
27737 | 67 |
@Override |
2 | 68 |
public void update(byte[] b, int off, int len) { |
69 |
if (b == null) { |
|
70 |
throw new NullPointerException(); |
|
71 |
} |
|
72 |
if (off < 0 || len < 0 || off > b.length - len) { |
|
73 |
throw new ArrayIndexOutOfBoundsException(); |
|
74 |
} |
|
75 |
crc = updateBytes(crc, b, off, len); |
|
76 |
} |
|
77 |
||
78 |
/** |
|
27737 | 79 |
* Updates the CRC-32 checksum with the bytes from the specified buffer. |
2 | 80 |
* |
27737 | 81 |
* The checksum is updated with the remaining bytes in the buffer, starting |
82 |
* at the buffer's position. Upon return, the buffer's position will be |
|
83 |
* 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
|
84 |
* |
f7248a4db8db
7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents:
5506
diff
changeset
|
85 |
* @since 1.8 |
f7248a4db8db
7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents:
5506
diff
changeset
|
86 |
*/ |
27737 | 87 |
@Override |
11113
f7248a4db8db
7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents:
5506
diff
changeset
|
88 |
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
|
89 |
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
|
90 |
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
|
91 |
assert (pos <= limit); |
f7248a4db8db
7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents:
5506
diff
changeset
|
92 |
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
|
93 |
if (rem <= 0) |
f7248a4db8db
7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents:
5506
diff
changeset
|
94 |
return; |
f7248a4db8db
7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents:
5506
diff
changeset
|
95 |
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
|
96 |
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
|
97 |
} 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
|
98 |
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
|
99 |
} else { |
27737 | 100 |
byte[] b = new byte[Math.min(buffer.remaining(), 4096)]; |
101 |
while (buffer.hasRemaining()) { |
|
102 |
int length = Math.min(buffer.remaining(), b.length); |
|
103 |
buffer.get(b, 0, length); |
|
104 |
update(b, 0, length); |
|
105 |
} |
|
11113
f7248a4db8db
7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents:
5506
diff
changeset
|
106 |
} |
f7248a4db8db
7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents:
5506
diff
changeset
|
107 |
buffer.position(limit); |
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 |
|
f7248a4db8db
7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents:
5506
diff
changeset
|
110 |
/** |
2 | 111 |
* Resets CRC-32 to initial value. |
112 |
*/ |
|
27737 | 113 |
@Override |
2 | 114 |
public void reset() { |
115 |
crc = 0; |
|
116 |
} |
|
117 |
||
118 |
/** |
|
119 |
* Returns CRC-32 value. |
|
120 |
*/ |
|
27737 | 121 |
@Override |
2 | 122 |
public long getValue() { |
123 |
return (long)crc & 0xffffffffL; |
|
124 |
} |
|
125 |
||
126 |
private native static int update(int crc, int b); |
|
127 |
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
|
128 |
|
f7248a4db8db
7109837: Provide a mechanism for computing an Adler32 checksum for the contents of a ByteBuffer
sherman
parents:
5506
diff
changeset
|
129 |
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
|
130 |
int off, int len); |
2 | 131 |
} |