2828 */
2929package org .janelia .saalfeldlab .n5 ;
3030
31- import org .janelia .saalfeldlab .n5 .N5Exception .N5IOException ;
32- import org .janelia .saalfeldlab .n5 .N5Exception .N5NoSuchKeyException ;
33-
34- import java .io .ByteArrayOutputStream ;
3531import java .io .IOException ;
36- import java .io .InputStream ;
37- import java .io .InputStreamReader ;
38- import java .io .OutputStream ;
39- import java .io .OutputStreamWriter ;
40- import java .io .Reader ;
4132import java .io .UncheckedIOException ;
42- import java .io .Writer ;
4333import java .nio .file .Path ;
4434import java .nio .file .Paths ;
4535import java .util .ArrayList ;
4636import java .util .HashMap ;
4737import java .util .HashSet ;
4838import java .util .Iterator ;
4939
40+ import org .janelia .saalfeldlab .n5 .N5Exception .N5IOException ;
41+ import org .janelia .saalfeldlab .n5 .N5Exception .N5NoSuchKeyException ;
42+ import org .janelia .saalfeldlab .n5 .readdata .LazyRead ;
5043import org .janelia .saalfeldlab .n5 .readdata .ReadData ;
44+ import org .janelia .saalfeldlab .n5 .readdata .VolatileReadData ;
5145
5246/**
5347 * An in-memory {@link KeyValueAccess}.
48+ * <p>
49+ * This implementation does not implement an {@link IoPolicy}.
5450 */
5551public class InMemoryKeyValueAccess implements KeyValueAccess {
5652
@@ -72,31 +68,15 @@ public InMemoryKeyValueAccess() {
7268 }
7369
7470 @ Override
75- public ReadData createReadData (final String normalPath ) {
76- return new KeyValueAccessReadData (new MemoryLazyRead (normalPath ));
77- }
78-
79- @ Override
80- public LockedChannel lockForReading (final String normalPath ) throws N5IOException {
81-
82- if (!isFile (normalPath ))
83- throw new N5NoSuchKeyException ("No such key: " + normalPath );
71+ public VolatileReadData createReadData (final String normalPath ) {
8472
85- try {
86- return new MemoryLockedChannel (normalPath );
87- } catch (UncheckedIOException e ) {
88- throw new N5IOException ("Failed to lock file for reading: " + normalPath , e );
89- }
73+ return VolatileReadData .from (new MemoryLazyRead (normalPath ));
9074 }
9175
9276 @ Override
93- public synchronized LockedChannel lockForWriting ( final String normalPath ) throws N5IOException {
77+ public void write ( String normalPath , ReadData data ) {
9478
95- try {
96- return new MemoryLockedChannel (normalPath );
97- } catch (UncheckedIOException e ) {
98- throw new N5IOException ("Failed to lock file for writing: " + normalPath , e );
99- }
79+ files .put (normalPath , data );
10080 }
10181
10282 @ Override
@@ -111,7 +91,6 @@ public boolean isDirectory(final String normalPath) {
11191 public boolean isFile (final String normalPath ) {
11292
11393 return files .containsKey (normalPath );
114-
11594 }
11695
11796 @ Override
@@ -275,6 +254,10 @@ public ReadData materialize(final long offset, final long length) {
275254 }
276255 }
277256
257+ @ Override
258+ public void close () {
259+ // No-op
260+ }
278261 }
279262
280263 private static boolean validBounds (long channelSize , long offset , long length ) {
@@ -289,47 +272,4 @@ else if (length >= 0 && offset + length > channelSize)
289272 return true ;
290273 }
291274
292- private class MemoryLockedChannel implements LockedChannel {
293-
294- private final String key ;
295-
296- private ByteArrayOutputStream os ;
297-
298- MemoryLockedChannel (final String normalKey ) {
299- this .key = normalKey ;
300- }
301-
302- @ Override
303- public void close () throws IOException {
304-
305- files .put (key , ReadData .from (os .toByteArray ()));
306- }
307-
308- @ Override
309- public Reader newReader () throws N5IOException {
310-
311- return new InputStreamReader (newInputStream ());
312- }
313-
314- @ Override
315- public InputStream newInputStream () throws N5IOException {
316-
317- return InMemoryKeyValueAccess .this .files .get (key ).inputStream ();
318- }
319-
320- @ Override
321- public Writer newWriter () throws N5IOException {
322-
323- return new OutputStreamWriter (newOutputStream ());
324- }
325-
326- @ Override
327- public OutputStream newOutputStream () throws N5IOException {
328-
329- os = new ByteArrayOutputStream ();
330- return os ;
331- }
332-
333- }
334-
335275}
0 commit comments