This is an automated email from the ASF dual-hosted git repository.
exceptionfactory pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi.git
The following commit(s) were added to refs/heads/main by this push:
new 10c6f7388c9 NIFI-16142 Simplified reading long numbers from bytes
using ByteBuffer (#11473)
10c6f7388c9 is described below
commit 10c6f7388c97cd094ea61a7fdc68b245b55e7280
Author: dan-s1 <[email protected]>
AuthorDate: Mon Jul 27 09:53:21 2026 -0400
NIFI-16142 Simplified reading long numbers from bytes using ByteBuffer
(#11473)
- Replaced code which applied bitwise operations to convert an 8-byte array
into a 64-bit long integer using Big-Endian byte ordering with use of Java's
java.nio.ByteBuffer and MethodHandles.byteArrayViewVarHandle
Signed-off-by: David Handermann <[email protected]>
---
.../java/org/apache/nifi/util/FlowFileUnpackagerV2.java | 12 +++---------
.../java/org/apache/nifi/util/FlowFileUnpackagerV3.java | 12 +++---------
.../hadoop/FlowFileStreamUnpackerSequenceFileWriter.java | 12 +++---------
.../apache/nifi/processors/standard/DetectDuplicate.java | 15 ++++++---------
.../org/apache/nifi/provenance/toc/StandardTocReader.java | 15 ++++++---------
5 files changed, 21 insertions(+), 45 deletions(-)
diff --git
a/nifi-commons/nifi-flowfile-packager/src/main/java/org/apache/nifi/util/FlowFileUnpackagerV2.java
b/nifi-commons/nifi-flowfile-packager/src/main/java/org/apache/nifi/util/FlowFileUnpackagerV2.java
index 673ff46f29c..086381df57d 100644
---
a/nifi-commons/nifi-flowfile-packager/src/main/java/org/apache/nifi/util/FlowFileUnpackagerV2.java
+++
b/nifi-commons/nifi-flowfile-packager/src/main/java/org/apache/nifi/util/FlowFileUnpackagerV2.java
@@ -20,6 +20,7 @@ import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
@@ -107,17 +108,10 @@ public class FlowFileUnpackagerV2 implements
FlowFileUnpackager {
return totalBytesRead;
}
- @SuppressWarnings("PMD.UnnecessaryCast")
protected long readLong(final InputStream in) throws IOException {
fillBuffer(in, readBuffer, 8);
- return (((long) readBuffer[0] << 56)
- + ((long) (readBuffer[1] & 255) << 48)
- + ((long) (readBuffer[2] & 255) << 40)
- + ((long) (readBuffer[3] & 255) << 32)
- + ((long) (readBuffer[4] & 255) << 24)
- + ((readBuffer[5] & 255) << 16)
- + ((readBuffer[6] & 255) << 8)
- + ((readBuffer[7] & 255)));
+ final ByteBuffer byteBuffer = ByteBuffer.wrap(readBuffer);
+ return byteBuffer.getLong();
}
private Integer readFieldLength(final InputStream in) throws IOException {
diff --git
a/nifi-commons/nifi-flowfile-packager/src/main/java/org/apache/nifi/util/FlowFileUnpackagerV3.java
b/nifi-commons/nifi-flowfile-packager/src/main/java/org/apache/nifi/util/FlowFileUnpackagerV3.java
index 28561986fef..aa0eb3b9ce3 100644
---
a/nifi-commons/nifi-flowfile-packager/src/main/java/org/apache/nifi/util/FlowFileUnpackagerV3.java
+++
b/nifi-commons/nifi-flowfile-packager/src/main/java/org/apache/nifi/util/FlowFileUnpackagerV3.java
@@ -20,6 +20,7 @@ import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
@@ -124,17 +125,10 @@ public class FlowFileUnpackagerV3 implements
FlowFileUnpackager {
return totalBytesRead;
}
- @SuppressWarnings("PMD.UnnecessaryCast")
protected long readLong(final InputStream in) throws IOException {
fillBuffer(in, readBuffer, 8);
- return (((long) readBuffer[0] << 56)
- + ((long) (readBuffer[1] & 255) << 48)
- + ((long) (readBuffer[2] & 255) << 40)
- + ((long) (readBuffer[3] & 255) << 32)
- + ((long) (readBuffer[4] & 255) << 24)
- + ((readBuffer[5] & 255) << 16)
- + ((readBuffer[6] & 255) << 8)
- + ((readBuffer[7] & 255)));
+ final ByteBuffer byteBuffer = ByteBuffer.wrap(readBuffer);
+ return byteBuffer.getLong();
}
private Integer readFieldLength(final InputStream in) throws IOException {
diff --git
a/nifi-extension-bundles/nifi-hadoop-bundle/nifi-hdfs-processors/src/main/java/org/apache/nifi/processors/hadoop/FlowFileStreamUnpackerSequenceFileWriter.java
b/nifi-extension-bundles/nifi-hadoop-bundle/nifi-hdfs-processors/src/main/java/org/apache/nifi/processors/hadoop/FlowFileStreamUnpackerSequenceFileWriter.java
index 7f599168107..65b48fb7623 100644
---
a/nifi-extension-bundles/nifi-hadoop-bundle/nifi-hdfs-processors/src/main/java/org/apache/nifi/processors/hadoop/FlowFileStreamUnpackerSequenceFileWriter.java
+++
b/nifi-extension-bundles/nifi-hadoop-bundle/nifi-hdfs-processors/src/main/java/org/apache/nifi/processors/hadoop/FlowFileStreamUnpackerSequenceFileWriter.java
@@ -28,6 +28,7 @@ import java.io.BufferedInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
@@ -132,17 +133,10 @@ public class FlowFileStreamUnpackerSequenceFileWriter
extends SequenceFileWriter
}
}
- @SuppressWarnings("PMD.UnnecessaryCast")
protected long readLong(final InputStream in) throws IOException {
fillBuffer(in, readBuffer, 8);
- return (((long) readBuffer[0] << 56)
- + ((long) (readBuffer[1] & 255) << 48)
- + ((long) (readBuffer[2] & 255) << 40)
- + ((long) (readBuffer[3] & 255) << 32)
- + ((long) (readBuffer[4] & 255) << 24)
- + ((readBuffer[5] & 255) << 16)
- + ((readBuffer[6] & 255) << 8)
- + ((readBuffer[7] & 255)));
+ final ByteBuffer byteBuffer = ByteBuffer.wrap(readBuffer);
+ return byteBuffer.getLong();
}
private Integer readFieldLength(final InputStream in) throws
IOException {
diff --git
a/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/DetectDuplicate.java
b/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/DetectDuplicate.java
index 475d136ba34..d816e8a2184 100644
---
a/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/DetectDuplicate.java
+++
b/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/DetectDuplicate.java
@@ -44,6 +44,7 @@ import org.apache.nifi.processor.util.StandardValidators;
import java.io.IOException;
import java.io.OutputStream;
+import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Set;
@@ -260,15 +261,11 @@ public class DetectDuplicate extends AbstractProcessor {
if (input.length == 0) {
return null;
}
- long time = ((long) input[0] << 56)
- + ((long) (input[1] & 255) << 48)
- + ((long) (input[2] & 255) << 40)
- + ((long) (input[3] & 255) << 32)
- + ((long) (input[4] & 255) << 24)
- + ((input[5] & 255) << 16)
- + ((input[6] & 255) << 8)
- + ((input[7] & 255));
- String description = new String(input, 8, input.length - 8,
StandardCharsets.UTF_8);
+
+ final ByteBuffer byteBuffer = ByteBuffer.wrap(input);
+ final long time = byteBuffer.getLong();
+ final String description = new String(input, 8, input.length - 8,
StandardCharsets.UTF_8);
+
return new CacheValue(description, time);
}
}
diff --git
a/nifi-framework-bundle/nifi-framework-extensions/nifi-provenance-repository-bundle/nifi-persistent-provenance-repository/src/main/java/org/apache/nifi/provenance/toc/StandardTocReader.java
b/nifi-framework-bundle/nifi-framework-extensions/nifi-provenance-repository-bundle/nifi-persistent-provenance-repository/src/main/java/org/apache/nifi/provenance/toc/StandardTocReader.java
index 3b3985fc0d8..4c8a5912566 100644
---
a/nifi-framework-bundle/nifi-framework-extensions/nifi-provenance-repository-bundle/nifi-persistent-provenance-repository/src/main/java/org/apache/nifi/provenance/toc/StandardTocReader.java
+++
b/nifi-framework-bundle/nifi-framework-extensions/nifi-provenance-repository-bundle/nifi-persistent-provenance-repository/src/main/java/org/apache/nifi/provenance/toc/StandardTocReader.java
@@ -23,6 +23,9 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.VarHandle;
+import java.nio.ByteOrder;
/**
* Standard implementation of TocReader.
@@ -37,6 +40,8 @@ import java.io.IOException;
* byte (N*8+2)-(N*8+9): long: offset of block N
*/
public class StandardTocReader implements TocReader {
+ private static final VarHandle LONG_VAR_HANDLE =
MethodHandles.byteArrayViewVarHandle(long[].class, ByteOrder.BIG_ENDIAN);
+
private final boolean compressed;
private final long[] offsets;
private final long[] firstEventIds;
@@ -96,16 +101,8 @@ public class StandardTocReader implements TocReader {
}
}
- @SuppressWarnings("PMD.UnnecessaryCast")
private long readLong(final byte[] buffer, final int offset) {
- return ((long) buffer[offset] << 56) +
- ((long) (buffer[offset + 1] & 0xFF) << 48) +
- ((long) (buffer[offset + 2] & 0xFF) << 40) +
- ((long) (buffer[offset + 3] & 0xFF) << 32) +
- ((long) (buffer[offset + 4] & 0xFF) << 24) +
- ((long) (buffer[offset + 5] & 0xFF) << 16) +
- ((long) (buffer[offset + 6] & 0xFF) << 8) +
- (buffer[offset + 7] & 0xFF);
+ return (long) LONG_VAR_HANDLE.get(buffer, offset);
}
@Override