Skip to content

Commit e7077bf

Browse files
committed
Add AbstractCompressedHandle: an abstract superclass for compressed handles
1 parent fa26949 commit e7077bf

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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.location;
33+
34+
import java.io.IOException;
35+
import java.io.InputStream;
36+
import java.io.OutputStream;
37+
38+
import org.scijava.io.handle.AbstractStreamHandle;
39+
import org.scijava.io.handle.DataHandle;
40+
import org.scijava.io.handle.DataHandleService;
41+
import org.scijava.io.handle.ResettableStreamHandle;
42+
import org.scijava.plugin.Parameter;
43+
44+
/**
45+
* Abstract superclass for {@link DataHandle}s that operate on compressed data.
46+
*
47+
* @author Gabriel Einsdorf
48+
*/
49+
public abstract class AbstractCompressedHandle<L extends AbstractHigherOrderLocation>
50+
extends AbstractStreamHandle<L> implements ResettableStreamHandle<L>
51+
{
52+
53+
private DataHandle<Location> rawHandle;
54+
protected InputStream inputStream;
55+
56+
@Parameter
57+
private DataHandleService dataHandleService;
58+
59+
public AbstractCompressedHandle() {
60+
super();
61+
}
62+
63+
@Override
64+
public void resetStream() throws IOException {
65+
66+
if (raw() instanceof ResettableStreamHandle<?>) {
67+
((ResettableStreamHandle<Location>) rawHandle).resetStream();
68+
}
69+
else {
70+
rawHandle.seek(0);
71+
}
72+
initInputStream();
73+
}
74+
75+
@Override
76+
public InputStream in() throws IOException {
77+
if (inputStream == null) {
78+
initInputStream();
79+
}
80+
return inputStream;
81+
}
82+
83+
@Override
84+
public long skip(long n) throws IOException {
85+
long skipped = in().skip(n);
86+
setOffset(offset() + skipped);
87+
return skipped;
88+
}
89+
90+
protected abstract void initInputStream() throws IOException;
91+
92+
@Override
93+
public OutputStream out() throws IOException {
94+
return null;
95+
}
96+
97+
@Override
98+
public boolean isWritable() {
99+
return false;
100+
}
101+
102+
@Override
103+
public boolean isReadable() {
104+
return true;
105+
}
106+
107+
@Override
108+
public long length() throws IOException {
109+
return raw().length();
110+
}
111+
112+
@Override
113+
public boolean exists() throws IOException {
114+
return raw().exists();
115+
}
116+
117+
@Override
118+
public void setLength(long length) throws IOException {
119+
throw new IOException("This handle " + this.getClass().getSimpleName() +
120+
" is read-only!");
121+
}
122+
123+
/**
124+
* @return the raw underlying DataHandle (not decompressed)
125+
*/
126+
protected DataHandle<Location> raw() {
127+
if (rawHandle == null) {
128+
rawHandle = dataHandleService.create(get().getBaseLocation());
129+
}
130+
return rawHandle;
131+
}
132+
133+
}

0 commit comments

Comments
 (0)