Skip to content

Commit 7b7e03b

Browse files
committed
DataHandle: Use persistent buffer for conversions.
- this avoids allocating a new byte array for each read
1 parent d2ff6ce commit 7b7e03b

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ public abstract class AbstractDataHandle<L extends Location> extends
4444
AbstractWrapperPlugin<L> implements DataHandle<L>
4545
{
4646

47+
private byte[] conversionBuffer = new byte[8];
48+
49+
@Override
50+
public byte[] conversionBuffer() {
51+
return conversionBuffer;
52+
}
53+
4754
// -- Fields --
4855

4956
private ByteOrder order = ByteOrder.BIG_ENDIAN;

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@ default void setLittleEndian(final boolean little) {
250250
/** Sets the native encoding of the stream. */
251251
void setEncoding(String encoding);
252252

253+
/**
254+
* @return a 8 byte long buffer array used for type conversions
255+
*/
256+
byte[] conversionBuffer();
257+
253258
/** Reads a string of arbitrary length, terminated by a null char. */
254259
default String readCString() throws IOException {
255260
final String line = findString("\0");
@@ -515,7 +520,7 @@ default int readUnsignedByte() throws IOException {
515520

516521
@Override
517522
default short readShort() throws IOException {
518-
final byte[] buf = new byte[2];
523+
final byte[] buf = conversionBuffer();
519524
final int read = read(buf, 0, 2);
520525
if (read < 2) throw new EOFException();
521526
return Bytes.toShort(buf, isLittleEndian());
@@ -533,17 +538,17 @@ default char readChar() throws IOException {
533538

534539
@Override
535540
default int readInt() throws IOException {
536-
final byte[] buf = new byte[4];
541+
final byte[] buf = conversionBuffer();
537542
final int read = read(buf, 0, 4);
538543
if (read < 4) throw new EOFException();
539544
return Bytes.toInt(buf, isLittleEndian());
540545
}
541546

542547
@Override
543548
default long readLong() throws IOException {
544-
byte[] buf = new byte[8];
545-
int read = read(buf, 0, 8);
546-
if (read < 0) {
549+
final byte[] buf = conversionBuffer();
550+
final int read = read(buf, 0, 8);
551+
if (read < 8) {
547552
throw new EOFException();
548553
}
549554
return Bytes.toLong(buf, isLittleEndian());
@@ -614,32 +619,42 @@ default void writeByte(final int v) throws IOException {
614619

615620
@Override
616621
default void writeShort(final int v) throws IOException {
617-
write(Bytes.fromShort((short) v, isLittleEndian()));
622+
final byte[] buf = conversionBuffer();
623+
Bytes.unpack(v, buf, 0, 2, isLittleEndian());
624+
write(buf, 0, 2);
618625
}
619626

620627
@Override
621628
default void writeChar(final int v) throws IOException {
622-
write(Bytes.fromShort((short) v, isLittleEndian()));
629+
writeShort(v);
623630
}
624631

625632
@Override
626633
default void writeInt(final int v) throws IOException {
627-
write(Bytes.fromInt(v, isLittleEndian()));
634+
final byte[] buf = conversionBuffer();
635+
Bytes.unpack(v, buf, 0, 4, isLittleEndian());
636+
write(buf, 0, 4);
628637
}
629638

630639
@Override
631640
default void writeLong(final long v) throws IOException {
632-
write(Bytes.fromLong(v, isLittleEndian()));
641+
final byte[] buf = conversionBuffer();
642+
Bytes.unpack(v, buf, 0, 8, isLittleEndian());
643+
write(buf, 0, 8);
633644
}
634645

635646
@Override
636647
default void writeFloat(final float v) throws IOException {
637-
write(Bytes.fromFloat(v, isLittleEndian()));
648+
final byte[] buf = conversionBuffer();
649+
Bytes.unpack(Float.floatToIntBits(v), buf, 0, 4, isLittleEndian());
650+
write(buf, 0, 4);
638651
}
639652

640653
@Override
641654
default void writeDouble(final double v) throws IOException {
642-
write(Bytes.fromDouble(v, isLittleEndian()));
655+
final byte[] buf = conversionBuffer();
656+
Bytes.unpack(Double.doubleToLongBits(v), buf, 0, 8, isLittleEndian());
657+
write(buf, 0, 8);
643658
}
644659

645660
@Override

0 commit comments

Comments
 (0)