Class BinaryCodec

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class BinaryCodec
    extends Object
    implements Closeable
    Encapsulates file representation of various primitive data types. Forces little-endian disk representation. Note that this class is currently not very efficient. There are plans to increase the size of the ByteBuffer, and move data between the ByteBuffer and the underlying input or output stream in larger chunks. All the read methods throw RuntimeEOFException if the input stream is exhausted before the required number of bytes are read.
    • Constructor Detail

      • BinaryCodec

        public BinaryCodec​(Path path,
                           boolean writing)
        Constructs BinaryCodec from a file and set its mode to writing or not
        Parameters:
        path - file to be written to or read from
        writing - whether the file is being written to
      • BinaryCodec

        public BinaryCodec​(File file,
                           boolean writing)
        Constructs BinaryCodec from a file and set its mode to writing or not
        Parameters:
        file - file to be written to or read from
        writing - whether the file is being written to
      • BinaryCodec

        public BinaryCodec​(String fileName,
                           boolean writing)
        Constructs BinaryCodec from a file name and set its mode to writing or not
        Parameters:
        fileName - name of the file to be written to or read from
        writing - writing whether the file is being written to
      • BinaryCodec

        public BinaryCodec​(OutputStream outputStream)
        Constructs BinaryCodec from an output stream
        Parameters:
        outputStream - Stream to write to, since it's an output stream we know that isWriting should be set to true
      • BinaryCodec

        public BinaryCodec​(InputStream inputStream)
        Constructs BinaryCodec from an input stream
        Parameters:
        inputStream - Stream to read from, since we are reading isWriting is set to false
      • BinaryCodec

        public BinaryCodec()
        Ambiguous whether reading or writing until set{In,Out}putStream is called
    • Method Detail

      • writeByte

        public void writeByte​(byte bite)
        Writes a byte to the output buffer
        Parameters:
        bite - byte array to write
      • writeByte

        public void writeByte​(int b)
      • writeBytes

        public void writeBytes​(byte[] bytes)
        Writes a byte array to the output buffer
        Parameters:
        bytes - value to write
      • writeBytes

        public void writeBytes​(byte[] bytes,
                               int startOffset,
                               int numBytes)
      • writeInt

        public void writeInt​(int value)
        Write a 32-bit int to the output stream
        Parameters:
        value - int to write
      • writeDouble

        public void writeDouble​(double value)
        Write a double (8 bytes) to the output stream
        Parameters:
        value - double to write
      • writeLong

        public void writeLong​(long value)
        Write a 64-bit long to the output stream
        Parameters:
        value - long to write
      • writeShort

        public void writeShort​(short value)
        Write a 16-bit short to output stream
      • writeFloat

        public void writeFloat​(float value)
        Write a float (4 bytes) to the output stream
        Parameters:
        value - float to write
      • writeBoolean

        public void writeBoolean​(boolean value)
        Writes a boolean (1 byte) to the output buffer
        Parameters:
        value - boolean to write
      • writeString

        public void writeString​(String value,
                                boolean writeLength,
                                boolean appendNull)
        Writes a string to the buffer as ASCII bytes
        Parameters:
        value - string to write to buffer
        writeLength - prefix the string with the length as a 32-bit int
        appendNull - add a null byte to the end of the string
      • writeUByte

        public void writeUByte​(short val)
        Write an 8-bit unsigned byte. NOTE: This method will break if we change to big-endian.
      • writeUShort

        public void writeUShort​(int val)
        Write a 16-bit unsigned short. NOTE: This method will break if we change to big-endian.
      • writeUInt

        public void writeUInt​(long val)
        Write a 32-bit unsigned int. NOTE: This method will break if we change to big-endian.
      • readBytes

        public void readBytes​(byte[] buffer)
        Read a byte array from the input stream.
        Throws:
        RuntimeEOFException - if fewer than buffer.length bytes to read
      • readBytes

        public void readBytes​(byte[] buffer,
                              int offset,
                              int length)
        Read a byte array from the input stream
        Parameters:
        buffer - where to put bytes read
        offset - offset to start putting bytes into buffer
        length - number of bytes to read
        Throws:
        RuntimeEOFException - if fewer than length bytes to read
      • readBytesOrFewer

        public int readBytesOrFewer​(byte[] buffer,
                                    int offset,
                                    int length)
        Reads a byte array from the input stream.
        Parameters:
        buffer - where to put bytes read
        offset - offset to start putting bytes into buffer
        length - number of bytes to read. Fewer bytes may be read if EOF is reached before length bytes have been read.
        Returns:
        the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
      • readByte

        public byte readByte()
        Returns:
        a single byte read from the input stream.
      • knownAtEof

        public boolean knownAtEof()
        Returns:
        true if it is possible to know for sure if at EOF, and it is known for sure. If the input stream is a ByteArrayInputStream, this is faster than causing a RuntimeEOFException to be thrown.
      • readString

        public String readString​(int length)
        Read a string off the input stream, as ASCII bytes
        Parameters:
        length - length of string to read
        Returns:
        String read from stream
      • readNullTerminatedString

        public String readNullTerminatedString()
        Read ASCII bytes from the input stream until a null byte is read
        Returns:
        String constructed from the ASCII bytes read
      • readLengthAndString

        public String readLengthAndString​(boolean devourNull)
        Read an int length, and then a String of that length
        Parameters:
        devourNull - if true, the length include a null terminator, which is read and discarded
      • readInt

        public int readInt()
        Read an int off the input stream
        Returns:
        int from input stream
      • readDouble

        public double readDouble()
        Reads a double off the input stream
        Returns:
        double
      • readLong

        public long readLong()
        Reads a long off the input stream
        Returns:
        long
      • readShort

        public short readShort()
      • readFloat

        public float readFloat()
        Reads a float off the input stream
        Returns:
        float
      • readBoolean

        public boolean readBoolean()
        Reads a boolean off the input stream, represented as a byte with value 1 or 0
        Returns:
        boolean
      • readUByte

        public short readUByte()
        Reads an 8-bit unsigned byte from the input stream. This method assumes little-endianness.
      • readUShort

        public int readUShort()
        Reads a 16-bit unsigned short from the input stream. This method assumes little-endianness.
      • readUInt

        public long readUInt()
        Reads a 32-bit unsigned int from the input stream. This method assumes little-endianness.
      • getInputFileName

        public String getInputFileName()
      • getOutputFileName

        public String getOutputFileName()
      • setOutputFileName

        public void setOutputFileName​(String outputFileName)
      • setInputFileName

        public void setInputFileName​(String inputFileName)
      • isWriting

        public boolean isWriting()
      • setInputStream

        public void setInputStream​(InputStream is)
      • setOutputStream

        public void setOutputStream​(OutputStream os)