From c9e867b2e8f4ef5d4cb7ac84b52a9ef40edfb53a Mon Sep 17 00:00:00 2001 From: raver119 Date: Wed, 17 Jul 2019 14:53:24 +0300 Subject: [PATCH] File existence validation for Nd4j.createFromNpyFile() Signed-off-by: raver119 --- .../src/main/java/org/nd4j/linalg/factory/Nd4j.java | 3 +++ .../test/java/org/nd4j/linalg/serde/NumpyFormatTests.java | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/factory/Nd4j.java b/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/factory/Nd4j.java index 73d70086c..eda5c7319 100644 --- a/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/factory/Nd4j.java +++ b/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/factory/Nd4j.java @@ -6916,6 +6916,9 @@ public class Nd4j { * @return the created ndarray */ public static INDArray createFromNpyFile(File file) { + if (!file.exists()) + throw new IllegalArgumentException("File [" + file.getAbsolutePath() + "] doesn't exist"); + return INSTANCE.createFromNpyFile(file); } diff --git a/nd4j/nd4j-backends/nd4j-tests/src/test/java/org/nd4j/linalg/serde/NumpyFormatTests.java b/nd4j/nd4j-backends/nd4j-tests/src/test/java/org/nd4j/linalg/serde/NumpyFormatTests.java index 06a0b62fb..217f9780f 100644 --- a/nd4j/nd4j-backends/nd4j-tests/src/test/java/org/nd4j/linalg/serde/NumpyFormatTests.java +++ b/nd4j/nd4j-backends/nd4j-tests/src/test/java/org/nd4j/linalg/serde/NumpyFormatTests.java @@ -304,6 +304,12 @@ public class NumpyFormatTests extends BaseNd4jTest { boolean eq = exp.equals(probablyShouldntLoad); //And is actually equal content } + @Test(expected = IllegalArgumentException.class) + public void testAbsentNumpyFile_1() throws Exception { + val f = new File("pew-pew-zomg.some_extension_that_wont_exist"); + INDArray act1 = Nd4j.createFromNpyFile(f); + } + @Override public char ordering() { return 'c';