@@ -438,7 +438,7 @@ default void readFully(final byte[] b, final int off, final int len)
438
438
if (len < 0 ) throw new IndexOutOfBoundsException ();
439
439
int n = 0 ;
440
440
while (n < len ) {
441
- int count = read (b , off + n , len - n );
441
+ final int count = read (b , off + n , len - n );
442
442
if (count < 0 ) throw new EOFException ();
443
443
n += count ;
444
444
}
@@ -464,10 +464,20 @@ default int readUnsignedByte() throws IOException {
464
464
465
465
@ Override
466
466
default short readShort () throws IOException {
467
- final int ch1 = read ();
468
- final int ch2 = read ();
469
- if ((ch1 | ch2 ) < 0 ) throw new EOFException ();
470
- return (short ) ((ch1 << 8 ) + (ch2 << 0 ));
467
+ final int ch0 ;
468
+ final int ch1 ;
469
+
470
+ if (isBigEndian ()) {
471
+ ch0 = read ();
472
+ ch1 = read ();
473
+ }
474
+ else {
475
+ ch1 = read ();
476
+ ch0 = read ();
477
+ }
478
+
479
+ if ((ch0 | ch1 ) < 0 ) throw new EOFException ();
480
+ return (short ) ((ch0 << 8 ) + (ch1 << 0 ));
471
481
}
472
482
473
483
@ Override
@@ -482,32 +492,64 @@ default char readChar() throws IOException {
482
492
483
493
@ Override
484
494
default int readInt () throws IOException {
485
- int ch1 = read ();
486
- int ch2 = read ();
487
- int ch3 = read ();
488
- int ch4 = read ();
489
- if ((ch1 | ch2 | ch3 | ch4 ) < 0 ) throw new EOFException ();
490
- return ((ch1 << 24 ) + (ch2 << 16 ) + (ch3 << 8 ) + (ch4 << 0 ));
495
+ final int ch0 ;
496
+ final int ch1 ;
497
+ final int ch2 ;
498
+ final int ch3 ;
499
+ if (isBigEndian ()) {
500
+ ch0 = read ();
501
+ ch1 = read ();
502
+ ch2 = read ();
503
+ ch3 = read ();
504
+ }
505
+ else {
506
+ ch3 = read ();
507
+ ch2 = read ();
508
+ ch1 = read ();
509
+ ch0 = read ();
510
+ }
511
+ if ((ch0 | ch1 | ch2 | ch3 ) < 0 ) throw new EOFException ();
512
+ return ((ch0 << 24 ) + (ch1 << 16 ) + (ch2 << 8 ) + (ch3 << 0 ));
491
513
}
492
514
493
515
@ Override
494
516
default long readLong () throws IOException {
495
- int ch1 = read ();
496
- int ch2 = read ();
497
- int ch3 = read ();
498
- int ch4 = read ();
499
- int ch5 = read ();
500
- int ch6 = read ();
501
- int ch7 = read ();
502
- int ch8 = read ();
503
- if ((ch1 | ch2 | ch3 | ch4 | ch5 | ch6 | ch7 | ch8 ) < 0 ) {
517
+ final int ch0 ;
518
+ final int ch1 ;
519
+ final int ch2 ;
520
+ final int ch3 ;
521
+ final int ch5 ;
522
+ final int ch6 ;
523
+ final int ch7 ;
524
+ final int ch8 ;
525
+ if (isBigEndian ()) {
526
+ ch0 = read ();
527
+ ch1 = read ();
528
+ ch2 = read ();
529
+ ch3 = read ();
530
+ ch5 = read ();
531
+ ch6 = read ();
532
+ ch7 = read ();
533
+ ch8 = read ();
534
+ }
535
+ else {
536
+ ch8 = read ();
537
+ ch7 = read ();
538
+ ch6 = read ();
539
+ ch5 = read ();
540
+ ch3 = read ();
541
+ ch2 = read ();
542
+ ch1 = read ();
543
+ ch0 = read ();
544
+ }
545
+ if ((ch0 | ch1 | ch2 | ch3 | ch5 | ch6 | ch7 | ch8 ) < 0 ) {
504
546
throw new EOFException ();
505
547
}
506
548
// TODO: Double check this inconsistent code.
507
- return ((long ) ch1 << 56 ) + //
508
- ((long ) (ch2 & 255 ) << 48 ) + //
509
- ((long ) (ch3 & 255 ) << 40 ) + //
510
- ((long ) (ch4 & 255 ) << 32 ) + //
549
+ return ((long ) ch0 << 56 ) + //
550
+ ((long ) (ch1 & 255 ) << 48 ) + //
551
+ ((long ) (ch2 & 255 ) << 40 ) + //
552
+ ((long ) (ch3 & 255 ) << 32 ) + //
511
553
((long ) (ch5 & 255 ) << 24 ) + //
512
554
((ch6 & 255 ) << 16 ) + //
513
555
((ch7 & 255 ) << 8 ) + //
@@ -540,7 +582,7 @@ default String readLine() throws IOException {
540
582
break ;
541
583
case '\r' :
542
584
eol = true ;
543
- long cur = offset ();
585
+ final long cur = offset ();
544
586
if (read () != '\n' ) seek (cur );
545
587
break ;
546
588
default :
@@ -579,34 +621,66 @@ default void writeByte(final int v) throws IOException {
579
621
580
622
@ Override
581
623
default void writeShort (final int v ) throws IOException {
582
- write ((v >>> 8 ) & 0xFF );
583
- write ((v >>> 0 ) & 0xFF );
624
+ if (isBigEndian ()) {
625
+ write ((v >>> 8 ) & 0xFF );
626
+ write ((v >>> 0 ) & 0xFF );
627
+ }
628
+ else {
629
+ write ((v >>> 0 ) & 0xFF );
630
+ write ((v >>> 8 ) & 0xFF );
631
+ }
584
632
}
585
633
586
634
@ Override
587
635
default void writeChar (final int v ) throws IOException {
588
- write ((v >>> 8 ) & 0xFF );
589
- write ((v >>> 0 ) & 0xFF );
636
+ if (isBigEndian ()) {
637
+ write ((v >>> 8 ) & 0xFF );
638
+ write ((v >>> 0 ) & 0xFF );
639
+ }
640
+ else {
641
+ write ((v >>> 0 ) & 0xFF );
642
+ write ((v >>> 8 ) & 0xFF );
643
+ }
590
644
}
591
645
592
646
@ Override
593
647
default void writeInt (final int v ) throws IOException {
594
- write ((v >>> 24 ) & 0xFF );
595
- write ((v >>> 16 ) & 0xFF );
596
- write ((v >>> 8 ) & 0xFF );
597
- write ((v >>> 0 ) & 0xFF );
648
+ if (isBigEndian ()) {
649
+ write ((v >>> 24 ) & 0xFF );
650
+ write ((v >>> 16 ) & 0xFF );
651
+ write ((v >>> 8 ) & 0xFF );
652
+ write ((v >>> 0 ) & 0xFF );
653
+ }
654
+ else {
655
+ write ((v >>> 0 ) & 0xFF );
656
+ write ((v >>> 8 ) & 0xFF );
657
+ write ((v >>> 16 ) & 0xFF );
658
+ write ((v >>> 24 ) & 0xFF );
659
+ }
598
660
}
599
661
600
662
@ Override
601
663
default void writeLong (final long v ) throws IOException {
602
- write ((byte ) (v >>> 56 ));
603
- write ((byte ) (v >>> 48 ));
604
- write ((byte ) (v >>> 40 ));
605
- write ((byte ) (v >>> 32 ));
606
- write ((byte ) (v >>> 24 ));
607
- write ((byte ) (v >>> 16 ));
608
- write ((byte ) (v >>> 8 ));
609
- write ((byte ) (v >>> 0 ));
664
+ if (isBigEndian ()) {
665
+ write ((byte ) (v >>> 56 ));
666
+ write ((byte ) (v >>> 48 ));
667
+ write ((byte ) (v >>> 40 ));
668
+ write ((byte ) (v >>> 32 ));
669
+ write ((byte ) (v >>> 24 ));
670
+ write ((byte ) (v >>> 16 ));
671
+ write ((byte ) (v >>> 8 ));
672
+ write ((byte ) (v >>> 0 ));
673
+ }
674
+ else {
675
+ write ((byte ) (v >>> 0 ));
676
+ write ((byte ) (v >>> 8 ));
677
+ write ((byte ) (v >>> 16 ));
678
+ write ((byte ) (v >>> 24 ));
679
+ write ((byte ) (v >>> 32 ));
680
+ write ((byte ) (v >>> 40 ));
681
+ write ((byte ) (v >>> 48 ));
682
+ write ((byte ) (v >>> 56 ));
683
+ }
610
684
}
611
685
612
686
@ Override
@@ -638,5 +712,4 @@ default void writeChars(final String s) throws IOException {
638
712
default void writeUTF (final String str ) throws IOException {
639
713
DataHandles .writeUTF (str , this );
640
714
}
641
-
642
715
}
0 commit comments