4343
4444import org .scijava .io .location .Location ;
4545import org .scijava .plugin .WrapperPlugin ;
46+ import org .scijava .util .Bytes ;
4647
4748/**
4849 * A <em>data handle</em> is a plugin which provides both streaming and random
@@ -514,18 +515,10 @@ default int readUnsignedByte() throws IOException {
514515
515516 @ Override
516517 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 ());
529522 }
530523
531524 @ Override
@@ -540,68 +533,20 @@ default char readChar() throws IOException {
540533
541534 @ Override
542535 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 ());
561540 }
562541
563542 @ Override
564543 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 ) {
594547 throw new EOFException ();
595548 }
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 ());
605550 }
606551
607552 @ Override
@@ -669,76 +614,32 @@ default void writeByte(final int v) throws IOException {
669614
670615 @ Override
671616 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 ()));
680618 }
681619
682620 @ Override
683621 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 ()));
692623 }
693624
694625 @ Override
695626 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 ()));
708628 }
709629
710630 @ Override
711631 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 ()));
732633 }
733634
734635 @ Override
735636 default void writeFloat (final float v ) throws IOException {
736- writeInt ( Float . floatToIntBits ( v ));
637+ write ( Bytes . fromFloat ( v , isLittleEndian () ));
737638 }
738639
739640 @ Override
740641 default void writeDouble (final double v ) throws IOException {
741- writeLong ( Double . doubleToLongBits ( v ));
642+ write ( Bytes . fromDouble ( v , isLittleEndian () ));
742643 }
743644
744645 @ Override
@@ -750,9 +651,7 @@ default void writeBytes(final String s) throws IOException {
750651 default void writeChars (final String s ) throws IOException {
751652 final int len = s .length ();
752653 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 ));
756655 }
757656 }
758657
0 commit comments