Skip to content

Commit ca478c1

Browse files
committed
HDDS-2222. Add a method to update ByteBuffer in PureJavaCrc32/PureJavaCrc32C.
1 parent bffcd33 commit ca478c1

File tree

5 files changed

+1336
-0
lines changed

5 files changed

+1336
-0
lines changed

hadoop-hdds/common/dev-support/findbugsExcludeFile.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@
2525
<Class name="org.apache.hadoop.hdds.cli.GenericCli"></Class>
2626
<Bug pattern="DM_EXIT" />
2727
</Match>
28+
<Match>
29+
<Class name="org.apache.hadoop.ozone.common.ChecksumByteBuffer$CrcIntTable" />
30+
<Method name="update" />
31+
<Bug pattern="SF_SWITCH_FALLTHROUGH,SF_SWITCH_NO_DEFAULT" />
32+
</Match>
2833
</FindBugsFilter>
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Some portions of this file Copyright (c) 2004-2006 Intel Corportation
19+
* and licensed under the BSD license.
20+
*/
21+
package org.apache.hadoop.ozone.common;
22+
23+
import org.apache.ratis.util.Preconditions;
24+
25+
import java.nio.ByteBuffer;
26+
import java.util.zip.Checksum;
27+
28+
/**
29+
* A sub-interface of {@link Checksum}
30+
* with a method to update checksum from a {@link ByteBuffer}.
31+
*/
32+
public interface ChecksumByteBuffer extends Checksum {
33+
/**
34+
* Updates the current checksum with the specified bytes in the buffer.
35+
* Upon return, the buffer's position will be equal to its limit.
36+
*
37+
* @param buffer the bytes to update the checksum with
38+
*/
39+
void update(ByteBuffer buffer);
40+
41+
@Override
42+
default void update(byte[] b, int off, int len) {
43+
update(ByteBuffer.wrap(b, off, len).asReadOnlyBuffer());
44+
}
45+
46+
/**
47+
* An abstract class implementing {@link ChecksumByteBuffer}
48+
* with a 32-bit checksum and a lookup table.
49+
*/
50+
abstract class CrcIntTable implements ChecksumByteBuffer {
51+
/** Current CRC value with bit-flipped. */
52+
private int crc;
53+
54+
CrcIntTable() {
55+
reset();
56+
Preconditions.assertTrue(getTable().length == 8 * (1 << 8));
57+
}
58+
59+
abstract int[] getTable();
60+
61+
@Override
62+
public final long getValue() {
63+
return (~crc) & 0xffffffffL;
64+
}
65+
66+
@Override
67+
public final void reset() {
68+
crc = 0xffffffff;
69+
}
70+
71+
@Override
72+
public final void update(int b) {
73+
crc = (crc >>> 8) ^ getTable()[(((crc ^ b) << 24) >>> 24)];
74+
}
75+
76+
@Override
77+
public final void update(ByteBuffer b) {
78+
crc = update(crc, b, getTable());
79+
}
80+
81+
private static int update(int crc, ByteBuffer b, int[] table) {
82+
for(; b.remaining() > 7;) {
83+
final int c0 = (b.get() ^ crc) & 0xff;
84+
final int c1 = (b.get() ^ (crc >>>= 8)) & 0xff;
85+
final int c2 = (b.get() ^ (crc >>>= 8)) & 0xff;
86+
final int c3 = (b.get() ^ (crc >>> 8)) & 0xff;
87+
crc = (table[0x700 + c0] ^ table[0x600 + c1])
88+
^ (table[0x500 + c2] ^ table[0x400 + c3]);
89+
90+
final int c4 = b.get() & 0xff;
91+
final int c5 = b.get() & 0xff;
92+
final int c6 = b.get() & 0xff;
93+
final int c7 = b.get() & 0xff;
94+
95+
crc ^= (table[0x300 + c4] ^ table[0x200 + c5])
96+
^ (table[0x100 + c6] ^ table[c7]);
97+
}
98+
99+
// loop unroll - duff's device style
100+
switch (b.remaining()) {
101+
case 7: crc = (crc >>> 8) ^ table[((crc ^ b.get()) & 0xff)];
102+
case 6: crc = (crc >>> 8) ^ table[((crc ^ b.get()) & 0xff)];
103+
case 5: crc = (crc >>> 8) ^ table[((crc ^ b.get()) & 0xff)];
104+
case 4: crc = (crc >>> 8) ^ table[((crc ^ b.get()) & 0xff)];
105+
case 3: crc = (crc >>> 8) ^ table[((crc ^ b.get()) & 0xff)];
106+
case 2: crc = (crc >>> 8) ^ table[((crc ^ b.get()) & 0xff)];
107+
case 1: crc = (crc >>> 8) ^ table[((crc ^ b.get()) & 0xff)];
108+
default: // noop
109+
}
110+
111+
return crc;
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)