Skip to content

Commit 7f92778

Browse files
committed
Add ResettableStreamHandle and SeekableStreamHandle
These subinterfaces of StreamHandle provide random access over the contained streams.
1 parent 763c105 commit 7f92778

File tree

3 files changed

+224
-0
lines changed

3 files changed

+224
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.io;
33+
34+
import java.io.IOException;
35+
36+
public abstract class AbstractSeekableStreamHandle<L extends Location> extends
37+
AbstractStreamHandle<L> implements SeekableStreamHandle<L>
38+
{
39+
40+
private long jumpCuttoff = 10000;
41+
42+
@Override
43+
public void seek(final long pos) throws IOException {
44+
45+
// how much and which direction we have to jump
46+
final long delta = pos - offset();
47+
48+
if (delta == 0) {
49+
return;
50+
// nothing to do
51+
}
52+
else if (delta > 0) {
53+
// offset position is "downstream"
54+
55+
// try to reconnect instead of linearly reading large chunks
56+
if (recreatePossible() && delta > jumpCuttoff) {
57+
recreateStreamFromPos(pos);
58+
}
59+
else {
60+
jump(delta);
61+
}
62+
63+
}
64+
else { // delta < 0
65+
// need to recreate the stream
66+
if (recreatePossible()) {
67+
recreateStreamFromPos(pos);
68+
}
69+
else {
70+
resetStream();
71+
jump(pos);
72+
}
73+
}
74+
setOffset(pos);
75+
}
76+
77+
/**
78+
* Recreates the internal input stream available through {@link #in()}, so
79+
* that it starts from the specified position.
80+
*
81+
* @param pos
82+
* @throws IOException
83+
*/
84+
protected abstract void recreateStreamFromPos(long pos) throws IOException;
85+
86+
/**
87+
* In some implementations of this class, the ability to recreate the stream
88+
* depends on external factors (e.g. server support). This influences a
89+
*
90+
* @return if recreate is actually possible.
91+
* @throws IOException
92+
*/
93+
protected abstract boolean recreatePossible() throws IOException;
94+
95+
/**
96+
* Sets the jumpCutoff, the maximum of bytes which are read from the stream
97+
* when seeking forward, any larger number will result in a call to
98+
* {@link #recreateStreamFromPos(long)}.
99+
*/
100+
protected void setJumpCuttoff(long jumpCuttoff) {
101+
this.jumpCuttoff = jumpCuttoff;
102+
}
103+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.io;
33+
34+
import java.io.IOException;
35+
import java.io.InputStream;
36+
import java.io.OutputStream;
37+
38+
/**
39+
* A {@link DataHandle} backed by an {@link InputStream} and/or
40+
* {@link OutputStream}. Supports resetting the handle to the start of the
41+
* internal stream(s).
42+
*/
43+
public interface ResettableStreamHandle<L extends Location> extends
44+
StreamHandle<L>
45+
{
46+
47+
@Override
48+
default void seek(final long pos) throws IOException {
49+
final long off = offset();
50+
if (pos == off) return; // nothing to do
51+
if (pos > off) {
52+
// jump from the current offset
53+
jump(pos - off);
54+
}
55+
else {
56+
// jump from the beginning of the stream
57+
resetStream();
58+
jump(pos);
59+
}
60+
setOffset(pos);
61+
}
62+
63+
/**
64+
* Resets the stream to its start.
65+
*
66+
* @throws IOException If something goes wrong with the reset
67+
*/
68+
@Override
69+
void resetStream() throws IOException;
70+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.io;
33+
34+
import java.io.IOException;
35+
import java.io.InputStream;
36+
import java.io.OutputStream;
37+
38+
/**
39+
* A {@link DataHandle} backed by an {@link InputStream} and/or
40+
* {@link OutputStream}. Supports seeking to an arbitrary position within the
41+
* stream.
42+
*
43+
* @author Gabriel Einsdorf
44+
*/
45+
public interface SeekableStreamHandle<L extends Location> extends
46+
ResettableStreamHandle<L>
47+
{
48+
49+
@Override
50+
void seek(long pos) throws IOException;
51+
}

0 commit comments

Comments
 (0)