|
34 | 34 | import org.janelia.saalfeldlab.n5.N5Exception.N5IOException; |
35 | 35 | import org.janelia.saalfeldlab.n5.N5Exception.N5NoSuchKeyException; |
36 | 36 |
|
37 | | -import java.nio.ByteBuffer; |
38 | | -import java.nio.channels.FileChannel; |
39 | 37 | import java.net.URI; |
40 | 38 | import java.net.URISyntaxException; |
41 | 39 | import java.nio.file.attribute.FileAttribute; |
|
45 | 43 | import java.util.stream.Stream; |
46 | 44 |
|
47 | 45 | import org.janelia.saalfeldlab.n5.readdata.ReadData; |
48 | | -import org.janelia.saalfeldlab.n5.readdata.LazyRead; |
49 | 46 | import org.janelia.saalfeldlab.n5.readdata.VolatileReadData; |
50 | 47 |
|
51 | | -import static org.janelia.saalfeldlab.n5.FileKeyLockManager.FILE_LOCK_MANAGER; |
52 | | - |
53 | 48 | /** |
54 | 49 | * Filesystem {@link KeyValueAccess}. |
55 | 50 | * |
@@ -100,44 +95,6 @@ public void write(final String normalPath, final ReadData data) throws N5IOExcep |
100 | 95 | } |
101 | 96 | } |
102 | 97 |
|
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 | | - |
141 | 98 | @Override |
142 | 99 | public boolean isDirectory(final String normalPath) { |
143 | 100 |
|
@@ -165,17 +122,6 @@ public long size(final String normalPath) { |
165 | 122 | return size(Paths.get(normalPath)); |
166 | 123 | } |
167 | 124 |
|
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 | | - |
179 | 125 | @Override |
180 | 126 | public String[] listDirectories(final String normalPath) throws N5IOException { |
181 | 127 |
|
@@ -346,6 +292,17 @@ public void delete(final String normalPath) throws N5IOException { |
346 | 292 | } |
347 | 293 | } |
348 | 294 |
|
| 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 | + |
349 | 306 | protected static void tryDelete(final Path path) throws IOException { |
350 | 307 |
|
351 | 308 | try { |
@@ -501,88 +458,4 @@ protected static void createAndCheckIsDirectory( |
501 | 458 | throw x; |
502 | 459 | } |
503 | 460 | } |
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 | | - |
588 | 461 | } |
0 commit comments