Skip to content

This replaces a bunch of methods attempting to read file bytes with one method.#727

Draft
leerho wants to merge 3 commits intomainfrom
Fix_get_file_bytes
Draft

This replaces a bunch of methods attempting to read file bytes with one method.#727
leerho wants to merge 3 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.

Check notice

Code scanning / CodeQL

Unread local variable Note test

Variable 'byte[] resultBytes' is never read.

Copilot Autofix

AI 1 day 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 12 hours ago

To fix the problem, we must ensure that the catch block does not dereference filePath when it might still be null. The behavior we want to preserve is: throw a RuntimeException that includes as much path information as is safely available, but never trigger a NullPointerException in the error handling itself.

The best minimal change is to avoid preinitializing filePath to null, move its declaration down so that it is definitely assigned before use when possible, and in the catch block handle the case where filePath was never set. Concretely:

  • Replace Path filePath = null; with Path filePath; and move it inside the try block, immediately before the resolve call, so that the variable is only in scope within the try/catch where it’s needed.
  • In the catch block, construct the error message defensively: if filePath is non-null, use filePath.toString(), otherwise fall back to a generic "unknown" or something based on basePath and fileName (both are guaranteed non-null by Objects.requireNonNull).

Because filePath is used only in this method and no external contracts depend on its exact error message format, this change preserves functionality while removing the null-dereference risk. No new imports or helper 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
@@ -127,13 +127,13 @@
     Objects.requireNonNull(basePath, "input parameter 'Path basePath' cannot be null.");
     Objects.requireNonNull(fileName, "input parameter 'String fileName' cannot be null.");
     Objects.requireNonNull(bytes, "input parameter 'byte[] bytes' cannot be null.");
-    Path filePath = null;
     try {
       Files.createDirectories(basePath); //create the directory if it doesn't exist.
-      filePath = basePath.resolve(fileName);
+      final Path filePath = basePath.resolve(fileName);
       Files.write(filePath, bytes);
     } catch (IOException e) {
-      throw new RuntimeException("System IO Error writing file: " + filePath.toString() + " " + e);
+      final String baseInfo = "basePath=" + basePath + ", fileName=" + fileName;
+      throw new RuntimeException("System IO Error writing file (" + baseInfo + "): " + e);
     }
   }
 }
EOF
@@ -127,13 +127,13 @@
Objects.requireNonNull(basePath, "input parameter 'Path basePath' cannot be null.");
Objects.requireNonNull(fileName, "input parameter 'String fileName' cannot be null.");
Objects.requireNonNull(bytes, "input parameter 'byte[] bytes' cannot be null.");
Path filePath = null;
try {
Files.createDirectories(basePath); //create the directory if it doesn't exist.
filePath = basePath.resolve(fileName);
final Path filePath = basePath.resolve(fileName);
Files.write(filePath, bytes);
} catch (IOException e) {
throw new RuntimeException("System IO Error writing file: " + filePath.toString() + " " + e);
final String baseInfo = "basePath=" + basePath + ", fileName=" + fileName;
throw new RuntimeException("System IO Error writing file (" + baseInfo + "): " + e);
}
}
}
Copilot is powered by AI and may make mistakes. Always verify output.
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

Comments