Skip to content

Commit ca292a9

Browse files
committed
Base Bytes{Handle|Location} on ByteBank
They are now called ByteBankHandle and ByteBankLocation
1 parent a1f65ae commit ca292a9

File tree

5 files changed

+258
-375
lines changed

5 files changed

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

src/main/java/org/scijava/io/location/bytes/BytesLocation.java renamed to src/main/java/org/scijava/io/location/bytebank/ByteBankLocation.java

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
* %%
99
* Redistribution and use in source and binary forms, with or without
1010
* modification, are permitted provided that the following conditions are met:
11-
*
11+
*
1212
* 1. Redistributions of source code must retain the above copyright notice,
1313
* this list of conditions and the following disclaimer.
1414
* 2. Redistributions in binary form must reproduce the above copyright notice,
1515
* this list of conditions and the following disclaimer in the documentation
1616
* and/or other materials provided with the distribution.
17-
*
17+
*
1818
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1919
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2020
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -29,58 +29,79 @@
2929
* #L%
3030
*/
3131

32-
package org.scijava.io.location.bytes;
32+
package org.scijava.io.location.bytebank;
3333

3434
import java.nio.ByteBuffer;
3535

3636
import org.scijava.io.Location;
37+
import org.scijava.io.bytes.ByteArrayByteBank;
38+
import org.scijava.io.bytes.ByteBank;
3739
import org.scijava.io.location.AbstractLocation;
40+
import org.scijava.util.ByteArray;
3841

3942
/**
40-
* {@link Location} backed by a {@link ByteBuffer}.
41-
* <p>
42-
* Note that the backing {@link ByteBuffer} reference is mutable, and might
43-
* change to a different, larger buffer if a sufficiently large amount of data
44-
* is written to this location via a wrapping {@link BytesHandle}.
45-
* </p>
46-
*
43+
* {@link Location} backed by a {@link ByteBank}.
44+
*
4745
* @author Curtis Rueden
48-
* @see BytesHandle
46+
* @author Gabriel Einsdorf
47+
* @see ByteBankHandle
4948
*/
50-
public class BytesLocation extends AbstractLocation {
49+
public class ByteBankLocation extends AbstractLocation {
5150

52-
private ByteBuffer bytes;
51+
private ByteBank byteBank;
5352

54-
public BytesLocation(final ByteBuffer bytes) {
55-
this.bytes = bytes;
53+
/**
54+
* Creates a {@link ByteBankLocation} backed by the specified
55+
* {@link ByteBank}.
56+
*
57+
* @param bytes the {@link ByteBank} that will back this {@link Location}
58+
*/
59+
public ByteBankLocation(final ByteBank bytes) {
60+
this.byteBank = bytes;
61+
}
62+
63+
/**
64+
* Creates a {@link ByteBankLocation} backed by a {@link ByteArrayByteBank}
65+
* with the specified initial capacity.
66+
*/
67+
public ByteBankLocation(final int initialCapacity) {
68+
this.byteBank = new ByteArrayByteBank(initialCapacity);
5669
}
5770

58-
public BytesLocation(final byte[] bytes) {
59-
this(ByteBuffer.wrap(bytes));
71+
/**
72+
* Creates a {@link ByteBankLocation} backed by a {@link ByteArrayByteBank}
73+
* that wraps the specified {@link ByteArray}.
74+
*/
75+
public ByteBankLocation(final ByteArray bytes) {
76+
this.byteBank = new ByteArrayByteBank(bytes);
6077
}
6178

62-
public BytesLocation(final byte[] bytes, final int offset, final int length) {
63-
this(ByteBuffer.wrap(bytes, offset, length));
79+
/**
80+
* Creates a {@link ByteBankLocation} backed by a {@link ByteArrayByteBank}
81+
* which wraps the specified array.
82+
*
83+
* @param bytes the array to wrap
84+
*/
85+
public ByteBankLocation(final byte[] bytes) {
86+
byteBank = new ByteArrayByteBank(bytes);
6487
}
6588

6689
/**
67-
* Creates a {@link BytesLocation} backed by a {@link ByteBuffer} with the
90+
* Creates a {@link ByteBankLocation} backed by a {@link ByteBuffer} with the
6891
* specified initial capacity.
6992
*/
70-
public BytesLocation(final int initialCapacity) {
71-
this(ByteBuffer.allocate(initialCapacity));
93+
public ByteBankLocation(final byte[] bytes, final int offset,
94+
final int length)
95+
{
96+
byteBank = new ByteArrayByteBank(length);
97+
byteBank.setBytes(0l, bytes, offset, length);
7298
}
7399

74100
// -- BytesLocation methods --
75101

76-
/** Gets the associated {@link ByteBuffer}. */
77-
public ByteBuffer getByteBuffer() {
78-
return bytes;
79-
}
80-
81-
/** Sets the associated {@link ByteBuffer}. */
82-
public void setByteBuffer(final ByteBuffer bytes) {
83-
this.bytes = bytes;
102+
/** Gets the backing {@link ByteBank}. */
103+
public ByteBank getByteBank() {
104+
return byteBank;
84105
}
85106

86107
@Override

0 commit comments

Comments
 (0)