jcontrol.io
Class DataOutputStream

java.lang.Object
  extended by jcontrol.io.OutputStream
      extended by jcontrol.io.DataOutputStream
Direct Known Subclasses:
GenericDataOutputStream

public abstract class DataOutputStream
extends OutputStream

A DataOutputStreams allows the output of java basic types (char, short, int, boolean) to any OutputStream. It is possible to select a specific endianess for the contained data. An internal byte counter is used to track information on the number of bytes produced by writing to this DataOutputStream. The counter can be read through method size().

A DataOutputStream can not be instantiated directly, instead an instance must be created through the static method createDataOutputStream( OutputStream out, int endianess).

Note: If the byte counter reaches the limit (Integer.MAX_INT), it overflows.


Field Summary
static int BIG_ENDIAN
          Big endian mode.
static int LITTLE_ENDIAN
          Little endian mode.
 
Constructor Summary
DataOutputStream()
           
 
Method Summary
static DataOutputStream createDataOutputStream(OutputStream out)
          Creates a new DataOutputStream with big endian data layout.
static DataOutputStream createDataOutputStream(OutputStream out, int endianess)
          Creates a new DataOutputStream.
abstract  int size()
          Returns the internal byte counter.
abstract  void write(byte[] b)
          Writes an array of bytes to the OutputStream.
abstract  void write(byte[] b, int start, int length)
          Writes a part of an array to the OutputStream.
abstract  void write(int b)
          Writes a single byte to the OutputStream.
abstract  void writeBoolean(boolean b)
          Writes a boolean to the DataOutputStream.
abstract  void writeChar(char c)
          Writes a char to the DataOutputStream and increases the internal byte counter by 2, the size of a char.
abstract  void writeDouble(double d)
          Writes an double to the DataOutputStream and increases the internal byte counter by 8, the size of a double.
abstract  void writeFloat(float f)
          Writes an float to the DataOutputStream and increases the internal byte counter by 4, the size of a float.
abstract  void writeInt(int i)
          Writes an int to the DataOutputStream and increases the internal byte counter by 4, the size of an int.
abstract  void writeLong(long l)
          Writes an long to the DataOutputStream and increases the internal byte counter by 8, the size of a long.
abstract  void writeShort(short s)
          Writes a short to the DataOutputStream and increases the internal byte counter by 2, the size of a short.
 
Methods inherited from class jcontrol.io.OutputStream
close, flush
 
Methods inherited from class java.lang.Object
clone, equals, getClass, notifyAll, toString, wait
 

Field Detail

BIG_ENDIAN

public static final int BIG_ENDIAN
Big endian mode.

See Also:
Constant Field Values

LITTLE_ENDIAN

public static final int LITTLE_ENDIAN
Little endian mode.

See Also:
Constant Field Values
Constructor Detail

DataOutputStream

public DataOutputStream()
Method Detail

createDataOutputStream

public static DataOutputStream createDataOutputStream(OutputStream out,
                                                      int endianess)
Creates a new DataOutputStream.
Note: No assumptions on the type of the returned DataOutputStream is allowed, because it may vary over different platforms.

Parameters:
out - OutputStream to use
endianess - BIG_ENDIAN or LITTLE_ENDIAN
Returns:
DataOutputStream

createDataOutputStream

public static DataOutputStream createDataOutputStream(OutputStream out)
Creates a new DataOutputStream with big endian data layout.
Note: No assumptions on the type of the returned DataOutputStream is allowed, because it may vary over different platforms.

Parameters:
out - OutputStream to use
Returns:
DataOutputStream

writeBoolean

public abstract void writeBoolean(boolean b)
                           throws IOException
Writes a boolean to the DataOutputStream. The boolean is represented as a byte value of 0 (false) or 1 (true). The internal byte counter is increased by 1.

Parameters:
b - boolean to write
Throws:
IOException - occurs on I/O errors or when the stream was closed

writeChar

public abstract void writeChar(char c)
                        throws IOException
Writes a char to the DataOutputStream and increases the internal byte counter by 2, the size of a char.

Parameters:
c - character to write
Throws:
IOException - occurs on I/O errors or when the stream was closed

writeShort

public abstract void writeShort(short s)
                         throws IOException
Writes a short to the DataOutputStream and increases the internal byte counter by 2, the size of a short.

Parameters:
s - short to write
Throws:
IOException - occurs on I/O errors or when the stream was closed

writeInt

public abstract void writeInt(int i)
                       throws IOException
Writes an int to the DataOutputStream and increases the internal byte counter by 4, the size of an int.

Parameters:
i - integer to write
Throws:
IOException - occurs on I/O errors or when the stream was closed

writeLong

public abstract void writeLong(long l)
                        throws IOException
Writes an long to the DataOutputStream and increases the internal byte counter by 8, the size of a long.

Parameters:
l - long to write
Throws:
IOException - occurs on I/O errors or when the stream was closed

writeFloat

public abstract void writeFloat(float f)
                         throws IOException
Writes an float to the DataOutputStream and increases the internal byte counter by 4, the size of a float.

Parameters:
f - float to write
Throws:
IOException - occurs on I/O errors or when the stream was closed

writeDouble

public abstract void writeDouble(double d)
                          throws IOException
Writes an double to the DataOutputStream and increases the internal byte counter by 8, the size of a double.

Parameters:
d - double to write
Throws:
IOException - occurs on I/O errors or when the stream was closed

size

public abstract int size()
Returns the internal byte counter.

Returns:
byte counter

write

public abstract void write(int b)
                    throws IOException
Writes a single byte to the OutputStream. The internal byte counter is increased by 1.

Specified by:
write in class OutputStream
Parameters:
b - byte to write
Throws:
IOException - occurs on I/O errors or when the stream is closed

write

public abstract void write(byte[] b)
                    throws IOException
Writes an array of bytes to the OutputStream. The internal byte counter is increased by the number of bytes written.

Overrides:
write in class OutputStream
Parameters:
b - array containing the data to send
Throws:
IOException - occurs on I/O errors or when the stream is closed

write

public abstract void write(byte[] b,
                           int start,
                           int length)
                    throws IOException
Writes a part of an array to the OutputStream. The internal byte counter is increased by the number of bytes written.

Specified by:
write in class OutputStream
Parameters:
b - array containing the data to send
start - index of the first byte to send in the array
length - number of bytes to send
Throws:
IOException - occurs on I/O errors or when the stream is closed