Skip to content

Commit

Permalink
fix: wrap unchecked IOExceptions as well (such as AccessDenied)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmhulbert committed Aug 29, 2023
1 parent 51e2b79 commit 22ae9da
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package org.janelia.saalfeldlab.n5;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Arrays;

import org.janelia.saalfeldlab.n5.N5Exception.N5IOException;
Expand Down Expand Up @@ -72,7 +73,7 @@ else if (getCache().exists(normalPath, N5KeyValueReader.ATTRIBUTES_JSON)) {
*/
try {
getKeyValueAccess().createDirectories(absoluteGroupPath(normalPath));
} catch (final IOException e) {
} catch (final IOException | UncheckedIOException e) {
throw new N5Exception.N5IOException("Failed to create group " + path, e);
}

Expand Down Expand Up @@ -146,7 +147,7 @@ default boolean remove(final String path) throws N5Exception {
try {
if (getKeyValueAccess().isDirectory(groupPath))
getKeyValueAccess().delete(groupPath);
} catch (final IOException e) {
} catch (final IOException | UncheckedIOException e) {
throw new N5IOException("Failed to remove " + path, e);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package org.janelia.saalfeldlab.n5;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Arrays;

import org.janelia.saalfeldlab.n5.N5Exception.N5IOException;
Expand Down Expand Up @@ -80,7 +81,7 @@ default JsonElement getAttributes(final String pathName) throws N5Exception {

try (final LockedChannel lockedChannel = getKeyValueAccess().lockForReading(attributesPath)) {
return GsonUtils.readAttributes(lockedChannel.newReader(), getGson());
} catch (final IOException e) {
} catch (final IOException | UncheckedIOException e) {
throw new N5IOException("Failed to read attributes from dataset " + pathName, e);
}

Expand All @@ -98,7 +99,7 @@ default DataBlock<?> readBlock(

try (final LockedChannel lockedChannel = getKeyValueAccess().lockForReading(path)) {
return DefaultBlockReader.readBlock(lockedChannel.newInputStream(), datasetAttributes, gridPosition);
} catch (final IOException e) {
} catch (final IOException | UncheckedIOException e) {
throw new N5IOException(
"Failed to read block " + Arrays.toString(gridPosition) + " from dataset " + path,
e);
Expand All @@ -110,7 +111,7 @@ default String[] list(final String pathName) throws N5Exception {

try {
return getKeyValueAccess().listDirectories(absoluteGroupPath(pathName));
} catch (final IOException e) {
} catch (final IOException | UncheckedIOException e) {
throw new N5IOException("Cannot list directories for group " + pathName, e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package org.janelia.saalfeldlab.n5;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -72,7 +73,7 @@ default void createGroup(final String path) throws N5Exception {
final String normalPath = N5URI.normalizeGroupPath(path);
try {
getKeyValueAccess().createDirectories(absoluteGroupPath(normalPath));
} catch (final IOException e) {
} catch (final IOException | UncheckedIOException e) {
throw new N5Exception.N5IOException("Failed to create group " + path, e);
}
}
Expand All @@ -96,7 +97,7 @@ default void writeAttributes(

try (final LockedChannel lock = getKeyValueAccess().lockForWriting(absoluteAttributesPath(normalGroupPath))) {
GsonUtils.writeAttributes(lock.newWriter(), attributes, getGson());
} catch (final IOException e) {
} catch (final IOException | UncheckedIOException e) {
throw new N5Exception.N5IOException("Failed to write attributes into " + normalGroupPath, e);
}
}
Expand Down Expand Up @@ -218,7 +219,7 @@ default <T> void writeBlock(
final String blockPath = absoluteDataBlockPath(N5URI.normalizeGroupPath(path), dataBlock.getGridPosition());
try (final LockedChannel lock = getKeyValueAccess().lockForWriting(blockPath)) {
DefaultBlockWriter.writeBlock(lock.newOutputStream(), datasetAttributes, dataBlock);
} catch (final IOException e) {
} catch (final IOException | UncheckedIOException e) {
throw new N5IOException(
"Failed to write block " + Arrays.toString(dataBlock.getGridPosition()) + " into dataset " + path,
e);
Expand All @@ -233,7 +234,7 @@ default boolean remove(final String path) throws N5Exception {
try {
if (getKeyValueAccess().isDirectory(groupPath))
getKeyValueAccess().delete(groupPath);
} catch (final IOException e) {
} catch (final IOException | UncheckedIOException e) {
throw new N5IOException("Failed to remove " + path, e);
}

Expand All @@ -250,7 +251,7 @@ default boolean deleteBlock(
try {
if (getKeyValueAccess().isFile(blockPath))
getKeyValueAccess().delete(blockPath);
} catch (final IOException e) {
} catch (final IOException | UncheckedIOException e) {
throw new N5IOException(
"Failed to delete block " + Arrays.toString(gridPosition) + " from dataset " + path,
e);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/janelia/saalfeldlab/n5/N5Exception.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ public N5IOException(final String message) {
super(message);
}

public N5IOException(final String message, final IOException cause) {
public N5IOException(final String message, final Exception cause) {

super(message, cause);
}

public N5IOException(final IOException cause) {
public N5IOException(final Exception cause) {

super(cause);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/janelia/saalfeldlab/n5/N5Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.io.UncheckedIOException;
import java.lang.reflect.Type;
import java.net.URI;
import java.util.ArrayList;
Expand Down Expand Up @@ -324,7 +325,7 @@ default <T> T readSerializedBlock(
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(block.toByteBuffer().array());
try (ObjectInputStream in = new ObjectInputStream(byteArrayInputStream)) {
return (T)in.readObject();
} catch (final IOException e) {
} catch (final IOException | UncheckedIOException e) {
throw new N5Exception.N5IOException(e);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/janelia/saalfeldlab/n5/N5Writer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.UncheckedIOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -280,7 +281,7 @@ default void writeSerializedBlock(
final ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
try (ObjectOutputStream out = new ObjectOutputStream(byteOutputStream)) {
out.writeObject(object);
} catch (final IOException e) {
} catch (final IOException | UncheckedIOException e) {
throw new N5Exception.N5IOException(e);
}
final byte[] bytes = byteOutputStream.toByteArray();
Expand Down

0 comments on commit 22ae9da

Please sign in to comment.