43
43
44
44
import org .scijava .io .location .Location ;
45
45
import org .scijava .plugin .WrapperPlugin ;
46
+ import org .scijava .util .Bytes ;
46
47
47
48
/**
48
49
* A <em>data handle</em> is a plugin which provides both streaming and random
@@ -514,18 +515,10 @@ default int readUnsignedByte() throws IOException {
514
515
515
516
@ Override
516
517
default short readShort () throws IOException {
517
- final int ch0 ;
518
- final int ch1 ;
519
- if (isBigEndian ()) {
520
- ch0 = read ();
521
- ch1 = read ();
522
- }
523
- else {
524
- ch1 = read ();
525
- ch0 = read ();
526
- }
527
- if ((ch0 | ch1 ) < 0 ) throw new EOFException ();
528
- return (short ) ((ch0 << 8 ) + (ch1 << 0 ));
518
+ final byte [] buf = new byte [2 ];
519
+ final int read = read (buf , 0 , 2 );
520
+ if (read < 2 ) throw new EOFException ();
521
+ return Bytes .toShort (buf , isLittleEndian ());
529
522
}
530
523
531
524
@ Override
@@ -540,68 +533,20 @@ default char readChar() throws IOException {
540
533
541
534
@ Override
542
535
default int readInt () throws IOException {
543
- final int ch0 ;
544
- final int ch1 ;
545
- final int ch2 ;
546
- final int ch3 ;
547
- if (isBigEndian ()) {
548
- ch0 = read ();
549
- ch1 = read ();
550
- ch2 = read ();
551
- ch3 = read ();
552
- }
553
- else {
554
- ch3 = read ();
555
- ch2 = read ();
556
- ch1 = read ();
557
- ch0 = read ();
558
- }
559
- if ((ch0 | ch1 | ch2 | ch3 ) < 0 ) throw new EOFException ();
560
- return ((ch0 << 24 ) + (ch1 << 16 ) + (ch2 << 8 ) + (ch3 << 0 ));
536
+ final byte [] buf = new byte [4 ];
537
+ final int read = read (buf , 0 , 4 );
538
+ if (read < 4 ) throw new EOFException ();
539
+ return Bytes .toInt (buf , isLittleEndian ());
561
540
}
562
541
563
542
@ Override
564
543
default long readLong () throws IOException {
565
- final int ch0 ;
566
- final int ch1 ;
567
- final int ch2 ;
568
- final int ch3 ;
569
- final int ch4 ;
570
- final int ch5 ;
571
- final int ch6 ;
572
- final int ch7 ;
573
- if (isBigEndian ()) {
574
- ch0 = read ();
575
- ch1 = read ();
576
- ch2 = read ();
577
- ch3 = read ();
578
- ch4 = read ();
579
- ch5 = read ();
580
- ch6 = read ();
581
- ch7 = read ();
582
- }
583
- else {
584
- ch7 = read ();
585
- ch6 = read ();
586
- ch5 = read ();
587
- ch4 = read ();
588
- ch3 = read ();
589
- ch2 = read ();
590
- ch1 = read ();
591
- ch0 = read ();
592
- }
593
- if ((ch0 | ch1 | ch2 | ch3 | ch4 | ch5 | ch6 | ch7 ) < 0 ) {
544
+ byte [] buf = new byte [8 ];
545
+ int read = read (buf , 0 , 8 );
546
+ if (read < 0 ) {
594
547
throw new EOFException ();
595
548
}
596
- // TODO: Double check this inconsistent code.
597
- return ((long ) ch0 << 56 ) + //
598
- ((long ) (ch1 & 255 ) << 48 ) + //
599
- ((long ) (ch2 & 255 ) << 40 ) + //
600
- ((long ) (ch3 & 255 ) << 32 ) + //
601
- ((long ) (ch4 & 255 ) << 24 ) + //
602
- ((ch5 & 255 ) << 16 ) + //
603
- ((ch6 & 255 ) << 8 ) + //
604
- ((ch7 & 255 ) << 0 );
549
+ return Bytes .toLong (buf , isLittleEndian ());
605
550
}
606
551
607
552
@ Override
@@ -669,76 +614,32 @@ default void writeByte(final int v) throws IOException {
669
614
670
615
@ Override
671
616
default void writeShort (final int v ) throws IOException {
672
- if (isBigEndian ()) {
673
- write ((v >>> 8 ) & 0xFF );
674
- write ((v >>> 0 ) & 0xFF );
675
- }
676
- else {
677
- write ((v >>> 0 ) & 0xFF );
678
- write ((v >>> 8 ) & 0xFF );
679
- }
617
+ write (Bytes .fromShort ((short ) v , isLittleEndian ()));
680
618
}
681
619
682
620
@ Override
683
621
default void writeChar (final int v ) throws IOException {
684
- if (isBigEndian ()) {
685
- write ((v >>> 8 ) & 0xFF );
686
- write ((v >>> 0 ) & 0xFF );
687
- }
688
- else {
689
- write ((v >>> 0 ) & 0xFF );
690
- write ((v >>> 8 ) & 0xFF );
691
- }
622
+ write (Bytes .fromShort ((short ) v , isLittleEndian ()));
692
623
}
693
624
694
625
@ Override
695
626
default void writeInt (final int v ) throws IOException {
696
- if (isBigEndian ()) {
697
- write ((v >>> 24 ) & 0xFF );
698
- write ((v >>> 16 ) & 0xFF );
699
- write ((v >>> 8 ) & 0xFF );
700
- write ((v >>> 0 ) & 0xFF );
701
- }
702
- else {
703
- write ((v >>> 0 ) & 0xFF );
704
- write ((v >>> 8 ) & 0xFF );
705
- write ((v >>> 16 ) & 0xFF );
706
- write ((v >>> 24 ) & 0xFF );
707
- }
627
+ write (Bytes .fromInt (v , isLittleEndian ()));
708
628
}
709
629
710
630
@ Override
711
631
default void writeLong (final long v ) throws IOException {
712
- if (isBigEndian ()) {
713
- write ((byte ) (v >>> 56 ));
714
- write ((byte ) (v >>> 48 ));
715
- write ((byte ) (v >>> 40 ));
716
- write ((byte ) (v >>> 32 ));
717
- write ((byte ) (v >>> 24 ));
718
- write ((byte ) (v >>> 16 ));
719
- write ((byte ) (v >>> 8 ));
720
- write ((byte ) (v >>> 0 ));
721
- }
722
- else {
723
- write ((byte ) (v >>> 0 ));
724
- write ((byte ) (v >>> 8 ));
725
- write ((byte ) (v >>> 16 ));
726
- write ((byte ) (v >>> 24 ));
727
- write ((byte ) (v >>> 32 ));
728
- write ((byte ) (v >>> 40 ));
729
- write ((byte ) (v >>> 48 ));
730
- write ((byte ) (v >>> 56 ));
731
- }
632
+ write (Bytes .fromLong (v , isLittleEndian ()));
732
633
}
733
634
734
635
@ Override
735
636
default void writeFloat (final float v ) throws IOException {
736
- writeInt ( Float . floatToIntBits ( v ));
637
+ write ( Bytes . fromFloat ( v , isLittleEndian () ));
737
638
}
738
639
739
640
@ Override
740
641
default void writeDouble (final double v ) throws IOException {
741
- writeLong ( Double . doubleToLongBits ( v ));
642
+ write ( Bytes . fromDouble ( v , isLittleEndian () ));
742
643
}
743
644
744
645
@ Override
@@ -750,9 +651,7 @@ default void writeBytes(final String s) throws IOException {
750
651
default void writeChars (final String s ) throws IOException {
751
652
final int len = s .length ();
752
653
for (int i = 0 ; i < len ; i ++) {
753
- final int v = s .charAt (i );
754
- write ((v >>> 8 ) & 0xFF );
755
- write ((v >>> 0 ) & 0xFF );
654
+ writeChar (s .charAt (i ));
756
655
}
757
656
}
758
657
0 commit comments