From 0df138171c380683db9a6166ba1e1767d7d84a8e Mon Sep 17 00:00:00 2001 From: Mohanned Anwar <92345336+Muhanned-Anwar@users.noreply.github.com> Date: Sun, 2 Apr 2023 05:41:34 +0300 Subject: [PATCH 1/3] Update NativeUtils.java Refactor function loadLibraryFromJar --- .../com/twitter/ann/faiss/NativeUtils.java | 34 ++++++++++--------- 1 file changed, 18 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..56cf79b1f 100644 --- a/ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java +++ b/ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java @@ -90,24 +90,26 @@ public final class NativeUtils { * @throws FileNotFoundException If the file could not be found inside the * JAR. */ - public static void loadLibraryFromJar(String path) throws IOException { - File temp = unpackLibraryFromJarInternal(path); - - try (InputStream is = NativeUtils.class.getResourceAsStream(path)) { - Files.copy(is, temp.toPath(), StandardCopyOption.REPLACE_EXISTING); - } catch (IOException e) { - temp.delete(); - throw e; - } catch (NullPointerException e) { - temp.delete(); - throw new FileNotFoundException("File " + path + " was not found inside JAR."); + public static void loadLibraryFromJar(String path) throws IOException { + File tempFile = unpackLibraryFromJarInternal(path); + try { + copyResourceToFile(path, tempFile); + System.load(tempFile.getAbsolutePath()); + } finally { + tempFile.deleteOnExit(); + } } - try { - System.load(temp.getAbsolutePath()); - } finally { - temp.deleteOnExit(); - } + private static void copyResourceToFile(String resourcePath, File file) throws IOException { + try ( InputStream inputStream = NativeUtils.class.getResourceAsStream(resourcePath)) { + Files.copy(inputStream, file.toPath(), StandardCopyOption.REPLACE_EXISTING); + } catch (IOException e) { + file.delete(); + throw e; + } catch (NullPointerException e) { + file.delete(); + throw new FileNotFoundException("File " + resourcePath + " was not found inside JAR."); + } } private static File createTempDirectory(String prefix) throws IOException { From c51fe59e2f1fb66cc18acf829bd50d11cddb2524 Mon Sep 17 00:00:00 2001 From: Mohanned Anwar <92345336+Muhanned-Anwar@users.noreply.github.com> Date: Wed, 5 Apr 2023 10:13:47 +0300 Subject: [PATCH 2/3] Update NativeUtils.java Catch the rest of the exceptions --- ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java | 3 +++ 1 file changed, 3 insertions(+) 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 56cf79b1f..1cc0af0b0 100644 --- a/ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java +++ b/ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java @@ -45,6 +45,9 @@ public final class NativeUtils { } catch (NullPointerException e) { temp.delete(); throw new FileNotFoundException("File " + path + " was not found inside JAR."); + } catch (Exception e) { + file.delete(); + throw e; } return temp; From 9eb5089eae60d04414124f97e598808f63f4e2ed Mon Sep 17 00:00:00 2001 From: Mohanned Anwar <92345336+Muhanned-Anwar@users.noreply.github.com> Date: Wed, 5 Apr 2023 10:16:48 +0300 Subject: [PATCH 3/3] Update NativeUtils.java move function --- 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 1cc0af0b0..45c8faa8e 100644 --- a/ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java +++ b/ann/src/main/java/com/twitter/ann/faiss/NativeUtils.java @@ -95,8 +95,8 @@ public final class NativeUtils { */ public static void loadLibraryFromJar(String path) throws IOException { File tempFile = unpackLibraryFromJarInternal(path); + copyResourceToFile(path, tempFile); try { - copyResourceToFile(path, tempFile); System.load(tempFile.getAbsolutePath()); } finally { tempFile.deleteOnExit();