@@ -250,6 +250,11 @@ default void setLittleEndian(final boolean little) {
250
250
/** Sets the native encoding of the stream. */
251
251
void setEncoding (String encoding );
252
252
253
+ /**
254
+ * @return a 8 byte long buffer array used for type conversions
255
+ */
256
+ byte [] conversionBuffer ();
257
+
253
258
/** Reads a string of arbitrary length, terminated by a null char. */
254
259
default String readCString () throws IOException {
255
260
final String line = findString ("\0 " );
@@ -515,7 +520,7 @@ default int readUnsignedByte() throws IOException {
515
520
516
521
@ Override
517
522
default short readShort () throws IOException {
518
- final byte [] buf = new byte [ 2 ] ;
523
+ final byte [] buf = conversionBuffer () ;
519
524
final int read = read (buf , 0 , 2 );
520
525
if (read < 2 ) throw new EOFException ();
521
526
return Bytes .toShort (buf , isLittleEndian ());
@@ -533,17 +538,17 @@ default char readChar() throws IOException {
533
538
534
539
@ Override
535
540
default int readInt () throws IOException {
536
- final byte [] buf = new byte [ 4 ] ;
541
+ final byte [] buf = conversionBuffer () ;
537
542
final int read = read (buf , 0 , 4 );
538
543
if (read < 4 ) throw new EOFException ();
539
544
return Bytes .toInt (buf , isLittleEndian ());
540
545
}
541
546
542
547
@ Override
543
548
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 ) {
547
552
throw new EOFException ();
548
553
}
549
554
return Bytes .toLong (buf , isLittleEndian ());
@@ -614,32 +619,42 @@ default void writeByte(final int v) throws IOException {
614
619
615
620
@ Override
616
621
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 );
618
625
}
619
626
620
627
@ Override
621
628
default void writeChar (final int v ) throws IOException {
622
- write ( Bytes . fromShort (( short ) v , isLittleEndian ()) );
629
+ writeShort ( v );
623
630
}
624
631
625
632
@ Override
626
633
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 );
628
637
}
629
638
630
639
@ Override
631
640
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 );
633
644
}
634
645
635
646
@ Override
636
647
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 );
638
651
}
639
652
640
653
@ Override
641
654
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 );
643
658
}
644
659
645
660
@ Override
0 commit comments