-
Notifications
You must be signed in to change notification settings - Fork 913
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try to use Java9 CRC32C when JNI based CRC is not available (#3309)
* Try to use Java9 CRC32C when JNI based CRC is not available * Fixed shading test dependency * Fixed dlog shading deps
- Loading branch information
Showing
11 changed files
with
257 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 0 additions & 92 deletions
92
circe-checksum/src/main/java/com/scurrilous/circe/checksum/Crc32cLongChecksum.java
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
circe-checksum/src/main/java/com/scurrilous/circe/checksum/IntHash.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package com.scurrilous.circe.checksum; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
|
||
public interface IntHash { | ||
int calculate(ByteBuf buffer); | ||
int resume(int current, ByteBuf buffer); | ||
} |
46 changes: 46 additions & 0 deletions
46
circe-checksum/src/main/java/com/scurrilous/circe/checksum/Java8IntHash.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package com.scurrilous.circe.checksum; | ||
|
||
import static com.scurrilous.circe.params.CrcParameters.CRC32C; | ||
import com.scurrilous.circe.IncrementalIntHash; | ||
import com.scurrilous.circe.crc.StandardCrcProvider; | ||
import io.netty.buffer.ByteBuf; | ||
import java.nio.ByteBuffer; | ||
import java.util.zip.Checksum; | ||
|
||
public class Java8IntHash implements IntHash { | ||
|
||
private final IncrementalIntHash hash = new StandardCrcProvider().getIncrementalInt(CRC32C); | ||
|
||
@Override | ||
public int calculate(ByteBuf buffer) { | ||
return resume(0, buffer); | ||
} | ||
|
||
@Override | ||
public int resume(int current, ByteBuf buffer) { | ||
if (buffer.hasArray()) { | ||
return hash.resume(current, buffer.array(), buffer.arrayOffset() + buffer.readerIndex(), | ||
buffer.readableBytes()); | ||
} else { | ||
return hash.resume(current, buffer.nioBuffer()); | ||
} | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
circe-checksum/src/main/java/com/scurrilous/circe/checksum/Java9IntHash.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package com.scurrilous.circe.checksum; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.util.concurrent.FastThreadLocal; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class Java9IntHash implements IntHash { | ||
static final boolean HAS_JAVA9_CRC32C; | ||
private static final Method UPDATE_BYTES; | ||
private static final Method UPDATE_DIRECT_BYTEBUFFER; | ||
|
||
private static final String CRC32C_CLASS_NAME = "java.util.zip.CRC32C"; | ||
|
||
private static final FastThreadLocal<byte[]> TL_BUFFER = new FastThreadLocal<byte[]>() { | ||
@Override | ||
protected byte[] initialValue() { | ||
return new byte[4096]; | ||
} | ||
}; | ||
|
||
static { | ||
boolean hasJava9CRC32C = false; | ||
Method updateBytes = null; | ||
Method updateDirectByteBuffer = null; | ||
|
||
try { | ||
Class<?> c = Class.forName(CRC32C_CLASS_NAME); | ||
updateBytes = c.getDeclaredMethod("updateBytes", int.class, byte[].class, int.class, int.class); | ||
updateBytes.setAccessible(true); | ||
updateDirectByteBuffer = | ||
c.getDeclaredMethod("updateDirectByteBuffer", int.class, long.class, int.class, int.class); | ||
updateDirectByteBuffer.setAccessible(true); | ||
|
||
hasJava9CRC32C = true; | ||
} catch (Exception e) { | ||
log.debug("Unable to use reflected methods: ", e); | ||
updateBytes = null; | ||
updateDirectByteBuffer = null; | ||
} | ||
|
||
HAS_JAVA9_CRC32C = hasJava9CRC32C; | ||
UPDATE_BYTES = updateBytes; | ||
UPDATE_DIRECT_BYTEBUFFER = updateDirectByteBuffer; | ||
} | ||
|
||
@Override | ||
public int calculate(ByteBuf buffer) { | ||
return resume(0, buffer); | ||
} | ||
|
||
private int resume(int current, long address, int offset, int length) { | ||
try { | ||
return (int) UPDATE_DIRECT_BYTEBUFFER.invoke(null, current, address, offset, offset + length); | ||
} catch (IllegalAccessException | InvocationTargetException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private int resume(int current, byte[] array, int offset, int length) { | ||
try { | ||
return (int) UPDATE_BYTES.invoke(null, current, array, offset, offset + length); | ||
} catch (IllegalAccessException | InvocationTargetException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public int resume(int current, ByteBuf buffer) { | ||
int negCrc = ~current; | ||
|
||
if (buffer.hasMemoryAddress()) { | ||
negCrc = resume(negCrc, buffer.memoryAddress(), buffer.readerIndex(), buffer.readableBytes()); | ||
} else if (buffer.hasArray()) { | ||
int offset = buffer.arrayOffset() + buffer.readerIndex(); | ||
negCrc = resume(negCrc, buffer.array(), offset, buffer.readableBytes()); | ||
} else { | ||
byte[] b = TL_BUFFER.get(); | ||
int toRead = buffer.readableBytes(); | ||
while (toRead > 0) { | ||
int length = Math.min(toRead, b.length); | ||
buffer.readBytes(b, 0, length); | ||
negCrc = resume(negCrc, b, 0, length); | ||
toRead -= length; | ||
} | ||
} | ||
|
||
return ~negCrc; | ||
} | ||
} |
Oops, something went wrong.