Skip to content

Commit 25c5da1

Browse files
committed
Merge branch 'more-handles-buffering'
2 parents e2a0b3f + 61ff8ea commit 25c5da1

File tree

9 files changed

+1053
-0
lines changed

9 files changed

+1053
-0
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ Konstanz, and KNIME GmbH.</license.copyrightOwners>
184184
<artifactId>junit</artifactId>
185185
<scope>test</scope>
186186
</dependency>
187+
<dependency>
188+
<groupId>org.mockito</groupId>
189+
<artifactId>mockito-core</artifactId>
190+
<scope>test</scope>
191+
</dependency>
187192
</dependencies>
188193

189194
<build>
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck
7+
* Institute of Molecular Cell Biology and Genetics, University of
8+
* Konstanz, and KNIME GmbH.
9+
* %%
10+
* Redistribution and use in source and binary forms, with or without
11+
* modification, are permitted provided that the following conditions are met:
12+
*
13+
* 1. Redistributions of source code must retain the above copyright notice,
14+
* this list of conditions and the following disclaimer.
15+
* 2. Redistributions in binary form must reproduce the above copyright notice,
16+
* this list of conditions and the following disclaimer in the documentation
17+
* and/or other materials provided with the distribution.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
23+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
* #L%
31+
*/
32+
33+
package org.scijava.io.handle;
34+
35+
import java.io.IOException;
36+
37+
import org.scijava.io.location.Location;
38+
39+
/**
40+
* Abstract superclass for {@link DataHandle}s that operate over other
41+
* {@link DataHandle}s.
42+
*
43+
* @author Gabriel Einsdorf
44+
*/
45+
public abstract class AbstractHigherOrderHandle<L extends Location> extends
46+
AbstractDataHandle<L>
47+
{
48+
49+
private DataHandle<L> handle;
50+
private boolean closed;
51+
52+
public AbstractHigherOrderHandle(final DataHandle<L> handle) {
53+
this.handle = handle;
54+
set(handle.get()); // provides access to underlying location
55+
}
56+
57+
@Override
58+
public boolean isReadable() {
59+
return !closed && handle.isReadable();
60+
}
61+
62+
@Override
63+
public boolean isWritable() {
64+
return !closed && handle.isWritable();
65+
}
66+
67+
@Override
68+
public long length() throws IOException {
69+
ensureOpen();
70+
return handle.length();
71+
}
72+
73+
@Override
74+
public Class<L> getType() {
75+
return handle.getType();
76+
}
77+
78+
@Override
79+
public boolean exists() throws IOException {
80+
return handle.exists();
81+
}
82+
83+
@Override
84+
public void close() throws IOException {
85+
if (!closed) {
86+
cleanup();
87+
closed = true;
88+
handle.close();
89+
handle = null;
90+
}
91+
}
92+
93+
protected void ensureOpen() throws IOException {
94+
if (closed) {
95+
throw new IOException("This handle is closed!");
96+
}
97+
}
98+
99+
/**
100+
* Clean up data structures after a handle has been closed in the
101+
* {@link #close()} method.
102+
*
103+
* @throws IOException
104+
*/
105+
protected abstract void cleanup() throws IOException;
106+
107+
/**
108+
* @return the {@link DataHandle} wrapped by this
109+
* {@link AbstractHigherOrderHandle}
110+
*/
111+
protected DataHandle<L> handle() {
112+
return handle;
113+
}
114+
115+
}

src/main/java/org/scijava/io/handle/DataHandleService.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
package org.scijava.io.handle;
3434

3535
import java.io.IOException;
36+
import java.util.Objects;
3637

3738
import org.scijava.io.IOService;
3839
import org.scijava.io.location.Location;
@@ -79,4 +80,40 @@ default boolean exists(final Location location) throws IOException {
7980
return handle.exists();
8081
}
8182
}
83+
84+
/**
85+
* Wraps the provided {@link DataHandle} in a read-only buffer for accelerated
86+
* reading.
87+
*
88+
* @param handle the handle to wrap
89+
* @see ReadBufferDataHandle#ReadBufferDataHandle(DataHandle)
90+
*/
91+
default DataHandle<Location> readBuffer(final DataHandle<Location> handle) {
92+
Objects.nonNull(handle);
93+
return new ReadBufferDataHandle(handle);
94+
}
95+
96+
/**
97+
* Creates a {@link DataHandle} on the provided {@link Location} wrapped in a
98+
* read-only buffer for accelerated reading.
99+
*
100+
* @param location the handle to wrap
101+
* @see ReadBufferDataHandle#ReadBufferDataHandle(DataHandle)
102+
*/
103+
default DataHandle<Location> readBuffer(final Location location) {
104+
final DataHandle<Location> handle = create(location);
105+
return handle == null ? null : new ReadBufferDataHandle(handle);
106+
}
107+
108+
/**
109+
* Wraps the provided {@link DataHandle} in a write-only buffer for
110+
* accelerated writing.
111+
*
112+
* @param handle the handle to wrap
113+
* @see WriteBufferDataHandle#WriteBufferDataHandle(DataHandle)
114+
*/
115+
default DataHandle<Location> writeBuffer(final DataHandle<Location> handle) {
116+
Objects.nonNull(handle);
117+
return new WriteBufferDataHandle(handle);
118+
}
82119
}

src/main/java/org/scijava/io/handle/DataHandles.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ private static synchronized void initUTFMethod() {
112112
"No usable DataOutputStream.writeUTF(String, DataOutput)", exc);
113113
}
114114
}
115+
116+
protected static IOException readOnlyException() {
117+
return new IOException("This handle is read-only!");
118+
}
119+
120+
protected static IOException writeOnlyException() {
121+
return new IOException("This handle is write-only!");
122+
}
123+
115124

116125
/**
117126
* Copies all bytes from the input to the output handle. Reading and writing

0 commit comments

Comments
 (0)