1 /*
2 * Copyright (c) 1996, 2013, 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
37 * @see Checksum
38 * @author David Connelly
39 */
40 public
41 class CRC32 implements Checksum {
42 private int crc;
43
44 /**
45 * Creates a new CRC32 object.
46 */
47 public CRC32() {
48 }
49
50
51 /**
52 * Updates the CRC-32 checksum with the specified byte (the low
53 * eight bits of the argument b).
54 *
55 * @param b the byte to update the checksum with
56 */
57 public void update(int b) {
58 crc = update(crc, b);
59 }
60
61 /**
62 * Updates the CRC-32 checksum with the specified array of bytes.
63 *
64 * @throws ArrayIndexOutOfBoundsException
65 * if {@code off} is negative, or {@code len} is negative,
66 * or {@code off+len} is greater than the length of the
67 * array {@code b}
68 */
69 public void update(byte[] b, int off, int len) {
70 if (b == null) {
71 throw new NullPointerException();
72 }
73 if (off < 0 || len < 0 || off > b.length - len) {
74 throw new ArrayIndexOutOfBoundsException();
75 }
76 crc = updateBytes(crc, b, off, len);
77 }
78
79 /**
80 * Updates the CRC-32 checksum with the specified array of bytes.
81 *
82 * @param b the array of bytes to update the checksum with
83 */
84 public void update(byte[] b) {
85 crc = updateBytes(crc, b, 0, b.length);
86 }
87
88 /**
89 * Updates the checksum with the bytes from the specified buffer.
90 *
91 * The checksum is updated using
92 * buffer.{@link java.nio.Buffer#remaining() remaining()}
93 * bytes starting at
94 * buffer.{@link java.nio.Buffer#position() position()}
95 * Upon return, the buffer's position will
96 * be updated to its limit; its limit will not have been changed.
97 *
98 * @param buffer the ByteBuffer to update the checksum with
99 * @since 1.8
100 */
101 public void update(ByteBuffer buffer) {
102 int pos = buffer.position();
103 int limit = buffer.limit();
104 assert (pos <= limit);
105 int rem = limit - pos;
106 if (rem <= 0)
107 return;
108 if (buffer instanceof DirectBuffer) {
109 crc = updateByteBuffer(crc, ((DirectBuffer)buffer).address(), pos, rem);
110 } else if (buffer.hasArray()) {
111 crc = updateBytes(crc, buffer.array(), pos + buffer.arrayOffset(), rem);
112 } else {
113 byte[] b = new byte[rem];
114 buffer.get(b);
115 crc = updateBytes(crc, b, 0, b.length);
116 }
117 buffer.position(limit);
118 }
119
120 /**
121 * Resets CRC-32 to initial value.
122 */
123 public void reset() {
124 crc = 0;
125 }
126
127 /**
128 * Returns CRC-32 value.
129 */
130 public long getValue() {
131 return (long)crc & 0xffffffffL;
132 }
133
134 private native static int update(int crc, int b);
135 private native static int updateBytes(int crc, byte[] b, int off, int len);
136
137 private native static int updateByteBuffer(int adler, long addr,
138 int off, int len);
139 }
|
1 /*
2 * Copyright (c) 1996, 2014, 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
37 * @see Checksum
38 * @author David Connelly
39 */
40 public
41 class CRC32 implements Checksum {
42 private int crc;
43
44 /**
45 * Creates a new CRC32 object.
46 */
47 public CRC32() {
48 }
49
50
51 /**
52 * Updates the CRC-32 checksum with the specified byte (the low
53 * eight bits of the argument b).
54 *
55 * @param b the byte to update the checksum with
56 */
57 @Override
58 public void update(int b) {
59 crc = update(crc, b);
60 }
61
62 /**
63 * Updates the CRC-32 checksum with the specified array of bytes.
64 *
65 * @throws ArrayIndexOutOfBoundsException
66 * if {@code off} is negative, or {@code len} is negative,
67 * or {@code off+len} is greater than the length of the
68 * array {@code b}
69 */
70 @Override
71 public void update(byte[] b, int off, int len) {
72 if (b == null) {
73 throw new NullPointerException();
74 }
75 if (off < 0 || len < 0 || off > b.length - len) {
76 throw new ArrayIndexOutOfBoundsException();
77 }
78 crc = updateBytes(crc, b, off, len);
79 }
80
81 /**
82 * Updates the CRC-32 checksum with the bytes from the specified buffer.
83 *
84 * The checksum is updated using
85 * buffer.{@link java.nio.Buffer#remaining() remaining()}
86 * bytes starting at
87 * buffer.{@link java.nio.Buffer#position() position()}
88 * Upon return, the buffer's position will
89 * be updated to its limit; its limit will not have been changed.
90 *
91 * @param buffer the ByteBuffer to update the checksum with
92 * @since 1.8
93 */
94 @Override
95 public void update(ByteBuffer buffer) {
96 int pos = buffer.position();
97 int limit = buffer.limit();
98 assert (pos <= limit);
99 int rem = limit - pos;
100 if (rem <= 0)
101 return;
102 if (buffer instanceof DirectBuffer) {
103 crc = updateByteBuffer(crc, ((DirectBuffer)buffer).address(), pos, rem);
104 } else if (buffer.hasArray()) {
105 crc = updateBytes(crc, buffer.array(), pos + buffer.arrayOffset(), rem);
106 } else {
107 byte[] b = new byte[rem];
108 buffer.get(b);
109 crc = updateBytes(crc, b, 0, b.length);
110 }
111 buffer.position(limit);
112 }
113
114 /**
115 * Resets CRC-32 to initial value.
116 */
117 @Override
118 public void reset() {
119 crc = 0;
120 }
121
122 /**
123 * Returns CRC-32 value.
124 */
125 @Override
126 public long getValue() {
127 return (long)crc & 0xffffffffL;
128 }
129
130 private native static int update(int crc, int b);
131 private native static int updateBytes(int crc, byte[] b, int off, int len);
132
133 private native static int updateByteBuffer(int adler, long addr,
134 int off, int len);
135 }
|