Class SerialWriter


  • public class SerialWriter
    extends Object
    Assists in endian writing to an output stream.
    Author:
    Matthew Tropiano
    • Constructor Detail

      • SerialWriter

        public SerialWriter​(boolean endianMode)
        Creates a new serial writer.
        Parameters:
        endianMode - an _ENDIAN mode.
    • Method Detail

      • setEndianMode

        public void setEndianMode​(boolean mode)
        Sets the byte endian mode for the byte conversion methods. LITTLE_ENDIAN (Intel), the default, orients values from lowest byte to highest, while BIG_ENDIAN (Motorola) orients values from highest byte to lowest.
        Parameters:
        mode - an _ENDIAN mode.
      • writeString

        public void writeString​(OutputStream out,
                                String s)
                         throws IOException
        Writes a String.
        Parameters:
        out - the output stream.
        s - the String to write.
        Throws:
        IOException - if an error occurred during the write.
      • writeString

        public void writeString​(OutputStream out,
                                String s,
                                String encodingType)
                         throws IOException
        Writes a String in a specific encoding.
        Parameters:
        out - the output stream.
        s - the String to write.
        encodingType - the encoding type name.
        Throws:
        IOException - if an error occurred during the write.
      • writeString

        public void writeString​(OutputStream out,
                                String s,
                                Charset charset)
                         throws IOException
        Writes a String in a specific encoding.
        Parameters:
        out - the output stream.
        s - the String to write.
        charset - the encoding charset.
        Throws:
        IOException - if an error occurred during the write.
      • writeStringArray

        public void writeStringArray​(OutputStream out,
                                     String[] s)
                              throws IOException
        Writes an array of Strings, which is the length of the array as an integer plus each String.
        Parameters:
        out - the output stream.
        s - the array to write.
        Throws:
        IOException - if an error occurred during the write.
      • writeByte

        public void writeByte​(OutputStream out,
                              byte b)
                       throws IOException
        Writes a byte.
        Parameters:
        out - the output stream.
        b - the value to write.
        Throws:
        IOException - if an error occurred during the write.
      • writeUnsignedByte

        public void writeUnsignedByte​(OutputStream out,
                                      short s)
                               throws IOException
        Writes a short that is less than 256 to a byte.
        Parameters:
        out - the output stream.
        s - the value to write.
        Throws:
        IOException - if an error occurred during the write.
      • writeUnsignedByte

        public void writeUnsignedByte​(OutputStream out,
                                      int b)
                               throws IOException
        Writes an int that is less than 256 to a byte.
        Parameters:
        out - the output stream.
        b - the value to write.
        Throws:
        IOException - if an error occurred during the write.
      • writeBytes

        public void writeBytes​(OutputStream out,
                               byte[] b)
                        throws IOException
        Writes a series of bytes.
        Parameters:
        out - the output stream.
        b - the array to write.
        Throws:
        IOException - if an error occurred during the write.
      • writeByteArray

        public void writeByteArray​(OutputStream out,
                                   byte[] b)
                            throws IOException
        Writes an array of bytes, which is the length of the array as an integer plus each byte.
        Parameters:
        out - the output stream.
        b - the array to write.
        Throws:
        IOException - if an error occurred during the write.
      • writeBoolean

        public void writeBoolean​(OutputStream out,
                                 boolean b)
                          throws IOException
        Writes a boolean as a byte.
        Parameters:
        out - the output stream.
        b - the array to write.
        Throws:
        IOException - if an error occurred during the write.
      • writeUnsignedInteger

        public void writeUnsignedInteger​(OutputStream out,
                                         long l)
                                  throws IOException
        Writes a long that is less than 2^32 to an integer.
        Parameters:
        out - the output stream.
        l - the value to write.
        Throws:
        IOException - if an error occurred during the write.
      • writeInt

        public void writeInt​(OutputStream out,
                             int i)
                      throws IOException
        Writes an integer.
        Parameters:
        out - the output stream.
        i - the value to write.
        Throws:
        IOException - if an error occurred during the write.
      • writeVariableLengthInt

        public void writeVariableLengthInt​(OutputStream out,
                                           int i)
                                    throws IOException
        Converts an integer from an int to a variable-length string of bytes. Makes up to four bytes. Due to the nature of this algorithm, it is always written out in a Big-Endian fashion.
        Parameters:
        out - the output stream.
        i - the int to convert.
        Throws:
        IllegalArgumentException - if the int value to convert is above 0x0fffffff.
        IOException - if an error occurred during the write.
      • writeVariableLengthLong

        public void writeVariableLengthLong​(OutputStream out,
                                            long i)
                                     throws IOException
        Converts a long from a long to a variable-length string of bytes. Makes up to eight bytes. Due to the nature of this algorithm, it is always written out in a Big-Endian fashion.
        Parameters:
        out - the output stream.
        i - the long to convert.
        Throws:
        IllegalArgumentException - if the long value to convert is above 0x7fffffffffffffffL.
        IOException - if an error occurred during the write.
      • writeIntArray

        public void writeIntArray​(OutputStream out,
                                  int[] i)
                           throws IOException
        Writes an integer array, which is the length of the array as an integer plus each integer.
        Parameters:
        out - the output stream.
        i - the array to write.
        Throws:
        IOException - if an error occurred during the write.
      • writeLong

        public void writeLong​(OutputStream out,
                              long l)
                       throws IOException
        Writes a long.
        Parameters:
        out - the output stream.
        l - the value to write.
        Throws:
        IOException - if an error occurred during the write.
      • writeLongArray

        public void writeLongArray​(OutputStream out,
                                   long[] l)
                            throws IOException
        Writes an array of longs, which is the length of the array as an integer plus each long.
        Parameters:
        out - the output stream.
        l - the array to write.
        Throws:
        IOException - if an error occurred during the write.
      • writeFloatArray

        public void writeFloatArray​(OutputStream out,
                                    float[] f)
                             throws IOException
        Writes an array of 32-bit floats, which is the length of the array as an integer plus each float.
        Parameters:
        out - the output stream.
        f - the array to write.
        Throws:
        IOException - if an error occurred during the write.
      • writeFloat

        public void writeFloat​(OutputStream out,
                               float f)
                        throws IOException
        Writes a 32-bit float.
        Parameters:
        out - the output stream.
        f - the value to write.
        Throws:
        IOException - if an error occurred during the write.
      • writeDouble

        public void writeDouble​(OutputStream out,
                                double d)
                         throws IOException
        Writes a 64-bit float.
        Parameters:
        out - the output stream.
        d - the value to write.
        Throws:
        IOException - if an error occurred during the write.
      • writeDoubleArray

        public void writeDoubleArray​(OutputStream out,
                                     double[] d)
                              throws IOException
        Writes an array of 64-bit floats, which is the length of the array as an integer plus each double.
        Parameters:
        out - the output stream.
        d - the array to write.
        Throws:
        IOException - if an error occurred during the write.
      • writeShort

        public void writeShort​(OutputStream out,
                               short s)
                        throws IOException
        Writes a short.
        Parameters:
        out - the output stream.
        s - the value to write.
        Throws:
        IOException - if an error occurred during the write.
      • writeUnsignedShort

        public void writeUnsignedShort​(OutputStream out,
                                       int s)
                                throws IOException
        Writes an integer, less than 65536, as a short.
        Parameters:
        out - the output stream.
        s - the value to write.
        Throws:
        IOException - if an error occurred during the write.
      • writeShortArray

        public void writeShortArray​(OutputStream out,
                                    short[] s)
                             throws IOException
        Writes an array of shorts, which is the length of the array as an integer plus each short.
        Parameters:
        out - the output stream.
        s - the array to write.
        Throws:
        IOException - if an error occurred during the write.
      • writeChar

        public void writeChar​(OutputStream out,
                              char c)
                       throws IOException
        Writes a character.
        Parameters:
        out - the output stream.
        c - the value to write.
        Throws:
        IOException - if an error occurred during the write.
      • writeCharArray

        public void writeCharArray​(OutputStream out,
                                   char[] c)
                            throws IOException
        Writes a character array, which is the length of the array as an integer plus each character.
        Parameters:
        out - the output stream.
        c - the array to write.
        Throws:
        IOException - if an error occurred during the write.
      • writeBooleanArray

        public void writeBooleanArray​(OutputStream out,
                                      boolean... b)
                               throws IOException
        Writes a boolean array, which is the length of the array as an integer plus each boolean grouped into integer bits.
        Parameters:
        out - the output stream.
        b - the array to write.
        Throws:
        IOException - if an error occurred during the write.