Skip to content

Commit a61acb7

Browse files
committed
refactor: back deprecated methods by default implementation using the new methods
1 parent 8ac4413 commit a61acb7

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.janelia.saalfeldlab.n5;
2+
3+
import org.janelia.saalfeldlab.n5.readdata.ReadData;
4+
5+
import java.io.*;
6+
7+
class BufferedKvaLockedChannel implements LockedChannel {
8+
9+
private final KeyValueAccess kva;
10+
private final String key;
11+
private ByteArrayOutputStream baos = null;
12+
13+
BufferedKvaLockedChannel(final KeyValueAccess kva, final String key) {
14+
this.kva = kva;
15+
this.key = key;
16+
}
17+
18+
@Override
19+
public Reader newReader() throws N5Exception.N5IOException {
20+
21+
return new InputStreamReader(newInputStream());
22+
}
23+
24+
@Override
25+
public InputStream newInputStream() throws N5Exception.N5IOException {
26+
return kva.createReadData(key).inputStream();
27+
}
28+
29+
@Override
30+
public Writer newWriter() throws N5Exception.N5IOException {
31+
32+
return new BufferedWriter(new OutputStreamWriter(newOutputStream()));
33+
}
34+
35+
@Override
36+
public OutputStream newOutputStream() throws N5Exception.N5IOException {
37+
if (baos == null)
38+
baos = new ByteArrayOutputStream();
39+
return baos;
40+
}
41+
42+
@Override
43+
public void close() throws IOException {
44+
if (baos != null && baos.size() > 0)
45+
kva.write(key, ReadData.from(baos.toByteArray()));
46+
}
47+
}

src/main/java/org/janelia/saalfeldlab/n5/KeyValueAccess.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,9 @@ default URI uri(final String uriString) throws URISyntaxException {
297297
* @deprecated migrate to {@link KeyValueAccess#createReadData(String)}
298298
*/
299299
@Deprecated
300-
LockedChannel lockForReading( final String normalPath ) throws N5IOException;
300+
default LockedChannel lockForReading( final String normalPath ) throws N5IOException {
301+
return new BufferedKvaLockedChannel(this, normalPath);
302+
}
301303

302304
/**
303305
* Create an exclusive lock on a path for writing. If the file doesn't exist
@@ -318,7 +320,9 @@ default URI uri(final String uriString) throws URISyntaxException {
318320
* @deprecated migrate to {@link KeyValueAccess#write(String, ReadData)}
319321
*/
320322
@Deprecated
321-
LockedChannel lockForWriting( final String normalPath ) throws N5IOException;
323+
default LockedChannel lockForWriting( final String normalPath ) throws N5IOException {
324+
return new BufferedKvaLockedChannel(this, normalPath);
325+
}
322326

323327
/**
324328
* List all 'directory'-like children of a path.

0 commit comments

Comments
 (0)