3434import org .janelia .saalfeldlab .n5 .N5Exception .N5IOException ;
3535import org .janelia .saalfeldlab .n5 .N5Exception .N5NoSuchKeyException ;
3636
37- import java .nio .ByteBuffer ;
38- import java .nio .channels .FileChannel ;
3937import java .net .URI ;
4038import java .net .URISyntaxException ;
4139import java .nio .file .attribute .FileAttribute ;
4543import java .util .stream .Stream ;
4644
4745import org .janelia .saalfeldlab .n5 .readdata .ReadData ;
48- import org .janelia .saalfeldlab .n5 .readdata .LazyRead ;
4946import org .janelia .saalfeldlab .n5 .readdata .VolatileReadData ;
5047
51- import static org .janelia .saalfeldlab .n5 .FileKeyLockManager .FILE_LOCK_MANAGER ;
52-
5348/**
5449 * Filesystem {@link KeyValueAccess}.
5550 *
@@ -67,11 +62,11 @@ private static IoPolicy getIoPolicy() {
6762 return FsIoPolicy .atomicWithFallback ;
6863
6964 switch (property ) {
70- case "atomic " :
65+ case "strict " :
7166 return new FsIoPolicy .Atomic ();
7267 case "unsafe" :
7368 return new FsIoPolicy .Unsafe ();
74- case "atomicFallbackUnsafe " :
69+ case "permissive " :
7570 default :
7671 return FsIoPolicy .atomicWithFallback ;
7772 }
@@ -100,44 +95,6 @@ public void write(final String normalPath, final ReadData data) throws N5IOExcep
10095 }
10196 }
10297
103- @ Override
104- @ Deprecated
105- public LockedFileChannel lockForReading (final String normalPath ) throws N5IOException {
106-
107- return lockForReading (Paths .get (normalPath ));
108- }
109-
110- @ Override
111- @ Deprecated
112- public LockedChannel lockForWriting (final String normalPath ) throws N5IOException {
113-
114- return lockForWriting (Paths .get (normalPath ));
115- }
116-
117- @ Deprecated
118- protected static LockedFileChannel lockForReading (final Path path ) throws N5IOException {
119-
120- try {
121- return FILE_LOCK_MANAGER .lockForReading (path );
122- } catch (final NoSuchFileException e ) {
123- throw new N5NoSuchKeyException ("No such file" , e );
124- } catch (IOException | UncheckedIOException e ) {
125- throw new N5IOException ("Failed to lock file for reading: " + path , e );
126- }
127- }
128-
129- @ Deprecated
130- static LockedFileChannel lockForWriting (final Path path ) throws N5IOException {
131-
132- try {
133- return FILE_LOCK_MANAGER .lockForWriting (path );
134- } catch (final NoSuchFileException e ) {
135- throw new N5NoSuchKeyException ("No such file" , e );
136- } catch (IOException | UncheckedIOException e ) {
137- throw new N5IOException ("Failed to lock file for writing: " + path , e );
138- }
139- }
140-
14198 @ Override
14299 public boolean isDirectory (final String normalPath ) {
143100
@@ -165,17 +122,6 @@ public long size(final String normalPath) {
165122 return size (Paths .get (normalPath ));
166123 }
167124
168- protected static long size (final Path path ) {
169-
170- try {
171- return Files .size (path );
172- } catch (NoSuchFileException e ) {
173- throw new N5NoSuchKeyException ("No such file" , e );
174- } catch (IOException | UncheckedIOException e ) {
175- throw new N5IOException (e );
176- }
177- }
178-
179125 @ Override
180126 public String [] listDirectories (final String normalPath ) throws N5IOException {
181127
@@ -346,6 +292,17 @@ public void delete(final String normalPath) throws N5IOException {
346292 }
347293 }
348294
295+ protected static long size (final Path path ) {
296+
297+ try {
298+ return Files .size (path );
299+ } catch (NoSuchFileException e ) {
300+ throw new N5NoSuchKeyException ("No such file" , e );
301+ } catch (IOException | UncheckedIOException e ) {
302+ throw new N5IOException (e );
303+ }
304+ }
305+
349306 protected static void tryDelete (final Path path ) throws IOException {
350307
351308 try {
@@ -501,88 +458,4 @@ protected static void createAndCheckIsDirectory(
501458 throw x ;
502459 }
503460 }
504-
505- static class FileLazyRead implements LazyRead {
506-
507- private static final Closeable NO_OP = () -> { };
508-
509- private final Path path ;
510- private Closeable lock ;
511-
512- FileLazyRead (final Path path ) throws IOException {
513- this (path , true );
514- }
515-
516- FileLazyRead (final Path path , final boolean requireLock ) throws IOException {
517- this .path = path ;
518- if (requireLock )
519- lock = FILE_LOCK_MANAGER .lockForReading (path );
520- else
521- lock = NO_OP ;
522- }
523-
524- @ Override
525- public long size () throws N5IOException {
526-
527- if (lock == null ) {
528- throw new N5IOException ("FileLazyRead is already closed." );
529- }
530- return FileSystemKeyValueAccess .size (path );
531- }
532-
533- @ Override
534- public ReadData materialize (final long offset , final long length ) {
535-
536- if (lock == null ) {
537- throw new N5IOException ("FileLazyRead is already closed." );
538- }
539-
540- try (final FileChannel channel = FileChannel .open (path , StandardOpenOption .READ )) {
541-
542- channel .position (offset );
543-
544- final long channelSize = channel .size ();
545- if (!validBounds (channelSize , offset , length )) {
546- throw new IndexOutOfBoundsException ();
547- }
548-
549- final long size = length < 0 ? (channelSize - offset ) : length ;
550- if (size > Integer .MAX_VALUE ) {
551- throw new IndexOutOfBoundsException ("Attempt to materialize too large data" );
552- }
553-
554- final byte [] data = new byte [(int ) size ];
555- final ByteBuffer buf = ByteBuffer .wrap (data );
556- channel .read (buf );
557- return ReadData .from (data );
558-
559- } catch (final NoSuchFileException e ) {
560- throw new N5NoSuchKeyException ("No such file" , e );
561- } catch (IOException | UncheckedIOException e ) {
562- throw new N5IOException (e );
563- }
564- }
565-
566- @ Override
567- public void close () throws IOException {
568-
569- if (lock != null ) {
570- lock .close ();
571- lock = null ;
572- }
573- }
574- }
575-
576- private static boolean validBounds (long channelSize , long offset , long length ) {
577-
578- if (offset < 0 )
579- return false ;
580- else if (channelSize > 0 && offset >= channelSize ) // offset == 0 and channelSize == 0 is okay
581- return false ;
582- else if (length >= 0 && offset + length > channelSize )
583- return false ;
584-
585- return true ;
586- }
587-
588461}
0 commit comments