Skip to content

Commit a23b1a0

Browse files
committed
Fixed creation of temporary dir in NativeUtils (#4262)
### Motivation Creating the temp directory for unpacking the native library is failing for the affinity library. ### Changes Use `Files.createTempDirectory()` instead.
1 parent 67a5819 commit a23b1a0

File tree

2 files changed

+22
-16
lines changed
  • circe-checksum/src/main/java/com/scurrilous/circe/utils
  • cpu-affinity/src/main/java/org/apache/bookkeeper/common/util/affinity/impl

2 files changed

+22
-16
lines changed

Diff for: circe-checksum/src/main/java/com/scurrilous/circe/utils/NativeUtils.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import java.io.IOException;
2727
import java.io.InputStream;
2828
import java.io.OutputStream;
29+
import java.nio.file.Files;
30+
import java.nio.file.Path;
2931
import java.util.Locale;
3032

3133
/**
@@ -50,13 +52,9 @@ public static void loadLibraryFromJar(String path) throws Exception {
5052
String[] parts = path.split("/");
5153
String filename = (parts.length > 0) ? parts[parts.length - 1] : null;
5254

53-
File dir = File.createTempFile("native", "");
54-
dir.delete();
55-
if (!(dir.mkdir())) {
56-
throw new IOException("Failed to create temp directory " + dir.getAbsolutePath());
57-
}
58-
dir.deleteOnExit();
59-
File temp = new File(dir, filename);
55+
Path dir = Files.createTempDirectory("native");
56+
dir.toFile().deleteOnExit();
57+
File temp = new File(dir.toString(), filename);
6058
temp.deleteOnExit();
6159

6260
byte[] buffer = new byte[1024];

Diff for: cpu-affinity/src/main/java/org/apache/bookkeeper/common/util/affinity/impl/NativeUtils.java

+17-9
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
import java.io.File;
2525
import java.io.FileNotFoundException;
2626
import java.io.FileOutputStream;
27-
import java.io.IOException;
2827
import java.io.InputStream;
2928
import java.io.OutputStream;
29+
import java.nio.file.Files;
30+
import java.nio.file.Path;
31+
import lombok.NonNull;
3032
import lombok.experimental.UtilityClass;
3133

3234
/**
@@ -46,17 +48,17 @@ public class NativeUtils {
4648
value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE",
4749
justification = "work around for java 9: https://github.com/spotbugs/spotbugs/issues/493")
4850
public static void loadLibraryFromJar(String path) throws Exception {
49-
com.google.common.base.Preconditions.checkArgument(path.startsWith("/"), "absolute path must start with /");
51+
checkArgument(path.startsWith("/"), "absolute path must start with /");
5052

5153
String[] parts = path.split("/");
52-
String filename = (parts.length > 0) ? parts[parts.length - 1] : null;
54+
checkArgument(parts.length > 0, "absolute path must contain file name");
5355

54-
File dir = File.createTempFile("native", "");
55-
if (!(dir.mkdir())) {
56-
throw new IOException("Failed to create temp directory " + dir.getAbsolutePath());
57-
}
58-
dir.deleteOnExit();
59-
File temp = new File(dir, filename);
56+
String filename = parts[parts.length - 1];
57+
checkArgument(path.startsWith("/"), "absolute path must start with /");
58+
59+
Path dir = Files.createTempDirectory("native");
60+
dir.toFile().deleteOnExit();
61+
File temp = new File(dir.toString(), filename);
6062
temp.deleteOnExit();
6163

6264
byte[] buffer = new byte[1024];
@@ -79,4 +81,10 @@ public static void loadLibraryFromJar(String path) throws Exception {
7981

8082
System.load(temp.getAbsolutePath());
8183
}
84+
85+
private static void checkArgument(boolean expression, @NonNull Object errorMessage) {
86+
if (!expression) {
87+
throw new IllegalArgumentException(String.valueOf(errorMessage));
88+
}
89+
}
8290
}

0 commit comments

Comments
 (0)