Note that there are some explanatory texts on larger screens.

plurals
  1. POJava : DataInputStream replacement for endianness
    primarykey
    data
    text
    <p>Below is my code that replaces the DataInputStream to wrap an InputStream, but provides extra methods to read little endian data types in addition to the normal methods that read big endian types. Feel free to use it if you'd like.</p> <p>I have a few reservations as follows. Notice the methods that do not change functionality (the functions that read big endian types). There is no way I could implement the DataInputStream as the base class and use its methods, like read(), readInt(), readChar(), etc? </p> <p>My class hierarchy seems a little odd here. Is this appropriate?</p> <p>Do any of these other types like readUTF() or readLine() need a little endian version? Or is that subjective to specific programs?</p> <p>How does Java store boolean types? Is that subjective to endianness also?</p> <p>Thanks for satisfying my curiosities :)</p> <pre><code>import java.io.*; /** * Replacement for a DataInputStream that provides both little and big endian reading capabilities for convenience without need to implement a ByteBuffer * @author Bill (unspecified.specification@gmail.com) */ public class EndianInputStream extends InputStream implements DataInput { private DataInputStream dataInStream; private InputStream inStream; private byte byteBuffer[]; /** * Constructor to wrap InputStream for little and big endian data * @param refInStream Inputstream to wrap */ public EndianInputStream(InputStream refInStream) { inStream = refInStream; dataInStream = new DataInputStream(inStream); byteBuffer = new byte[8]; // Largest data type is 64-bits (8 bytes) } @Override public int available() throws IOException { return dataInStream.available(); } @Override public final int read(byte refBuffer[], int offset, int readLen) throws IOException { return inStream.read(refBuffer, offset, readLen); } @Override public int read() throws IOException { return inStream.read(); } @Override public final int readUnsignedByte() throws IOException { return dataInStream.readUnsignedByte(); } @Deprecated @Override public final String readLine() throws IOException { return dataInStream.readLine(); } @Override public final String readUTF() throws IOException { return dataInStream.readUTF(); } @Override public final void close() throws IOException { dataInStream.close(); } @Override public final void readFully(byte refBuffer[]) throws IOException { dataInStream.readFully(refBuffer, 0, refBuffer.length); } @Override public final void readFully(byte refBuffer[], int offset, int readLen) throws IOException { dataInStream.readFully(refBuffer, offset, readLen); } @Override public final int skipBytes(int n) throws IOException { return dataInStream.skipBytes(n); } @Override public final boolean readBoolean() throws IOException { return dataInStream.readBoolean(); } @Override public final byte readByte() throws IOException { return dataInStream.readByte(); } @Override public final float readFloat() throws IOException { return Float.intBitsToFloat(readInt()); } @Override public final double readDouble() throws IOException { return Double.longBitsToDouble(readLong()); } @Override public final short readShort() throws IOException { return dataInStream.readShort(); } @Override public final int readUnsignedShort() throws IOException { return dataInStream.readUnsignedShort(); } @Override public final long readLong() throws IOException { return dataInStream.readLong(); } @Override public final char readChar() throws IOException { return dataInStream.readChar(); } @Override public final int readInt() throws IOException { return dataInStream.readInt(); } /** * Reads floating point type stored in little endian (see readFloat() for big endian) * @return float value translated from little endian * @throws IOException if an IO error occurs */ public final float readLittleFloat() throws IOException { return Float.intBitsToFloat(readLittleInt()); } /** * Reads double precision floating point type stored in little endian (see readDouble() for big endian) * @return double precision float value translated from little endian * @throws IOException if an IO error occurs */ public final double readLittleDouble() throws IOException { return Double.longBitsToDouble(readLittleLong()); } /** * Reads short type stored in little endian (see readShort() for big endian) * @return short value translated from little endian * @throws IOException if an IO error occurs */ public final short readLittleShort() throws IOException { dataInStream.readFully(byteBuffer, 0, 2); return (short)((byteBuffer[1] &amp; 0xff) &lt;&lt; 8 | (byteBuffer[0] &amp; 0xff)); } /** * Reads char (16-bits) type stored in little endian (see readChar() for big endian) * @return char value translated from little endian * @throws IOException if an IO error occurs */ public final char readLittleChar() throws IOException { dataInStream.readFully(byteBuffer, 0, 2); return (char)((byteBuffer[1] &amp; 0xff) &lt;&lt; 8 | (byteBuffer[0] &amp; 0xff)); } /** * Reads integer type stored in little endian (see readInt() for big endian) * @return integer value translated from little endian * @throws IOException if an IO error occurs */ public final int readLittleInt() throws IOException { dataInStream.readFully(byteBuffer, 0, 4); return (byteBuffer[3]) &lt;&lt; 24 | (byteBuffer[2] &amp; 0xff) &lt;&lt; 16 | (byteBuffer[1] &amp; 0xff) &lt;&lt; 8 | (byteBuffer[0] &amp; 0xff); } /** * Reads long type stored in little endian (see readLong() for big endian) * @return long value translated from little endian * @throws IOException if an IO error occurs */ public final long readLittleLong() throws IOException { dataInStream.readFully(byteBuffer, 0, 8); return (long)(byteBuffer[7]) &lt;&lt; 56 | (long)(byteBuffer[6]&amp;0xff) &lt;&lt; 48 | (long)(byteBuffer[5] &amp; 0xff) &lt;&lt; 40 | (long)(byteBuffer[4] &amp; 0xff) &lt;&lt; 32 | (long)(byteBuffer[3] &amp; 0xff) &lt;&lt; 24 | (long)(byteBuffer[2] &amp; 0xff) &lt;&lt; 16 | (long)(byteBuffer[1] &amp; 0xff) &lt;&lt; 8 | (long)(byteBuffer[0] &amp; 0xff); } /** * Reads unsigned short type stored in little endian (see readUnsignedShort() for big endian) * @return integer value representing unsigned short value translated from little endian * @throws IOException if an IO error occurs */ public final int readLittleUnsignedShort() throws IOException { dataInStream.readFully(byteBuffer, 0, 2); return ((byteBuffer[1] &amp; 0xff) &lt;&lt; 8 | (byteBuffer[0] &amp; 0xff)); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload