@@ -250,6 +250,11 @@ default void setLittleEndian(final boolean little) {
250250 /** Sets the native encoding of the stream. */
251251 void setEncoding (String encoding );
252252
253+ /**
254+ * @return a 8 byte long buffer array used for type conversions
255+ */
256+ byte [] conversionBuffer ();
257+
253258 /** Reads a string of arbitrary length, terminated by a null char. */
254259 default String readCString () throws IOException {
255260 final String line = findString ("\0 " );
@@ -515,7 +520,7 @@ default int readUnsignedByte() throws IOException {
515520
516521 @ Override
517522 default short readShort () throws IOException {
518- final byte [] buf = new byte [ 2 ] ;
523+ final byte [] buf = conversionBuffer () ;
519524 final int read = read (buf , 0 , 2 );
520525 if (read < 2 ) throw new EOFException ();
521526 return Bytes .toShort (buf , isLittleEndian ());
@@ -533,17 +538,17 @@ default char readChar() throws IOException {
533538
534539 @ Override
535540 default int readInt () throws IOException {
536- final byte [] buf = new byte [ 4 ] ;
541+ final byte [] buf = conversionBuffer () ;
537542 final int read = read (buf , 0 , 4 );
538543 if (read < 4 ) throw new EOFException ();
539544 return Bytes .toInt (buf , isLittleEndian ());
540545 }
541546
542547 @ Override
543548 default long readLong () throws IOException {
544- byte [] buf = new byte [ 8 ] ;
545- int read = read (buf , 0 , 8 );
546- if (read < 0 ) {
549+ final byte [] buf = conversionBuffer () ;
550+ final int read = read (buf , 0 , 8 );
551+ if (read < 8 ) {
547552 throw new EOFException ();
548553 }
549554 return Bytes .toLong (buf , isLittleEndian ());
@@ -614,32 +619,42 @@ default void writeByte(final int v) throws IOException {
614619
615620 @ Override
616621 default void writeShort (final int v ) throws IOException {
617- write (Bytes .fromShort ((short ) v , isLittleEndian ()));
622+ final byte [] buf = conversionBuffer ();
623+ Bytes .unpack (v , buf , 0 , 2 , isLittleEndian ());
624+ write (buf , 0 , 2 );
618625 }
619626
620627 @ Override
621628 default void writeChar (final int v ) throws IOException {
622- write ( Bytes . fromShort (( short ) v , isLittleEndian ()) );
629+ writeShort ( v );
623630 }
624631
625632 @ Override
626633 default void writeInt (final int v ) throws IOException {
627- write (Bytes .fromInt (v , isLittleEndian ()));
634+ final byte [] buf = conversionBuffer ();
635+ Bytes .unpack (v , buf , 0 , 4 , isLittleEndian ());
636+ write (buf , 0 , 4 );
628637 }
629638
630639 @ Override
631640 default void writeLong (final long v ) throws IOException {
632- write (Bytes .fromLong (v , isLittleEndian ()));
641+ final byte [] buf = conversionBuffer ();
642+ Bytes .unpack (v , buf , 0 , 8 , isLittleEndian ());
643+ write (buf , 0 , 8 );
633644 }
634645
635646 @ Override
636647 default void writeFloat (final float v ) throws IOException {
637- write (Bytes .fromFloat (v , isLittleEndian ()));
648+ final byte [] buf = conversionBuffer ();
649+ Bytes .unpack (Float .floatToIntBits (v ), buf , 0 , 4 , isLittleEndian ());
650+ write (buf , 0 , 4 );
638651 }
639652
640653 @ Override
641654 default void writeDouble (final double v ) throws IOException {
642- write (Bytes .fromDouble (v , isLittleEndian ()));
655+ final byte [] buf = conversionBuffer ();
656+ Bytes .unpack (Double .doubleToLongBits (v ), buf , 0 , 8 , isLittleEndian ());
657+ write (buf , 0 , 8 );
643658 }
644659
645660 @ Override
0 commit comments