Skip to content

Commit b46eeb0

Browse files
authored
Merge pull request #336 from scijava/datahandle-service-nullpointer
DataHandleService: better handling of failure states
2 parents 5098b2c + db59c30 commit b46eeb0

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/main/java/org/scijava/io/handle/DataHandleService.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
package org.scijava.io.handle;
3434

3535
import java.io.IOException;
36-
import java.util.Objects;
3736

3837
import org.scijava.io.IOService;
3938
import org.scijava.io.location.Location;
@@ -72,12 +71,13 @@ default Class<Location> getType() {
7271
*
7372
* @param location the location to test
7473
* @return The result of {@link DataHandle#exists()} on a newly created handle
75-
* on this location
76-
* @throws IOException
74+
* on this location. Also returns {@code false} if the handle can not
75+
* be created.
76+
* @throws IOException if the creation of the handle fails exceptionally
7777
*/
7878
default boolean exists(final Location location) throws IOException {
7979
try (DataHandle<Location> handle = create(location)) {
80-
return handle.exists();
80+
return handle == null ? false : handle.exists();
8181
}
8282
}
8383

@@ -86,18 +86,22 @@ default boolean exists(final Location location) throws IOException {
8686
* reading.
8787
*
8888
* @param handle the handle to wrap
89+
* @return The handle wrapped in a read-only buffer, or {@code null} if the
90+
* input handle is {@code null}
8991
* @see ReadBufferDataHandle#ReadBufferDataHandle(DataHandle)
9092
*/
9193
default DataHandle<Location> readBuffer(final DataHandle<Location> handle) {
92-
Objects.nonNull(handle);
93-
return new ReadBufferDataHandle(handle);
94+
return handle == null ? null : new ReadBufferDataHandle(handle);
9495
}
9596

9697
/**
9798
* Creates a {@link DataHandle} on the provided {@link Location} wrapped in a
9899
* read-only buffer for accelerated reading.
99100
*
100-
* @param location the handle to wrap
101+
* @param location the Location to create a buffered handle on.
102+
* @return A {@link DataHandle} on the provided location wrapped in a
103+
* read-only buffer, or {@code null} if no handle could be created for
104+
* the location.
101105
* @see ReadBufferDataHandle#ReadBufferDataHandle(DataHandle)
102106
*/
103107
default DataHandle<Location> readBuffer(final Location location) {
@@ -110,10 +114,11 @@ default DataHandle<Location> readBuffer(final Location location) {
110114
* accelerated writing.
111115
*
112116
* @param handle the handle to wrap
117+
* @return the handle wrapped in a write-only buffer or {@code null} if the
118+
* provided handle is {@code null}
113119
* @see WriteBufferDataHandle#WriteBufferDataHandle(DataHandle)
114120
*/
115121
default DataHandle<Location> writeBuffer(final DataHandle<Location> handle) {
116-
Objects.nonNull(handle);
117-
return new WriteBufferDataHandle(handle);
122+
return handle == null ? null : new WriteBufferDataHandle(handle);
118123
}
119124
}

0 commit comments

Comments
 (0)