From 140b423f70232cbd0ff0686a3b8fa6556fdf5c31 Mon Sep 17 00:00:00 2001 From: John Blum Date: Wed, 17 Jun 2020 15:03:16 -0700 Subject: [PATCH] Add nullSafeLength(:File) method to safely invoke file.length() protexting against a null File reference. --- .../springframework/data/gemfire/tests/util/FileUtils.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/FileUtils.java b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/FileUtils.java index 378b6b5..f3f721d 100644 --- a/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/FileUtils.java +++ b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/FileUtils.java @@ -13,7 +13,6 @@ * or implied. See the License for the specific language governing * permissions and limitations under the License. */ - package org.springframework.data.gemfire.tests.util; import java.io.BufferedReader; @@ -59,7 +58,10 @@ public abstract class FileUtils extends IOUtils { return new File(parent, pathname); } - @SuppressWarnings("all") + public static long nullSafeLength(File path) { + return path != null ? path.length() : 0L; + } + public static String read(File file) throws IOException { Assert.isTrue(isFile(file), String.format("The File [%s] to read the contents from is not a valid file", file)); @@ -82,7 +84,6 @@ public abstract class FileUtils extends IOUtils { } } - /* (non-Javadoc) */ public static void write(File file, String contents) throws IOException { Assert.notNull(file, "File is required");