Skip to content

Commit 9c0a1da

Browse files
gab1onectrueden
authored andcommitted
DataHandle: make reads & writes respect endianness
1 parent 82ccbdb commit 9c0a1da

File tree

1 file changed

+115
-43
lines changed

1 file changed

+115
-43
lines changed

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

Lines changed: 115 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -456,10 +456,18 @@ default int readUnsignedByte() throws IOException {
456456

457457
@Override
458458
default short readShort() throws IOException {
459-
final int ch1 = read();
460-
final int ch2 = read();
461-
if ((ch1 | ch2) < 0) throw new EOFException();
462-
return (short) ((ch1 << 8) + (ch2 << 0));
459+
final int ch0;
460+
final int ch1;
461+
if (isBigEndian()) {
462+
ch0 = read();
463+
ch1 = read();
464+
}
465+
else {
466+
ch1 = read();
467+
ch0 = read();
468+
}
469+
if ((ch0 | ch1) < 0) throw new EOFException();
470+
return (short) ((ch0 << 8) + (ch1 << 0));
463471
}
464472

465473
@Override
@@ -474,36 +482,68 @@ default char readChar() throws IOException {
474482

475483
@Override
476484
default int readInt() throws IOException {
477-
int ch1 = read();
478-
int ch2 = read();
479-
int ch3 = read();
480-
int ch4 = read();
481-
if ((ch1 | ch2 | ch3 | ch4) < 0) throw new EOFException();
482-
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
485+
final int ch0;
486+
final int ch1;
487+
final int ch2;
488+
final int ch3;
489+
if (isBigEndian()) {
490+
ch0 = read();
491+
ch1 = read();
492+
ch2 = read();
493+
ch3 = read();
494+
}
495+
else {
496+
ch3 = read();
497+
ch2 = read();
498+
ch1 = read();
499+
ch0 = read();
500+
}
501+
if ((ch0 | ch1 | ch2 | ch3) < 0) throw new EOFException();
502+
return ((ch0 << 24) + (ch1 << 16) + (ch2 << 8) + (ch3 << 0));
483503
}
484504

485505
@Override
486506
default long readLong() throws IOException {
487-
int ch1 = read();
488-
int ch2 = read();
489-
int ch3 = read();
490-
int ch4 = read();
491-
int ch5 = read();
492-
int ch6 = read();
493-
int ch7 = read();
494-
int ch8 = read();
495-
if ((ch1 | ch2 | ch3 | ch4 | ch5 | ch6 | ch7 | ch8) < 0) {
507+
final int ch0;
508+
final int ch1;
509+
final int ch2;
510+
final int ch3;
511+
final int ch4;
512+
final int ch5;
513+
final int ch6;
514+
final int ch7;
515+
if (isBigEndian()) {
516+
ch0 = read();
517+
ch1 = read();
518+
ch2 = read();
519+
ch3 = read();
520+
ch4 = read();
521+
ch5 = read();
522+
ch6 = read();
523+
ch7 = read();
524+
}
525+
else {
526+
ch7 = read();
527+
ch6 = read();
528+
ch5 = read();
529+
ch4 = read();
530+
ch3 = read();
531+
ch2 = read();
532+
ch1 = read();
533+
ch0 = read();
534+
}
535+
if ((ch0 | ch1 | ch2 | ch3 | ch4 | ch5 | ch6 | ch7) < 0) {
496536
throw new EOFException();
497537
}
498538
// TODO: Double check this inconsistent code.
499-
return ((long) ch1 << 56) + //
500-
((long) (ch2 & 255) << 48) + //
501-
((long) (ch3 & 255) << 40) + //
502-
((long) (ch4 & 255) << 32) + //
503-
((long) (ch5 & 255) << 24) + //
504-
((ch6 & 255) << 16) + //
505-
((ch7 & 255) << 8) + //
506-
((ch8 & 255) << 0);
539+
return ((long) ch0 << 56) + //
540+
((long) (ch1 & 255) << 48) + //
541+
((long) (ch2 & 255) << 40) + //
542+
((long) (ch3 & 255) << 32) + //
543+
((long) (ch4 & 255) << 24) + //
544+
((ch5 & 255) << 16) + //
545+
((ch6 & 255) << 8) + //
546+
((ch7 & 255) << 0);
507547
}
508548

509549
@Override
@@ -571,34 +611,66 @@ default void writeByte(final int v) throws IOException {
571611

572612
@Override
573613
default void writeShort(final int v) throws IOException {
574-
write((v >>> 8) & 0xFF);
575-
write((v >>> 0) & 0xFF);
614+
if (isBigEndian()) {
615+
write((v >>> 8) & 0xFF);
616+
write((v >>> 0) & 0xFF);
617+
}
618+
else {
619+
write((v >>> 0) & 0xFF);
620+
write((v >>> 8) & 0xFF);
621+
}
576622
}
577623

578624
@Override
579625
default void writeChar(final int v) throws IOException {
580-
write((v >>> 8) & 0xFF);
581-
write((v >>> 0) & 0xFF);
626+
if (isBigEndian()) {
627+
write((v >>> 8) & 0xFF);
628+
write((v >>> 0) & 0xFF);
629+
}
630+
else {
631+
write((v >>> 0) & 0xFF);
632+
write((v >>> 8) & 0xFF);
633+
}
582634
}
583635

584636
@Override
585637
default void writeInt(final int v) throws IOException {
586-
write((v >>> 24) & 0xFF);
587-
write((v >>> 16) & 0xFF);
588-
write((v >>> 8) & 0xFF);
589-
write((v >>> 0) & 0xFF);
638+
if (isBigEndian()) {
639+
write((v >>> 24) & 0xFF);
640+
write((v >>> 16) & 0xFF);
641+
write((v >>> 8) & 0xFF);
642+
write((v >>> 0) & 0xFF);
643+
}
644+
else {
645+
write((v >>> 0) & 0xFF);
646+
write((v >>> 8) & 0xFF);
647+
write((v >>> 16) & 0xFF);
648+
write((v >>> 24) & 0xFF);
649+
}
590650
}
591651

592652
@Override
593653
default void writeLong(final long v) throws IOException {
594-
write((byte) (v >>> 56));
595-
write((byte) (v >>> 48));
596-
write((byte) (v >>> 40));
597-
write((byte) (v >>> 32));
598-
write((byte) (v >>> 24));
599-
write((byte) (v >>> 16));
600-
write((byte) (v >>> 8));
601-
write((byte) (v >>> 0));
654+
if (isBigEndian()) {
655+
write((byte) (v >>> 56));
656+
write((byte) (v >>> 48));
657+
write((byte) (v >>> 40));
658+
write((byte) (v >>> 32));
659+
write((byte) (v >>> 24));
660+
write((byte) (v >>> 16));
661+
write((byte) (v >>> 8));
662+
write((byte) (v >>> 0));
663+
}
664+
else {
665+
write((byte) (v >>> 0));
666+
write((byte) (v >>> 8));
667+
write((byte) (v >>> 16));
668+
write((byte) (v >>> 24));
669+
write((byte) (v >>> 32));
670+
write((byte) (v >>> 40));
671+
write((byte) (v >>> 48));
672+
write((byte) (v >>> 56));
673+
}
602674
}
603675

604676
@Override

0 commit comments

Comments
 (0)