From 76089a346611673acbbdb9f148e40377bca6451f Mon Sep 17 00:00:00 2001 From: Mohanned Anwar <92345336+Muhanned-Anwar@users.noreply.github.com> Date: Sun, 2 Apr 2023 05:34:32 +0300 Subject: [PATCH 1/4] Update NativeUtils.java --- .../com/twitter/ann/faiss/NativeUtils.java | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java b/ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java index 424d28890..c2c46ae50 100644 --- a/ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java +++ b/ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java @@ -19,36 +19,33 @@ public final class NativeUtils { } private static File unpackLibraryFromJarInternal(String path) throws IOException { - if (null == path || !path.startsWith("/")) { - throw new IllegalArgumentException("The path has to be absolute (start with '/')."); + Objects.requireNonNull(path, "The path cannot be null."); + if (!path.startsWith("/")) { + throw new IllegalArgumentException("The path has to be absolute (start with '/')."); } String[] parts = path.split("/"); - String filename = (parts.length > 1) ? parts[parts.length - 1] : null; + String filename = parts.length > 1 ? parts[parts.length - 1] : null; if (filename == null || filename.length() < MIN_PREFIX_LENGTH) { - throw new IllegalArgumentException("The filename has to be at least 3 characters long."); + throw new IllegalArgumentException("The filename has to be at least 3 characters long."); } - if (temporaryDir == null) { - temporaryDir = createTempDirectory(NATIVE_FOLDER_PATH_PREFIX); - temporaryDir.deleteOnExit(); - } - - File temp = new File(temporaryDir, filename); + File temp = Files.createTempFile(NATIVE_FOLDER_PATH_PREFIX, filename).toFile(); + temp.deleteOnExit(); try (InputStream is = NativeUtils.class.getResourceAsStream(path)) { - Files.copy(is, temp.toPath(), StandardCopyOption.REPLACE_EXISTING); + Files.copy(is, temp.toPath(), StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { - temp.delete(); - throw e; + temp.delete(); + throw e; } catch (NullPointerException e) { - temp.delete(); - throw new FileNotFoundException("File " + path + " was not found inside JAR."); + temp.delete(); + throw new FileNotFoundException("File " + path + " was not found inside JAR."); } return temp; - } +} /** * Unpack library from JAR into temporary path From 20dacabd2071bd81dcb797881f497df1708e4615 Mon Sep 17 00:00:00 2001 From: Mohanned Anwar <92345336+Muhanned-Anwar@users.noreply.github.com> Date: Sun, 2 Apr 2023 06:55:40 +0300 Subject: [PATCH 2/4] Update NativeUtils.java --- ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java b/ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java index c2c46ae50..1206af1fb 100644 --- a/ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java +++ b/ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java @@ -6,7 +6,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.StandardCopyOption; -import java.util.Locale; +import java.util.*; public final class NativeUtils { From 907ece2810b73f22055fccde3ca795ff7808b5c1 Mon Sep 17 00:00:00 2001 From: Mohanned Anwar <92345336+Muhanned-Anwar@users.noreply.github.com> Date: Sun, 2 Apr 2023 08:55:14 +0300 Subject: [PATCH 3/4] Update NativeUtils.java --- ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java b/ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java index 1206af1fb..a47456995 100644 --- a/ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java +++ b/ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java @@ -6,7 +6,8 @@ import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.StandardCopyOption; -import java.util.*; +import java.util.Locale; +import java.util.Objects; public final class NativeUtils { From 2e150358ad20e15cab400c084ddd4310b83e5102 Mon Sep 17 00:00:00 2001 From: Mohanned Anwar <92345336+Muhanned-Anwar@users.noreply.github.com> Date: Sun, 2 Apr 2023 08:56:57 +0300 Subject: [PATCH 4/4] Update NativeUtils.java --- ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java b/ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java index a47456995..8de761d01 100644 --- a/ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java +++ b/ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java @@ -26,7 +26,7 @@ public final class NativeUtils { } String[] parts = path.split("/"); - String filename = parts.length > 1 ? parts[parts.length - 1] : null; + String filename = (parts.length > 1) ? parts[parts.length - 1] : null; if (filename == null || filename.length() < MIN_PREFIX_LENGTH) { throw new IllegalArgumentException("The filename has to be at least 3 characters long.");