maspack.util
Class BinaryOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by maspack.util.BinaryOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class BinaryOutputStream
extends java.io.FilterOutputStream

A data output stream class that can be set to convert its output from big-endian to little-endian. Characters can also be specified to be either word or byte length. The code is a mash-up of Elliotte Rusty Harold's LittleEndianOutputStream and Antonio Sanchez's BinaryStreamReader.

Author:
John E. Lloyd, Antonio Sanchez, Elliotte Harold, Dec 16, 2013

Field Summary
static int BYTE_CHAR
           
static int LITTLE_ENDIAN
           
 
Constructor Summary
BinaryOutputStream(java.io.OutputStream out)
          Creates a new BinaryOutputStream from an output stream.
BinaryOutputStream(java.io.OutputStream out, int flags)
          Creates a new BinaryOutputStream from an output stream and prescribed set of flag values.
 
Method Summary
 int getFlags()
          Gets the flags for this stream.
 boolean isLittleEndian()
          Returns true if this stream converts it's output to little-endian where appropriate.
 void setByteChar(boolean bytechar)
          Sets whether or not this stream uses byte-length characters as opposed to 2-byte word characters.
 void setFlags(int flags)
          Sets the flags for this stream.
 void setLittleEndian(boolean little)
          Sets whether or not this stream converts its output to little-endian.
 int size()
          Returns the number of bytes that have been written to this stream.
 boolean usesByteChar()
          Returns true if this stream uses byte-length characters as opposed to 2-byte word characters.
 void write(byte[] data, int off, int len)
          Writes len byte values from datato this stream, starting at off.
 void write(int b)
          Writes a single byte value to this stream.
 void writeBoolean(boolean b)
          Writes a boolean value to this stream.
 void writeByte(int b)
          Writes a byte value to this stream.
 void writeBytes(java.lang.String s)
          Writes the bytes of a string to this stream.
 void writeChar(int c)
          Writes a character value to this stream.
 void writeChars(java.lang.String s)
          Writes the characters of a string to this stream.
 void writeDouble(double d)
          Writes a double value to this stream.
 void writeFloat(float f)
          Writes a float value to this stream.
 void writeInt(int v)
          Writes an integer value to this stream.
 void writeLong(long v)
          Writes a long value to this stream.
 void writeShort(int s)
          Writes a short value to this stream.
 void writeUTF(java.lang.String s)
          Writes a UTF-8 string to this stream.
 
Methods inherited from class java.io.FilterOutputStream
close, flush, write
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LITTLE_ENDIAN

public static final int LITTLE_ENDIAN
See Also:
Constant Field Values

BYTE_CHAR

public static final int BYTE_CHAR
See Also:
Constant Field Values
Constructor Detail

BinaryOutputStream

public BinaryOutputStream(java.io.OutputStream out)
Creates a new BinaryOutputStream from an output stream. Flag values are set to their defaults so that the stream will be big-endian and use word characters instead of byte characters.

Parameters:
out - underlying output stream

BinaryOutputStream

public BinaryOutputStream(java.io.OutputStream out,
                          int flags)
Creates a new BinaryOutputStream from an output stream and prescribed set of flag values.

Parameters:
out - underlying output stream
flags - flag values
Method Detail

setFlags

public void setFlags(int flags)
Sets the flags for this stream.

Parameters:
flags - new flag values

getFlags

public int getFlags()
Gets the flags for this stream.

Returns:
flags for this stream

isLittleEndian

public boolean isLittleEndian()
Returns true if this stream converts it's output to little-endian where appropriate.

Returns:
true if this stream converts to little-endian

setLittleEndian

public void setLittleEndian(boolean little)
Sets whether or not this stream converts its output to little-endian.

Parameters:
little - if true, enables little-endian conversion.

usesByteChar

public boolean usesByteChar()
Returns true if this stream uses byte-length characters as opposed to 2-byte word characters.

Returns:
true if this stream uses byte-length characters

setByteChar

public void setByteChar(boolean bytechar)
Sets whether or not this stream uses byte-length characters as opposed to 2-byte word characters.

Parameters:
bytechar - if true, enables byte-length characters.

write

public final void write(int b)
                 throws java.io.IOException
Writes a single byte value to this stream.

Overrides:
write in class java.io.FilterOutputStream
Parameters:
b - byte value to write
Throws:
java.io.IOException

write

public final void write(byte[] data,
                        int off,
                        int len)
                 throws java.io.IOException
Writes len byte values from datato this stream, starting at off.

Overrides:
write in class java.io.FilterOutputStream
Parameters:
data - byte values to write
off - starting point within data
len - number of bytes to write
Throws:
java.io.IOException

writeBoolean

public final void writeBoolean(boolean b)
                        throws java.io.IOException
Writes a boolean value to this stream.

Parameters:
b - boolean value to write
Throws:
java.io.IOException

writeByte

public final void writeByte(int b)
                     throws java.io.IOException
Writes a byte value to this stream.

Parameters:
b - byte value to write
Throws:
java.io.IOException

writeShort

public final void writeShort(int s)
                      throws java.io.IOException
Writes a short value to this stream. If isLittleEndian() returns true, then the byte order is switched from big-endian to little-endian.

Parameters:
s - short value to write
Throws:
java.io.IOException

writeChar

public final void writeChar(int c)
                     throws java.io.IOException
Writes a character value to this stream. The character is either one or two bytes, depending on whether usesByteChar() returns true or false. In the latter case, if isLittleEndian() returns true, then the byte order is switched from big-endian to little-endian.

Parameters:
c - character value to write
Throws:
java.io.IOException

writeInt

public final void writeInt(int v)
                    throws java.io.IOException
Writes an integer value to this stream. If isLittleEndian() returns true, then the byte order is switched from big-endian to little-endian.

Parameters:
v - integer value to write
Throws:
java.io.IOException

writeLong

public final void writeLong(long v)
                     throws java.io.IOException
Writes a long value to this stream. If isLittleEndian() returns true, then the byte order is switched from big-endian to little-endian.

Parameters:
v - long value to write
Throws:
java.io.IOException

writeBytes

public void writeBytes(java.lang.String s)
                throws java.io.IOException
Writes the bytes of a string to this stream.

Parameters:
s - string to write
Throws:
java.io.IOException

writeChars

public void writeChars(java.lang.String s)
                throws java.io.IOException
Writes the characters of a string to this stream. Characters are either one or two bytes, depending on whether usesByteChar() returns true or false. In the latter case, if isLittleEndian() returns true, then the byte order is switched from big-endian to little-endian.

Throws:
java.io.IOException

writeFloat

public final void writeFloat(float f)
                      throws java.io.IOException
Writes a float value to this stream. If isLittleEndian() returns true, then the byte order is switched from big-endian to little-endian.

Parameters:
f - float value to write
Throws:
java.io.IOException

writeDouble

public final void writeDouble(double d)
                       throws java.io.IOException
Writes a double value to this stream. If isLittleEndian() returns true, then the byte order is switched from big-endian to little-endian.

Parameters:
d - double value to write
Throws:
java.io.IOException

writeUTF

public final void writeUTF(java.lang.String s)
                    throws java.io.IOException
Writes a UTF-8 string to this stream.

Parameters:
s - string to write
Throws:
java.io.IOException

size

public int size()
Returns the number of bytes that have been written to this stream.