5353 */
5454package org .janelia .saalfeldlab .n5 ;
5555
56+ import org .janelia .saalfeldlab .n5 .N5Exception .N5IOException ;
57+ import org .janelia .saalfeldlab .n5 .N5Exception .N5NoSuchKeyException ;
58+
5659import java .io .File ;
5760import java .io .IOException ;
5861import java .io .InputStream ;
5962import java .io .OutputStream ;
6063import java .io .Reader ;
64+ import java .io .UncheckedIOException ;
6165import java .io .Writer ;
6266import java .net .URI ;
6367import java .net .URISyntaxException ;
@@ -141,29 +145,40 @@ protected LockedFileChannel(final Path path, final boolean readOnly) throws IOEx
141145 }
142146 }
143147
148+ private void truncateChannel (int size ) {
149+
150+ try {
151+ channel .truncate (size );
152+ } catch (NoSuchFileException e ) {
153+ throw new N5NoSuchKeyException ("No such file" , e );
154+ } catch (IOException | UncheckedIOException e ) {
155+ throw new N5IOException ("Failed to truncate locked file channel" , e );
156+ }
157+ }
158+
144159 @ Override
145- public Reader newReader () throws IOException {
160+ public Reader newReader () throws N5IOException {
146161
147162 return Channels .newReader (channel , StandardCharsets .UTF_8 .name ());
148163 }
149164
150165 @ Override
151- public Writer newWriter () throws IOException {
166+ public Writer newWriter () throws N5IOException {
152167
153- channel . truncate (0 );
168+ truncateChannel (0 );
154169 return Channels .newWriter (channel , StandardCharsets .UTF_8 .name ());
155170 }
156171
157172 @ Override
158- public InputStream newInputStream () throws IOException {
173+ public InputStream newInputStream () throws N5IOException {
159174
160175 return Channels .newInputStream (channel );
161176 }
162177
163178 @ Override
164- public OutputStream newOutputStream () throws IOException {
179+ public OutputStream newOutputStream () throws N5IOException {
165180
166- channel . truncate (0 );
181+ truncateChannel (0 );
167182 return Channels .newOutputStream (channel );
168183 }
169184
@@ -187,33 +202,49 @@ public FileSystemKeyValueAccess(final FileSystem fileSystem) {
187202 }
188203
189204 @ Override
190- public LockedFileChannel lockForReading (final String normalPath ) throws IOException {
205+ public LockedChannel lockForReading (final String normalPath ) throws N5IOException {
191206
192207 try {
193208 return new LockedFileChannel (normalPath , true );
194- } catch (NoSuchFileException e ) {
195- throw new N5Exception .N5NoSuchKeyException ("No such file" , e );
209+ } catch (final NoSuchFileException e ) {
210+ throw new N5NoSuchKeyException ("No such file" , e );
211+ } catch (IOException | UncheckedIOException e ) {
212+ throw new N5IOException ("Failed to lock file for reading: " + normalPath , e );
196213 }
197214 }
198215
199216 @ Override
200- public LockedFileChannel lockForWriting (final String normalPath ) throws IOException {
217+ public LockedChannel lockForWriting (final String normalPath ) throws N5IOException {
201218
202- return new LockedFileChannel (normalPath , false );
219+ try {
220+ return new LockedFileChannel (normalPath , false );
221+ } catch (final NoSuchFileException e ) {
222+ throw new N5NoSuchKeyException ("No such file" , e );
223+ } catch (IOException | UncheckedIOException e ) {
224+ throw new N5IOException ("Failed to lock file for writing: " + normalPath , e );
225+ }
203226 }
204227
205- public LockedFileChannel lockForReading (final Path path ) throws IOException {
228+ public LockedChannel lockForReading (final Path path ) throws N5IOException {
206229
207230 try {
208231 return new LockedFileChannel (path , true );
209- } catch (NoSuchFileException e ) {
210- throw new N5Exception .N5NoSuchKeyException ("No such file" , e );
232+ } catch (final NoSuchFileException e ) {
233+ throw new N5NoSuchKeyException ("No such file" , e );
234+ } catch (IOException | UncheckedIOException e ) {
235+ throw new N5IOException ("Failed to lock file for reading: " + path , e );
211236 }
212237 }
213238
214- public LockedFileChannel lockForWriting (final Path path ) throws IOException {
239+ public LockedChannel lockForWriting (final Path path ) throws N5IOException {
215240
216- return new LockedFileChannel (path , false );
241+ try {
242+ return new LockedFileChannel (path , false );
243+ } catch (final NoSuchFileException e ) {
244+ throw new N5NoSuchKeyException ("No such file" , e );
245+ } catch (IOException | UncheckedIOException e ) {
246+ throw new N5IOException ("Failed to lock file for writing: " + path , e );
247+ }
217248 }
218249
219250 @ Override
@@ -238,25 +269,33 @@ public boolean exists(final String normalPath) {
238269 }
239270
240271 @ Override
241- public String [] listDirectories (final String normalPath ) throws IOException {
272+ public String [] listDirectories (final String normalPath ) throws N5IOException {
242273
243274 final Path path = fileSystem .getPath (normalPath );
244275 try (final Stream <Path > pathStream = Files .list (path )) {
245276 return pathStream
246277 .filter (a -> Files .isDirectory (a ))
247278 .map (a -> path .relativize (a ).toString ())
248279 .toArray (n -> new String [n ]);
280+ } catch (NoSuchFileException e ) {
281+ throw new N5NoSuchKeyException ("No such file" , e );
282+ } catch (IOException | UncheckedIOException e ) {
283+ throw new N5IOException ("Failed to list directories" , e );
249284 }
250285 }
251286
252287 @ Override
253- public String [] list (final String normalPath ) throws IOException {
288+ public String [] list (final String normalPath ) throws N5IOException {
254289
255290 final Path path = fileSystem .getPath (normalPath );
256291 try (final Stream <Path > pathStream = Files .list (path )) {
257292 return pathStream
258293 .map (a -> path .relativize (a ).toString ())
259294 .toArray (n -> new String [n ]);
295+ } catch (NoSuchFileException e ) {
296+ throw new N5NoSuchKeyException ("No such file" , e );
297+ } catch (IOException | UncheckedIOException e ) {
298+ throw new N5IOException ("Failed to list files" , e );
260299 }
261300 }
262301
@@ -362,32 +401,42 @@ public String compose(final String... components) {
362401 }
363402
364403 @ Override
365- public void createDirectories (final String normalPath ) throws IOException {
404+ public void createDirectories (final String normalPath ) throws N5IOException {
366405
367- createDirectories (fileSystem .getPath (normalPath ));
406+ try {
407+ createDirectories (fileSystem .getPath (normalPath ));
408+ } catch (NoSuchFileException e ) {
409+ throw new N5NoSuchKeyException ("No such file" , e );
410+ } catch (IOException | UncheckedIOException e ) {
411+ throw new N5IOException ("Failed to create directories" , e );
412+ }
368413 }
369414
370415 @ Override
371- public void delete (final String normalPath ) throws IOException {
416+ public void delete (final String normalPath ) throws N5IOException {
372417
373- final Path path = fileSystem .getPath (normalPath );
418+ try {
419+ final Path path = fileSystem .getPath (normalPath );
374420
375- if (Files .isRegularFile (path ))
376- try (final LockedChannel channel = lockForWriting (path )) {
377- Files .delete (path );
378- }
379- else {
380- try (final Stream <Path > pathStream = Files .walk (path )) {
381- for (final Iterator <Path > i = pathStream .sorted (Comparator .reverseOrder ()).iterator (); i .hasNext ();) {
382- final Path childPath = i .next ();
383- if (Files .isRegularFile (childPath ))
384- try (final LockedChannel channel = lockForWriting (childPath )) {
385- Files .delete (childPath );
386- }
387- else
388- tryDelete (childPath );
421+ if (Files .isRegularFile (path ))
422+ try (final LockedChannel channel = lockForWriting (path )) {
423+ Files .delete (path );
424+ }
425+ else {
426+ try (final Stream <Path > pathStream = Files .walk (path )) {
427+ for (final Iterator <Path > i = pathStream .sorted (Comparator .reverseOrder ()).iterator (); i .hasNext ();) {
428+ final Path childPath = i .next ();
429+ if (Files .isRegularFile (childPath ))
430+ try (final LockedChannel channel = lockForWriting (childPath )) {
431+ Files .delete (childPath );
432+ }
433+ else
434+ tryDelete (childPath );
435+ }
389436 }
390437 }
438+ } catch (IOException | UncheckedIOException e ) {
439+ throw new N5IOException ("Failed to delete file at " + normalPath , e );
391440 }
392441 }
393442
0 commit comments