diff --git a/src/main/java/org/janelia/saalfeldlab/n5/CachedGsonKeyValueN5Reader.java b/src/main/java/org/janelia/saalfeldlab/n5/CachedGsonKeyValueN5Reader.java index fd51202..812c6eb 100644 --- a/src/main/java/org/janelia/saalfeldlab/n5/CachedGsonKeyValueN5Reader.java +++ b/src/main/java/org/janelia/saalfeldlab/n5/CachedGsonKeyValueN5Reader.java @@ -25,7 +25,6 @@ */ package org.janelia.saalfeldlab.n5; -import java.io.IOException; import java.lang.reflect.Type; import com.google.gson.JsonSyntaxException; @@ -233,20 +232,6 @@ default String[] listFromContainer(final String normalPathName) { return GsonKeyValueN5Reader.super.list(normalPathName); } - @Override - default String absoluteDataBlockPath( - final String normalPath, - final long... gridPosition) { - - final String[] components = new String[gridPosition.length + 1]; - components[0] = normalPath; - int i = 0; - for (final long p : gridPosition) - components[++i] = Long.toString(p); - - return getKeyValueAccess().compose(getURI(), components); - } - /** * Check for attributes that are required for a group to be a dataset. * diff --git a/src/main/java/org/janelia/saalfeldlab/n5/FileSystemKeyValueAccess.java b/src/main/java/org/janelia/saalfeldlab/n5/FileSystemKeyValueAccess.java index 01dc75d..cb7444f 100644 --- a/src/main/java/org/janelia/saalfeldlab/n5/FileSystemKeyValueAccess.java +++ b/src/main/java/org/janelia/saalfeldlab/n5/FileSystemKeyValueAccess.java @@ -160,7 +160,11 @@ public FileSystemKeyValueAccess(final FileSystem fileSystem) { @Override public LockedFileChannel lockForReading(final String normalPath) throws IOException { - return new LockedFileChannel(normalPath, true); + try { + return new LockedFileChannel(normalPath, true); + } catch (NoSuchFileException e) { + throw new N5Exception.N5NoSuchKeyException("No such file", e); + } } @Override @@ -171,7 +175,11 @@ public LockedFileChannel lockForWriting(final String normalPath) throws IOExcept public LockedFileChannel lockForReading(final Path path) throws IOException { - return new LockedFileChannel(path, true); + try { + return new LockedFileChannel(path, true); + } catch (NoSuchFileException e) { + throw new N5Exception.N5NoSuchKeyException("No such file", e); + } } public LockedFileChannel lockForWriting(final Path path) throws IOException { diff --git a/src/main/java/org/janelia/saalfeldlab/n5/GsonKeyValueN5Reader.java b/src/main/java/org/janelia/saalfeldlab/n5/GsonKeyValueN5Reader.java index 644aca5..080d5df 100644 --- a/src/main/java/org/janelia/saalfeldlab/n5/GsonKeyValueN5Reader.java +++ b/src/main/java/org/janelia/saalfeldlab/n5/GsonKeyValueN5Reader.java @@ -94,11 +94,11 @@ default DataBlock readBlock( final long... gridPosition) throws N5Exception { final String path = absoluteDataBlockPath(N5URI.normalizeGroupPath(pathName), gridPosition); - if (!getKeyValueAccess().isFile(path)) - return null; try (final LockedChannel lockedChannel = getKeyValueAccess().lockForReading(path)) { return DefaultBlockReader.readBlock(lockedChannel.newInputStream(), datasetAttributes, gridPosition); + } catch (final N5Exception.N5NoSuchKeyException e) { + return null; } catch (final IOException | UncheckedIOException e) { throw new N5IOException( "Failed to read block " + Arrays.toString(gridPosition) + " from dataset " + path, @@ -137,14 +137,13 @@ default String absoluteDataBlockPath( final String normalPath, final long... gridPosition) { - final String[] components = new String[gridPosition.length + 2]; - components[0] = getURI().getPath(); - components[1] = normalPath; - int i = 1; + final String[] components = new String[gridPosition.length + 1]; + components[0] = normalPath; + int i = 0; for (final long p : gridPosition) components[++i] = Long.toString(p); - return getKeyValueAccess().compose(components); + return getKeyValueAccess().compose(getURI(), components); } /** diff --git a/src/main/java/org/janelia/saalfeldlab/n5/N5Exception.java b/src/main/java/org/janelia/saalfeldlab/n5/N5Exception.java index bbdba53..345a7cd 100644 --- a/src/main/java/org/janelia/saalfeldlab/n5/N5Exception.java +++ b/src/main/java/org/janelia/saalfeldlab/n5/N5Exception.java @@ -94,4 +94,31 @@ protected N5ClassCastException( super(message, cause, enableSuppression, writableStackTrace); } } + + public static class N5NoSuchKeyException extends N5IOException { + + public N5NoSuchKeyException(final String message) { + + super(message); + } + + public N5NoSuchKeyException(final String message, final Throwable cause) { + + super(message, cause); + } + + public N5NoSuchKeyException(final Throwable cause) { + + super(cause); + } + + protected N5NoSuchKeyException( + final String message, + final Throwable cause, + final boolean enableSuppression, + final boolean writableStackTrace) { + + super(message, cause, enableSuppression, writableStackTrace); + } + } }