Skip to content

Commit 6c87eb0

Browse files
committed
DataHandle: add more default method impls
1 parent e36a8fa commit 6c87eb0

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

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

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,68 @@ default int readUnsignedByte() throws IOException {
442442
return readByte() & 0xff;
443443
}
444444

445+
@Override
446+
default short readShort() throws IOException {
447+
final int ch1 = read();
448+
final int ch2 = read();
449+
if ((ch1 | ch2) < 0) throw new EOFException();
450+
return (short) ((ch1 << 8) + (ch2 << 0));
451+
}
452+
445453
@Override
446454
default int readUnsignedShort() throws IOException {
447455
return readShort() & 0xffff;
448456
}
449457

458+
@Override
459+
default char readChar() throws IOException {
460+
return (char) readShort();
461+
}
462+
463+
@Override
464+
default int readInt() throws IOException {
465+
int ch1 = read();
466+
int ch2 = read();
467+
int ch3 = read();
468+
int ch4 = read();
469+
if ((ch1 | ch2 | ch3 | ch4) < 0) throw new EOFException();
470+
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
471+
}
472+
473+
@Override
474+
default long readLong() throws IOException {
475+
int ch1 = read();
476+
int ch2 = read();
477+
int ch3 = read();
478+
int ch4 = read();
479+
int ch5 = read();
480+
int ch6 = read();
481+
int ch7 = read();
482+
int ch8 = read();
483+
if ((ch1 | ch2 | ch3 | ch4 | ch5 | ch6 | ch7 | ch8) < 0) {
484+
throw new EOFException();
485+
}
486+
// TODO: Double check this inconsistent code.
487+
return ((long) ch1 << 56) + //
488+
((long) (ch2 & 255) << 48) + //
489+
((long) (ch3 & 255) << 40) + //
490+
((long) (ch4 & 255) << 32) + //
491+
((long) (ch5 & 255) << 24) + //
492+
((ch6 & 255) << 16) + //
493+
((ch7 & 255) << 8) + //
494+
((ch8 & 255) << 0);
495+
}
496+
497+
@Override
498+
default float readFloat() throws IOException {
499+
return Float.intBitsToFloat(readInt());
500+
}
501+
502+
@Override
503+
default double readDouble() throws IOException {
504+
return Double.longBitsToDouble(readLong());
505+
}
506+
450507
@Override
451508
default String readLine() throws IOException {
452509
// NB: Code adapted from java.io.RandomAccessFile.readLine().
@@ -511,6 +568,42 @@ default void writeByte(final int v) throws IOException {
511568
write(v);
512569
}
513570

571+
@Override
572+
default void writeChar(final int v) throws IOException {
573+
write((v >>> 8) & 0xFF);
574+
write((v >>> 0) & 0xFF);
575+
}
576+
577+
@Override
578+
default void writeInt(final int v) throws IOException {
579+
write((v >>> 24) & 0xFF);
580+
write((v >>> 16) & 0xFF);
581+
write((v >>> 8) & 0xFF);
582+
write((v >>> 0) & 0xFF);
583+
}
584+
585+
@Override
586+
default void writeLong(final long v) throws IOException {
587+
write((byte) (v >>> 56));
588+
write((byte) (v >>> 48));
589+
write((byte) (v >>> 40));
590+
write((byte) (v >>> 32));
591+
write((byte) (v >>> 24));
592+
write((byte) (v >>> 16));
593+
write((byte) (v >>> 8));
594+
write((byte) (v >>> 0));
595+
}
596+
597+
@Override
598+
default void writeFloat(final float v) throws IOException {
599+
writeInt(Float.floatToIntBits(v));
600+
}
601+
602+
@Override
603+
default void writeDouble(final double v) throws IOException {
604+
writeLong(Double.doubleToLongBits(v));
605+
}
606+
514607
@Override
515608
default void writeBytes(final String s) throws IOException {
516609
write(s.getBytes("UTF-8"));

0 commit comments

Comments
 (0)