Skip to content

Commit b73ab8c

Browse files
committed
DataHandle: remove methods and classes refrencing java.nio
The java.nio package is not available on all JVMs so it's use must be optional.
1 parent ca292a9 commit b73ab8c

File tree

5 files changed

+81
-106
lines changed

5 files changed

+81
-106
lines changed

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

Lines changed: 26 additions & 82 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
@@ -38,16 +38,15 @@
3838
import java.io.EOFException;
3939
import java.io.IOException;
4040
import java.io.InputStreamReader;
41-
import java.io.UTFDataFormatException;
4241
import java.nio.ByteBuffer;
43-
import java.nio.ByteOrder;
4442

43+
import org.scijava.io.bytes.BytesOrder;
4544
import org.scijava.plugin.WrapperPlugin;
4645

4746
/**
4847
* A <em>data handle</em> is a plugin which provides both streaming and random
4948
* access to bytes at a {@link Location} (e.g., files or arrays).
50-
*
49+
*
5150
* @author Curtis Rueden
5251
* @see DataHandleInputStream
5352
* @see DataHandleOutputStream
@@ -76,15 +75,15 @@ public interface DataHandle<L extends Location> extends WrapperPlugin<L>,
7675

7776
/**
7877
* Sets the new length of the handle.
79-
*
78+
*
8079
* @param length New length.
8180
* @throws IOException If there is an error changing the handle's length.
8281
*/
8382
void setLength(long length) throws IOException;
8483

8584
/**
86-
* Gets the number of bytes which can be read from, or written to, the
87-
* data handle, bounded by the specified number of bytes.
85+
* Gets the number of bytes which can be read from, or written to, the data
86+
* handle, bounded by the specified number of bytes.
8887
* <p>
8988
* In the case of reading, attempting to read the returned number of bytes is
9089
* guaranteed not to throw {@link EOFException}. However, be aware that the
@@ -106,8 +105,8 @@ public interface DataHandle<L extends Location> extends WrapperPlugin<L>,
106105
* </p>
107106
*
108107
* @param count Desired number of bytes to read/write.
109-
* @return The actual number of bytes which could be read/written,
110-
* which might be less than the requested value.
108+
* @return The actual number of bytes which could be read/written, which might
109+
* be less than the requested value.
111110
* @throws IOException If something goes wrong with the check.
112111
*/
113112
default long available(final long count) throws IOException {
@@ -123,7 +122,7 @@ default long available(final long count) throws IOException {
123122

124123
/**
125124
* Ensures that the handle has sufficient bytes available to read.
126-
*
125+
*
127126
* @param count Number of bytes to read.
128127
* @see #available(long)
129128
* @throws EOFException If there are insufficient bytes available.
@@ -138,7 +137,7 @@ default void ensureReadable(final long count) throws IOException {
138137
/**
139138
* Ensures that the handle has the correct length to be written to, and
140139
* extends it as required.
141-
*
140+
*
142141
* @param count Number of bytes to write.
143142
* @return {@code true} if the handle's length was sufficient, or
144143
* {@code false} if the handle's length required an extension.
@@ -157,42 +156,42 @@ default boolean ensureWritable(final long count) throws IOException {
157156
}
158157

159158
/** Returns the byte order of the stream. */
160-
ByteOrder getOrder();
159+
BytesOrder getOrder();
161160

162161
/**
163162
* Sets the byte order of the stream.
164163
*
165164
* @param order Order to set.
166165
*/
167-
void setOrder(ByteOrder order);
166+
void setOrder(BytesOrder order);
168167

169168
/**
170-
* Returns true iff the stream's order is {@link ByteOrder#BIG_ENDIAN}.
169+
* Returns true iff the stream's order is {@link BytesOrder#BIG_ENDIAN}.
171170
*
172171
* @see #getOrder()
173172
*/
174173
default boolean isBigEndian() {
175-
return getOrder() == ByteOrder.BIG_ENDIAN;
174+
return getOrder() == BytesOrder.BIG_ENDIAN;
176175
}
177176

178177
/**
179-
* Returns true iff the stream's order is {@link ByteOrder#LITTLE_ENDIAN}.
178+
* Returns true iff the stream's order is {@link BytesOrder#LITTLE_ENDIAN}.
180179
*
181180
* @see #getOrder()
182181
*/
183182
default boolean isLittleEndian() {
184-
return getOrder() == ByteOrder.LITTLE_ENDIAN;
183+
return getOrder() == BytesOrder.LITTLE_ENDIAN;
185184
}
186185

187186
/**
188187
* Sets the endianness of the stream.
189188
*
190-
* @param little If true, sets the order to {@link ByteOrder#LITTLE_ENDIAN};
191-
* otherwise, sets the order to {@link ByteOrder#BIG_ENDIAN}.
192-
* @see #setOrder(ByteOrder)
189+
* @param little If true, sets the order to {@link BytesOrder#LITTLE_ENDIAN};
190+
* otherwise, sets the order to {@link BytesOrder#BIG_ENDIAN}.
191+
* @see #setOrder(BytesOrder)
193192
*/
194193
default void setLittleEndian(final boolean little) {
195-
setOrder(little ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN);
194+
setOrder(little ? BytesOrder.LITTLE_ENDIAN : BytesOrder.BIG_ENDIAN);
196195
}
197196

198197
/** Gets the native encoding of the stream. */
@@ -201,62 +200,6 @@ default void setLittleEndian(final boolean little) {
201200
/** Sets the native encoding of the stream. */
202201
void setEncoding(String encoding);
203202

204-
/**
205-
* Reads up to {@code buf.remaining()} bytes of data from the stream into a
206-
* {@link ByteBuffer}.
207-
*/
208-
default int read(final ByteBuffer buf) throws IOException {
209-
return read(buf, buf.remaining());
210-
}
211-
212-
/**
213-
* Reads up to {@code len} bytes of data from the stream into a
214-
* {@link ByteBuffer}.
215-
*
216-
* @return the total number of bytes read into the buffer.
217-
*/
218-
default int read(final ByteBuffer buf, final int len) throws IOException {
219-
final int n;
220-
if (buf.hasArray()) {
221-
// read directly into the array
222-
n = read(buf.array(), buf.arrayOffset(), len);
223-
}
224-
else {
225-
// read into a temporary array, then copy
226-
final byte[] b = new byte[len];
227-
n = read(b);
228-
buf.put(b, 0, n);
229-
}
230-
return n;
231-
}
232-
233-
/**
234-
* Writes {@code buf.remaining()} bytes of data from the given
235-
* {@link ByteBuffer} to the stream.
236-
*/
237-
default void write(final ByteBuffer buf) throws IOException {
238-
write(buf, buf.remaining());
239-
}
240-
241-
/**
242-
* Writes {@code len} bytes of data from the given {@link ByteBuffer} to the
243-
* stream.
244-
*/
245-
default void write(final ByteBuffer buf, final int len)
246-
throws IOException
247-
{
248-
if (buf.hasArray()) {
249-
// write directly from the buffer's array
250-
write(buf.array(), buf.arrayOffset(), len);
251-
}
252-
else {
253-
// copy into a temporary array, then write
254-
final byte[] b = new byte[len];
255-
buf.get(b);
256-
write(b);
257-
}
258-
}
259-
260203
/** Reads a string of arbitrary length, terminated by a null char. */
261204
default String readCString() throws IOException {
262205
final String line = findString("\0");
@@ -273,7 +216,7 @@ default String readString(final int n) throws IOException {
273216

274217
/**
275218
* Reads a string ending with one of the characters in the given string.
276-
*
219+
*
277220
* @see #findString(String...)
278221
*/
279222
default String readString(final String lastChars) throws IOException {
@@ -287,7 +230,7 @@ default String readString(final String lastChars) throws IOException {
287230

288231
/**
289232
* Reads a string ending with one of the given terminating substrings.
290-
*
233+
*
291234
* @param terminators The strings for which to search.
292235
* @return The string from the initial position through the end of the
293236
* terminating sequence, or through the end of the stream if no
@@ -449,12 +392,13 @@ default int read() throws IOException {
449392
*
450393
* @return the total number of bytes read into the buffer.
451394
*/
452-
default int read(byte[] b) throws IOException {
395+
default int read(final byte[] b) throws IOException {
453396
return read(b, 0, b.length);
454397
}
455398

456399
/**
457-
* Reads up to len bytes of data from the stream into an array of bytes.
400+
* Reads up to {@code len} bytes of data from the stream into an array of
401+
* bytes.
458402
*
459403
* @return the total number of bytes read into the buffer.
460404
*/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.bytes;
33+
34+
/**
35+
* @author gabriel
36+
*/
37+
public enum BytesOrder {
38+
LITTLE_ENDIAN, BIG_ENDIAN
39+
}

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

Lines changed: 6 additions & 7 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
@@ -31,10 +31,9 @@
3131

3232
package org.scijava.io.location;
3333

34-
import java.nio.ByteOrder;
35-
3634
import org.scijava.io.DataHandle;
3735
import org.scijava.io.Location;
36+
import org.scijava.io.bytes.BytesOrder;
3837
import org.scijava.plugin.AbstractWrapperPlugin;
3938

4039
/**
@@ -48,18 +47,18 @@ public abstract class AbstractDataHandle<L extends Location> extends
4847

4948
// -- Fields --
5049

51-
private ByteOrder order = ByteOrder.BIG_ENDIAN;
50+
private BytesOrder order = BytesOrder.BIG_ENDIAN;
5251
private String encoding = "UTF-8";
5352

5453
// -- DataHandle methods --
5554

5655
@Override
57-
public ByteOrder getOrder() {
56+
public BytesOrder getOrder() {
5857
return order;
5958
}
6059

6160
@Override
62-
public void setOrder(final ByteOrder order) {
61+
public void setOrder(final BytesOrder order) {
6362
this.order = order;
6463
}
6564

src/test/java/org/scijava/ContextCreationTest.java

Lines changed: 4 additions & 4 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
@@ -55,7 +55,7 @@
5555

5656
/**
5757
* Tests {@link Context} creation with {@link Service} dependencies.
58-
*
58+
*
5959
* @author Curtis Rueden
6060
*/
6161
public class ContextCreationTest {
@@ -97,8 +97,8 @@ public void testFull() {
9797
org.scijava.input.DefaultInputService.class,
9898
org.scijava.io.DefaultDataHandleService.class,
9999
org.scijava.io.DefaultIOService.class,
100-
org.scijava.io.nio.DefaultNIOService.class,
101100
org.scijava.io.DefaultRecentFileService.class,
101+
org.scijava.io.nio.DefaultNIOService.class,
102102
org.scijava.main.DefaultMainService.class,
103103
org.scijava.menu.DefaultMenuService.class,
104104
org.scijava.module.DefaultModuleService.class,

0 commit comments

Comments
 (0)