Skip to content

Commit ebca914

Browse files
committed
Add DataHandle implementation for FileLocations
This is a redesigned version of SCIFIO's old FileHandle class. It delegates to java.io.RandomAccessFile for the bulk of its implementation.
1 parent 0686301 commit ebca914

File tree

1 file changed

+285
-0
lines changed

1 file changed

+285
-0
lines changed
Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2015 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.RandomAccessFile;
36+
37+
import org.scijava.plugin.Plugin;
38+
39+
/**
40+
* {@link DataHandle} for a {@link FileLocation}.
41+
*
42+
* @author Curtis Rueden
43+
*/
44+
@Plugin(type = DataHandle.class)
45+
public class FileHandle extends AbstractDataHandle<FileLocation> {
46+
47+
// -- Fields --
48+
49+
/** The {@link RandomAccessFile} backing this file handle. */
50+
private RandomAccessFile raf;
51+
52+
/** The mode of the {@link RandomAccessFile}. */
53+
private String mode = "rw";
54+
55+
// -- FileHandle methods --
56+
57+
/** Gets the random access file object backing this FileHandle. */
58+
public RandomAccessFile getRandomAccessFile() throws IOException {
59+
return raf();
60+
}
61+
62+
public String getMode() {
63+
return mode;
64+
}
65+
66+
public void setMode(final String mode) {
67+
if (raf != null) {
68+
throw new IllegalStateException("File already initialized");
69+
}
70+
this.mode = mode;
71+
}
72+
73+
// -- DataHandle methods --
74+
75+
@Override
76+
public long offset() throws IOException {
77+
return raf().getFilePointer();
78+
}
79+
80+
@Override
81+
public long length() throws IOException {
82+
return raf().length();
83+
}
84+
85+
@Override
86+
public int read() throws IOException {
87+
return raf().read();
88+
}
89+
90+
@Override
91+
public int read(final byte[] b) throws IOException {
92+
return raf().read(b);
93+
}
94+
95+
@Override
96+
public int read(final byte[] b, final int off, final int len)
97+
throws IOException
98+
{
99+
return raf().read(b, off, len);
100+
}
101+
102+
@Override
103+
public void seek(final long pos) throws IOException {
104+
raf().seek(pos);
105+
}
106+
107+
// -- DataInput methods --
108+
109+
@Override
110+
public boolean readBoolean() throws IOException {
111+
return raf().readBoolean();
112+
}
113+
114+
@Override
115+
public byte readByte() throws IOException {
116+
return raf().readByte();
117+
}
118+
119+
@Override
120+
public char readChar() throws IOException {
121+
return raf().readChar();
122+
}
123+
124+
@Override
125+
public double readDouble() throws IOException {
126+
return raf().readDouble();
127+
}
128+
129+
@Override
130+
public float readFloat() throws IOException {
131+
return raf().readFloat();
132+
}
133+
134+
@Override
135+
public void readFully(final byte[] b) throws IOException {
136+
raf().readFully(b);
137+
}
138+
139+
@Override
140+
public void readFully(final byte[] b, final int off, final int len)
141+
throws IOException
142+
{
143+
raf().readFully(b, off, len);
144+
}
145+
146+
@Override
147+
public int readInt() throws IOException {
148+
return raf().readInt();
149+
}
150+
151+
@Override
152+
public String readLine() throws IOException {
153+
return raf().readLine();
154+
}
155+
156+
@Override
157+
public long readLong() throws IOException {
158+
return raf().readLong();
159+
}
160+
161+
@Override
162+
public short readShort() throws IOException {
163+
return raf().readShort();
164+
}
165+
166+
@Override
167+
public int readUnsignedByte() throws IOException {
168+
return raf().readUnsignedByte();
169+
}
170+
171+
@Override
172+
public int readUnsignedShort() throws IOException {
173+
return raf().readUnsignedShort();
174+
}
175+
176+
@Override
177+
public String readUTF() throws IOException {
178+
return raf().readUTF();
179+
}
180+
181+
@Override
182+
public int skipBytes(final int n) throws IOException {
183+
return raf().skipBytes(n);
184+
}
185+
186+
// -- DataOutput methods --
187+
188+
@Override
189+
public void write(final byte[] b) throws IOException {
190+
raf().write(b);
191+
}
192+
193+
@Override
194+
public void write(final byte[] b, final int off, final int len)
195+
throws IOException
196+
{
197+
raf().write(b, off, len);
198+
}
199+
200+
@Override
201+
public void write(final int b) throws IOException {
202+
raf().write(b);
203+
}
204+
205+
@Override
206+
public void writeBoolean(final boolean v) throws IOException {
207+
raf().writeBoolean(v);
208+
}
209+
210+
@Override
211+
public void writeByte(final int v) throws IOException {
212+
raf().writeByte(v);
213+
}
214+
215+
@Override
216+
public void writeBytes(final String s) throws IOException {
217+
raf().writeBytes(s);
218+
}
219+
220+
@Override
221+
public void writeChar(final int v) throws IOException {
222+
raf().writeChar(v);
223+
}
224+
225+
@Override
226+
public void writeChars(final String s) throws IOException {
227+
raf().writeChars(s);
228+
}
229+
230+
@Override
231+
public void writeDouble(final double v) throws IOException {
232+
raf().writeDouble(v);
233+
}
234+
235+
@Override
236+
public void writeFloat(final float v) throws IOException {
237+
raf().writeFloat(v);
238+
}
239+
240+
@Override
241+
public void writeInt(final int v) throws IOException {
242+
raf().writeInt(v);
243+
}
244+
245+
@Override
246+
public void writeLong(final long v) throws IOException {
247+
raf().writeLong(v);
248+
}
249+
250+
@Override
251+
public void writeShort(final int v) throws IOException {
252+
raf().writeShort(v);
253+
}
254+
255+
@Override
256+
public void writeUTF(final String str) throws IOException {
257+
raf().writeUTF(str);
258+
}
259+
260+
// -- Closeable methods --
261+
262+
@Override
263+
public void close() throws IOException {
264+
raf().close();
265+
}
266+
267+
// -- Typed methods --
268+
269+
@Override
270+
public Class<FileLocation> getType() {
271+
return FileLocation.class;
272+
}
273+
274+
// -- Helper methods --
275+
276+
private RandomAccessFile raf() throws IOException {
277+
if (raf == null) initRAF();
278+
return raf;
279+
}
280+
281+
private synchronized void initRAF() throws IOException {
282+
raf = new RandomAccessFile(get().getFile(), getMode());
283+
}
284+
285+
}

0 commit comments

Comments
 (0)