Skip to content

Comments

Cleanup read and write bytes in test & pom improvements#727

Draft
leerho wants to merge 8 commits intomainfrom
Fix_get_file_bytes
Draft

Cleanup read and write bytes in test & pom improvements#727
leerho wants to merge 8 commits intomainfrom
Fix_get_file_bytes

Conversation

@leerho
Copy link
Member

@leerho leerho commented Feb 18, 2026

This replaces a bunch of methods attempting to read file bytes that were not robust, especially in a Windows OS environment.
This is a far simpler and more robust and modern solution that is only one method.

method.

They were not robust, especially in a Windows OS environment.
@Test
public void testGetFileBytes_NotRegular_NotReadable() throws IOException {
try {
byte[] resultBytes = getFileBytes(resPath, "");

Check notice

Code scanning / CodeQL

Unread local variable Note test

Variable 'byte[] resultBytes' is never read.

Copilot Autofix

AI 4 days ago

To fix the problem, remove the unused local variable while preserving the side effect of calling getFileBytes. This means we should keep the method invocation but drop the assignment to resultBytes.

The best minimal change is within testGetFileBytes_NotRegular_NotReadable in src/test/java/org/apache/datasketches/common/TestUtilTest.java: replace byte[] resultBytes = getFileBytes(resPath, ""); with just getFileBytes(resPath, "");. This does not alter any test logic (the test still triggers the call that may throw a RuntimeException), but eliminates the unread local variable. No new imports, methods, or other definitions are required.

Suggested changeset 1
src/test/java/org/apache/datasketches/common/TestUtilTest.java

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/test/java/org/apache/datasketches/common/TestUtilTest.java b/src/test/java/org/apache/datasketches/common/TestUtilTest.java
--- a/src/test/java/org/apache/datasketches/common/TestUtilTest.java
+++ b/src/test/java/org/apache/datasketches/common/TestUtilTest.java
@@ -57,7 +57,7 @@
   @Test
   public void testGetFileBytes_NotRegular_NotReadable() throws IOException {
     try {
-      byte[] resultBytes = getFileBytes(resPath, "");
+      getFileBytes(resPath, "");
     } catch (RuntimeException e) {
       System.out.println(e.toString());
     }
EOF
@@ -57,7 +57,7 @@
@Test
public void testGetFileBytes_NotRegular_NotReadable() throws IOException {
try {
byte[] resultBytes = getFileBytes(resPath, "");
getFileBytes(resPath, "");
} catch (RuntimeException e) {
System.out.println(e.toString());
}
Copilot is powered by AI and may make mistakes. Always verify output.
Unable to commit as this autofix suggestion is now outdated
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

filePath = basePath.resolve(fileName);
Files.write(filePath, bytes);
} catch (IOException e) {
throw new RuntimeException("System IO Error writing file: " + filePath.toString() + " " + e);

Check warning

Code scanning / CodeQL

Dereferenced variable may be null Warning test

Variable
filePath
may be null at this access because of
this
assignment.

Copilot Autofix

AI about 19 hours ago

In general, to fix this type of issue you must ensure that any variable dereferenced in a catch/error path is definitely non-null on all paths, or guard the dereference with an explicit null check or a safe fallback value. Logging or message construction should not itself depend on state that may not have been initialized due to the very failure being reported.

For this specific case, the best fix without changing functionality is to avoid dereferencing filePath when it may be null. Instead of calling filePath.toString() in the error message, we can build the message from values that are always available: basePath and fileName, both of which are validated as non-null at the top of the method. We can simply replace "System IO Error writing file: " + filePath.toString() + " " + e with "System IO Error writing file: " + basePath.resolve(fileName) + " " + e. This uses basePath.resolve(fileName) again purely for string construction; even if that throws, it would be another RuntimeException being thrown from within the catch, which is acceptable and does not reintroduce a null dereference. Alternatively, we could fall back to a literal that indicates an unknown path, but using basePath and fileName preserves nearly identical functionality.

Only the catch block in putFileBytes (lines 135–137) of src/test/java/org/apache/datasketches/common/TestUtil.java needs to change. No new imports or methods are required.

Suggested changeset 1
src/test/java/org/apache/datasketches/common/TestUtil.java

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/test/java/org/apache/datasketches/common/TestUtil.java b/src/test/java/org/apache/datasketches/common/TestUtil.java
--- a/src/test/java/org/apache/datasketches/common/TestUtil.java
+++ b/src/test/java/org/apache/datasketches/common/TestUtil.java
@@ -133,7 +133,8 @@
       filePath = basePath.resolve(fileName);
       Files.write(filePath, bytes);
     } catch (IOException e) {
-      throw new RuntimeException("System IO Error writing file: " + filePath.toString() + " " + e);
+      throw new RuntimeException(
+          "System IO Error writing file: " + basePath.resolve(fileName) + " " + e);
     }
   }
 }
EOF
@@ -133,7 +133,8 @@
filePath = basePath.resolve(fileName);
Files.write(filePath, bytes);
} catch (IOException e) {
throw new RuntimeException("System IO Error writing file: " + filePath.toString() + " " + e);
throw new RuntimeException(
"System IO Error writing file: " + basePath.resolve(fileName) + " " + e);
}
}
}
Copilot is powered by AI and may make mistakes. Always verify output.
@leerho leerho changed the title This replaces a bunch of methods attempting to read file bytes with one method. Cleanup read and write bytes in test & pom improvements Feb 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant