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
@@ -249,6 +250,11 @@ default void setLittleEndian(final boolean little) {
249
250
/** Sets the native encoding of the stream. */
250
251
void setEncoding (String encoding );
251
252
253
+ /**
254
+ * @return a 8 byte long buffer array used for type conversions
255
+ */
256
+ byte [] conversionBuffer ();
257
+
252
258
/** Reads a string of arbitrary length, terminated by a null char. */
253
259
default String readCString () throws IOException {
254
260
final String line = findString ("\0 " );
@@ -514,18 +520,10 @@ default int readUnsignedByte() throws IOException {
514
520
515
521
@ Override
516
522
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 ));
523
+ final byte [] buf = conversionBuffer ();
524
+ final int read = read (buf , 0 , 2 );
525
+ if (read < 2 ) throw new EOFException ();
526
+ return Bytes .toShort (buf , isLittleEndian ());
529
527
}
530
528
531
529
@ Override
@@ -540,68 +538,20 @@ default char readChar() throws IOException {
540
538
541
539
@ Override
542
540
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 ));
541
+ final byte [] buf = conversionBuffer ();
542
+ final int read = read (buf , 0 , 4 );
543
+ if (read < 4 ) throw new EOFException ();
544
+ return Bytes .toInt (buf , isLittleEndian ());
561
545
}
562
546
563
547
@ Override
564
548
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 ) {
549
+ final byte [] buf = conversionBuffer ();
550
+ final int read = read (buf , 0 , 8 );
551
+ if (read < 8 ) {
594
552
throw new EOFException ();
595
553
}
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 );
554
+ return Bytes .toLong (buf , isLittleEndian ());
605
555
}
606
556
607
557
@ Override
@@ -618,7 +568,7 @@ default double readDouble() throws IOException {
618
568
default String readLine () throws IOException {
619
569
// NB: Adapted from java.io.RandomAccessFile.readLine().
620
570
621
- final StringBuffer input = new StringBuffer ();
571
+ final StringBuilder input = new StringBuilder ();
622
572
int c = -1 ;
623
573
boolean eol = false ;
624
574
@@ -669,76 +619,42 @@ default void writeByte(final int v) throws IOException {
669
619
670
620
@ Override
671
621
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
- }
622
+ final byte [] buf = conversionBuffer ();
623
+ Bytes .unpack (v , buf , 0 , 2 , isLittleEndian ());
624
+ write (buf , 0 , 2 );
680
625
}
681
626
682
627
@ Override
683
628
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
- }
629
+ writeShort (v );
692
630
}
693
631
694
632
@ Override
695
633
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
- }
634
+ final byte [] buf = conversionBuffer ();
635
+ Bytes .unpack (v , buf , 0 , 4 , isLittleEndian ());
636
+ write (buf , 0 , 4 );
708
637
}
709
638
710
639
@ Override
711
640
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
- }
641
+ final byte [] buf = conversionBuffer ();
642
+ Bytes .unpack (v , buf , 0 , 8 , isLittleEndian ());
643
+ write (buf , 0 , 8 );
732
644
}
733
645
734
646
@ Override
735
647
default void writeFloat (final float v ) throws IOException {
736
- writeInt (Float .floatToIntBits (v ));
648
+ final byte [] buf = conversionBuffer ();
649
+ Bytes .unpack (Float .floatToIntBits (v ), buf , 0 , 4 , isLittleEndian ());
650
+ write (buf , 0 , 4 );
737
651
}
738
652
739
653
@ Override
740
654
default void writeDouble (final double v ) throws IOException {
741
- writeLong (Double .doubleToLongBits (v ));
655
+ final byte [] buf = conversionBuffer ();
656
+ Bytes .unpack (Double .doubleToLongBits (v ), buf , 0 , 8 , isLittleEndian ());
657
+ write (buf , 0 , 8 );
742
658
}
743
659
744
660
@ Override
@@ -750,9 +666,7 @@ default void writeBytes(final String s) throws IOException {
750
666
default void writeChars (final String s ) throws IOException {
751
667
final int len = s .length ();
752
668
for (int i = 0 ; i < len ; i ++) {
753
- final int v = s .charAt (i );
754
- write ((v >>> 8 ) & 0xFF );
755
- write ((v >>> 0 ) & 0xFF );
669
+ writeChar (s .charAt (i ));
756
670
}
757
671
}
758
672
0 commit comments