From 6a287efc0cdb9138aa54f0efb339ab746ba98ec0 Mon Sep 17 00:00:00 2001 From: Gabriel Einsdorf Date: Sun, 6 May 2018 15:12:56 +0200 Subject: [PATCH] DataHandleService: Add a method to check if the source of a handle exists. --- .../org/scijava/io/handle/DataHandleService.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main/java/org/scijava/io/handle/DataHandleService.java b/src/main/java/org/scijava/io/handle/DataHandleService.java index 97175e4d9..5d4f1a77e 100644 --- a/src/main/java/org/scijava/io/handle/DataHandleService.java +++ b/src/main/java/org/scijava/io/handle/DataHandleService.java @@ -32,6 +32,8 @@ package org.scijava.io.handle; +import java.io.IOException; + import org.scijava.io.IOService; import org.scijava.io.location.Location; import org.scijava.plugin.WrapperService; @@ -63,4 +65,18 @@ default Class> getPluginType() { default Class getType() { return Location.class; } + + /** + * Convenience method to test whether it describes an existing file. + * + * @param location the location to test + * @return The result of {@link DataHandle#exists()} on a newly created handle + * on this location + * @throws IOException + */ + default boolean exists(final Location location) throws IOException { + try (DataHandle handle = create(location)) { + return handle.exists(); + } + } }