From 6123ff144a76bc84b8f9a0b7303daf51a86e4b86 Mon Sep 17 00:00:00 2001 From: Gabriel Einsdorf Date: Fri, 24 Aug 2018 13:06:54 +0200 Subject: [PATCH 1/2] DataHandleService: improve robustness of handle access No longer throws NullPointerException when a handle can not be created. --- .../java/org/scijava/io/handle/DataHandleService.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/scijava/io/handle/DataHandleService.java b/src/main/java/org/scijava/io/handle/DataHandleService.java index f2a24ba6d..0547e4cc8 100644 --- a/src/main/java/org/scijava/io/handle/DataHandleService.java +++ b/src/main/java/org/scijava/io/handle/DataHandleService.java @@ -33,7 +33,6 @@ package org.scijava.io.handle; import java.io.IOException; -import java.util.Objects; import org.scijava.io.IOService; import org.scijava.io.location.Location; @@ -77,7 +76,7 @@ default Class getType() { */ default boolean exists(final Location location) throws IOException { try (DataHandle handle = create(location)) { - return handle.exists(); + return handle == null ? false : handle.exists(); } } @@ -89,8 +88,7 @@ default boolean exists(final Location location) throws IOException { * @see ReadBufferDataHandle#ReadBufferDataHandle(DataHandle) */ default DataHandle readBuffer(final DataHandle handle) { - Objects.nonNull(handle); - return new ReadBufferDataHandle(handle); + return handle == null ? null : new ReadBufferDataHandle(handle); } /** @@ -113,7 +111,6 @@ default DataHandle readBuffer(final Location location) { * @see WriteBufferDataHandle#WriteBufferDataHandle(DataHandle) */ default DataHandle writeBuffer(final DataHandle handle) { - Objects.nonNull(handle); - return new WriteBufferDataHandle(handle); + return handle == null ? null : new WriteBufferDataHandle(handle); } } From db59c302cad50a6b937f97d65501ca4cbb255d25 Mon Sep 17 00:00:00 2001 From: Gabriel Einsdorf Date: Fri, 14 Sep 2018 16:57:18 +0200 Subject: [PATCH 2/2] DataHandleService: document behavior when encountering null --- .../org/scijava/io/handle/DataHandleService.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/scijava/io/handle/DataHandleService.java b/src/main/java/org/scijava/io/handle/DataHandleService.java index 0547e4cc8..8bc55a2b6 100644 --- a/src/main/java/org/scijava/io/handle/DataHandleService.java +++ b/src/main/java/org/scijava/io/handle/DataHandleService.java @@ -71,8 +71,9 @@ default Class getType() { * * @param location the location to test * @return The result of {@link DataHandle#exists()} on a newly created handle - * on this location - * @throws IOException + * on this location. Also returns {@code false} if the handle can not + * be created. + * @throws IOException if the creation of the handle fails exceptionally */ default boolean exists(final Location location) throws IOException { try (DataHandle handle = create(location)) { @@ -85,6 +86,8 @@ default boolean exists(final Location location) throws IOException { * reading. * * @param handle the handle to wrap + * @return The handle wrapped in a read-only buffer, or {@code null} if the + * input handle is {@code null} * @see ReadBufferDataHandle#ReadBufferDataHandle(DataHandle) */ default DataHandle readBuffer(final DataHandle handle) { @@ -95,7 +98,10 @@ default DataHandle readBuffer(final DataHandle handle) { * Creates a {@link DataHandle} on the provided {@link Location} wrapped in a * read-only buffer for accelerated reading. * - * @param location the handle to wrap + * @param location the Location to create a buffered handle on. + * @return A {@link DataHandle} on the provided location wrapped in a + * read-only buffer, or {@code null} if no handle could be created for + * the location. * @see ReadBufferDataHandle#ReadBufferDataHandle(DataHandle) */ default DataHandle readBuffer(final Location location) { @@ -108,6 +114,8 @@ default DataHandle readBuffer(final Location location) { * accelerated writing. * * @param handle the handle to wrap + * @return the handle wrapped in a write-only buffer or {@code null} if the + * provided handle is {@code null} * @see WriteBufferDataHandle#WriteBufferDataHandle(DataHandle) */ default DataHandle writeBuffer(final DataHandle handle) {