Skip to content

Commit 433849c

Browse files
committed
Add BytesHandle for working with BytesLocations
1 parent b459699 commit 433849c

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2016 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.handle;
33+
34+
import java.io.EOFException;
35+
import java.io.IOException;
36+
37+
import org.scijava.io.ByteBank;
38+
import org.scijava.io.location.BytesLocation;
39+
import org.scijava.plugin.Plugin;
40+
41+
/**
42+
* {@link DataHandle} for a {@link BytesLocation}.
43+
*
44+
* @author Curtis Rueden
45+
* @author Melissa Linkert
46+
* @author Gabriel Einsdorf
47+
*/
48+
@Plugin(type = DataHandle.class)
49+
public class BytesHandle extends AbstractDataHandle<BytesLocation> {
50+
51+
private long offset = 0;
52+
53+
// -- DataHandle methods --
54+
55+
@Override
56+
public long offset() {
57+
return offset;
58+
}
59+
60+
@Override
61+
public long length() {
62+
return bytes().getMaxPos();
63+
}
64+
65+
@Override
66+
public void setLength(final long length) throws IOException {
67+
// check if new length is legal
68+
bytes().basicRangeCheck(0, length);
69+
// TODO update the maxLength?
70+
}
71+
72+
@Override
73+
public int read(final byte[] b, final int off, int len) throws IOException {
74+
if (offset + len > length()) {
75+
len = (int) (length() - offset);
76+
}
77+
bytes().getBytes(offset, b, off, len);
78+
offset += len;
79+
return len;
80+
}
81+
82+
@Override
83+
public void seek(final long pos) throws IOException {
84+
if (pos > length()) setLength(pos);
85+
offset = pos;
86+
}
87+
88+
// -- DataInput methods --
89+
90+
@Override
91+
public byte readByte() throws IOException {
92+
ensureReadable(1);
93+
try {
94+
// we need to convert the bytes into the range 0-255
95+
return bytes().getByte(offset++);
96+
}
97+
catch (final Exception e) {
98+
throw eofException(e);
99+
}
100+
}
101+
102+
@Override
103+
public void readFully(final byte[] b, final int off, final int len)
104+
throws IOException
105+
{
106+
ensureReadable(len);
107+
try {
108+
bytes().getBytes(offset, b, off, len);
109+
offset += len;
110+
}
111+
catch (final Exception e) {
112+
throw eofException(e);
113+
}
114+
}
115+
116+
// -- DataOutput methods --
117+
118+
@Override
119+
public void write(final byte[] b, final int off, final int len)
120+
throws IOException
121+
{
122+
ensureWritable(len);
123+
bytes().setBytes(offset, b, off, len);
124+
offset += len;
125+
}
126+
127+
@Override
128+
public void write(final int b) throws IOException {
129+
ensureWritable(1);
130+
bytes().setByte(offset, (byte) b);
131+
offset++;
132+
}
133+
134+
// -- Closeable methods --
135+
136+
@Override
137+
public void close() {
138+
// NB: No action needed.
139+
}
140+
141+
// -- Typed methods --
142+
143+
@Override
144+
public Class<BytesLocation> getType() {
145+
return BytesLocation.class;
146+
}
147+
148+
// -- Helper methods --
149+
150+
private ByteBank bytes() {
151+
return get().getByteBank();
152+
}
153+
154+
private EOFException eofException(final Throwable cause) {
155+
final EOFException eof = new EOFException();
156+
eof.initCause(cause);
157+
return eof;
158+
}
159+
160+
}

0 commit comments

Comments
 (0)