Skip to content

Commit 9f98e17

Browse files
committed
Add methods for raw I/O with primitive arrays
1 parent eeabf09 commit 9f98e17

1 file changed

Lines changed: 91 additions & 0 deletions

File tree

  • scijava/scijava-testutil/src/main/java/org/scijava/testutil
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package org.scijava.testutil;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.io.OutputStream;
6+
import java.nio.ByteBuffer;
7+
import java.nio.DoubleBuffer;
8+
import java.nio.FloatBuffer;
9+
import java.nio.IntBuffer;
10+
import java.nio.LongBuffer;
11+
import java.nio.ShortBuffer;
12+
13+
/**
14+
* Utility functions for reading and writing arrays simply.
15+
*
16+
* @author Curtis Rueden
17+
*/
18+
public final class ArrayIO {
19+
20+
private ArrayIO() {
21+
// NB: Prevent instantiation of utility class.
22+
}
23+
24+
public static byte[] bytes(final InputStream is) throws IOException {
25+
return is.readAllBytes();
26+
}
27+
28+
public static double[] doubles(final InputStream is) throws IOException {
29+
final ByteBuffer bb = ByteBuffer.wrap(is.readAllBytes());
30+
final DoubleBuffer view = bb.asDoubleBuffer();
31+
return DoubleBuffer.allocate(view.limit()).put(view).array();
32+
}
33+
34+
public static float[] floats(final InputStream is) throws IOException {
35+
final ByteBuffer bb = ByteBuffer.wrap(is.readAllBytes());
36+
final FloatBuffer view = bb.asFloatBuffer();
37+
return FloatBuffer.allocate(view.limit()).put(view).array();
38+
}
39+
40+
public static int[] ints(final InputStream is) throws IOException {
41+
final ByteBuffer bb = ByteBuffer.wrap(is.readAllBytes());
42+
final IntBuffer view = bb.asIntBuffer();
43+
return IntBuffer.allocate(view.limit()).put(view).array();
44+
}
45+
46+
public static long[] longs(final InputStream is) throws IOException {
47+
final ByteBuffer bb = ByteBuffer.wrap(is.readAllBytes());
48+
final LongBuffer view = bb.asLongBuffer();
49+
return LongBuffer.allocate(view.limit()).put(view).array();
50+
}
51+
52+
public static short[] shorts(final InputStream is) throws IOException {
53+
final ByteBuffer bb = ByteBuffer.wrap(is.readAllBytes());
54+
final ShortBuffer view = bb.asShortBuffer();
55+
return ShortBuffer.allocate(view.limit()).put(view).array();
56+
}
57+
58+
public static void writeBytes(final byte[] data, final OutputStream os) throws IOException {
59+
os.write(data);
60+
}
61+
62+
public static void writeDoubles(final double[] data, final OutputStream os) throws IOException {
63+
final ByteBuffer bb = ByteBuffer.allocate(4 * data.length);
64+
bb.asDoubleBuffer().put(DoubleBuffer.wrap(data));
65+
os.write(bb.array());
66+
}
67+
68+
public static void writeFloats(final float[] data, final OutputStream os) throws IOException {
69+
final ByteBuffer bb = ByteBuffer.allocate(4 * data.length);
70+
bb.asFloatBuffer().put(FloatBuffer.wrap(data));
71+
os.write(bb.array());
72+
}
73+
74+
public static void writeInts(final int[] data, final OutputStream os) throws IOException {
75+
final ByteBuffer bb = ByteBuffer.allocate(4 * data.length);
76+
bb.asIntBuffer().put(IntBuffer.wrap(data));
77+
os.write(bb.array());
78+
}
79+
80+
public static void writeLongs(final long[] data, final OutputStream os) throws IOException {
81+
final ByteBuffer bb = ByteBuffer.allocate(8 * data.length);
82+
bb.asLongBuffer().put(LongBuffer.wrap(data));
83+
os.write(bb.array());
84+
}
85+
86+
public static void writeShorts(final short[] data, final OutputStream os) throws IOException {
87+
final ByteBuffer bb = ByteBuffer.allocate(4 * data.length);
88+
bb.asShortBuffer().put(ShortBuffer.wrap(data));
89+
os.write(bb.array());
90+
}
91+
}

0 commit comments

Comments
 (0)