Skip to content

Commit 8b065ba

Browse files
gab1onectrueden
authored andcommitted
DataHandle: remove built-in references to java.nio
The java.nio package is not available on all JVMs, so its use must be optional on top of the base API layer.
1 parent 4251229 commit 8b065ba

File tree

3 files changed

+5
-70
lines changed

3 files changed

+5
-70
lines changed

src/main/java/org/scijava/io/handle/AbstractDataHandle.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131

3232
package org.scijava.io.handle;
3333

34-
import java.nio.ByteOrder;
35-
3634
import org.scijava.io.location.Location;
3735
import org.scijava.plugin.AbstractWrapperPlugin;
3836

src/main/java/org/scijava/io/handle/DataHandle.java

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
import java.io.EOFException;
3939
import java.io.IOException;
4040
import java.io.InputStreamReader;
41-
import java.nio.ByteBuffer;
42-
import java.nio.ByteOrder;
4341

4442
import org.scijava.io.location.Location;
4543
import org.scijava.plugin.WrapperPlugin;
@@ -56,6 +54,10 @@ public interface DataHandle<L extends Location> extends WrapperPlugin<L>,
5654
DataInput, DataOutput, Closeable
5755
{
5856

57+
public enum ByteOrder {
58+
LITTLE_ENDIAN, BIG_ENDIAN
59+
}
60+
5961
/** Default block size to use when searching through the stream. */
6062
int DEFAULT_BLOCK_SIZE = 256 * 1024; // 256 KB
6163

@@ -92,8 +94,6 @@ public interface DataHandle<L extends Location> extends WrapperPlugin<L>,
9294
* by this method:
9395
* </p>
9496
* <ul>
95-
* <li>{@link #read(ByteBuffer)}</li>
96-
* <li>{@link #read(ByteBuffer, int)}</li>
9797
* <li>{@link #read(byte[])}</li>
9898
* <li>{@link #read(byte[], int, int)}</li>
9999
* <li>{@link #skip(long)}</li>
@@ -191,62 +191,6 @@ default void setLittleEndian(final boolean little) {
191191
/** Sets the native encoding of the stream. */
192192
void setEncoding(String encoding);
193193

194-
/**
195-
* Reads up to {@code buf.remaining()} bytes of data from the stream into a
196-
* {@link ByteBuffer}.
197-
*/
198-
default int read(final ByteBuffer buf) throws IOException {
199-
return read(buf, buf.remaining());
200-
}
201-
202-
/**
203-
* Reads up to {@code len} bytes of data from the stream into a
204-
* {@link ByteBuffer}.
205-
*
206-
* @return the total number of bytes read into the buffer.
207-
*/
208-
default int read(final ByteBuffer buf, final int len) throws IOException {
209-
final int n;
210-
if (buf.hasArray()) {
211-
// read directly into the array
212-
n = read(buf.array(), buf.arrayOffset(), len);
213-
}
214-
else {
215-
// read into a temporary array, then copy
216-
final byte[] b = new byte[len];
217-
n = read(b);
218-
buf.put(b, 0, n);
219-
}
220-
return n;
221-
}
222-
223-
/**
224-
* Writes {@code buf.remaining()} bytes of data from the given
225-
* {@link ByteBuffer} to the stream.
226-
*/
227-
default void write(final ByteBuffer buf) throws IOException {
228-
write(buf, buf.remaining());
229-
}
230-
231-
/**
232-
* Writes {@code len} bytes of data from the given {@link ByteBuffer} to the
233-
* stream.
234-
*/
235-
default void write(final ByteBuffer buf, final int len)
236-
throws IOException
237-
{
238-
if (buf.hasArray()) {
239-
// write directly from the buffer's array
240-
write(buf.array(), buf.arrayOffset(), len);
241-
}
242-
else {
243-
// copy into a temporary array, then write
244-
final byte[] b = new byte[len];
245-
buf.get(b);
246-
write(b);
247-
}
248-
}
249-
250194
/** Reads a string of arbitrary length, terminated by a null char. */
251195
default String readCString() throws IOException {
252196
final String line = findString("\0");

src/test/java/org/scijava/io/handle/DataHandleTest.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,12 @@
3535

3636
import java.io.IOException;
3737
import java.io.OutputStream;
38-
import java.nio.ByteBuffer;
39-
import java.nio.ByteOrder;
4038
import java.util.Arrays;
4139

4240
import org.junit.Test;
4341
import org.scijava.Context;
4442
import org.scijava.io.handle.DataHandle;
43+
import org.scijava.io.handle.DataHandle.ByteOrder;
4544
import org.scijava.io.handle.DataHandleService;
4645
import org.scijava.io.location.Location;
4746
import org.scijava.util.Bytes;
@@ -113,12 +112,6 @@ protected <L extends Location> void checkReads(final DataHandle<L> handle)
113112
handle.seek(1);
114113
assertBytesMatch(1, handle.read(buf), buf);
115114

116-
// test read(ByteBuffer)
117-
Arrays.fill(buf, (byte) 0);
118-
final ByteBuffer byteBuffer = ByteBuffer.wrap(buf);
119-
handle.seek(2);
120-
assertBytesMatch(2, handle.read(byteBuffer), byteBuffer.array());
121-
122115
// test readByte()
123116
handle.seek(0);
124117
for (int i = 0; i < BYTES.length; i++) {

0 commit comments

Comments
 (0)