edu.rit.io
Class DataOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by edu.rit.io.DataOutputStream
All Implemented Interfaces:
Closeable, Flushable

public class DataOutputStream
extends FilterOutputStream

Class DataOutputStream provides an output stream that writes primitive data types and strings in binary form. It behaves similarly to class java.io.DataOutputStream, except the methods for writing types byte, short, char, int, long, and String are implemented differently. These methods write an integer value using a variable number of bytes, as described below. This can save space in the file if small integer values are written more frequently than large integer values. The resulting byte stream can be read using class DataInputStream.

Note that class DataOutputStream does not implement interface java.io.DataOutput, because the methods do not obey the contract specified in that interface.


Field Summary
 
Fields inherited from class java.io.FilterOutputStream
out
 
Constructor Summary
DataOutputStream(OutputStream out)
          Construct a new data output stream.
 
Method Summary
 void writeBoolean(boolean v)
          Write the given Boolean value to this data output stream.
 void writeDouble(double v)
          Write the given double value to this data output stream.
 void writeFloat(float v)
          Write the given float value to this data output stream.
 void writeInt(int v)
          Write the given integer value to this data output stream.
 void writeLong(long v)
          Write the given long value to this data output stream.
 void writeString(String v)
          Write the given string value to this data output stream.
 void writeUnsignedInt(int v)
          Write the given unsigned integer value to this data output stream.
 void writeUnsignedLong(long v)
          Write the given unsigned long value to this data output stream.
 
Methods inherited from class java.io.FilterOutputStream
close, flush, write, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DataOutputStream

public DataOutputStream(OutputStream out)
Construct a new data output stream.

Parameters:
out - Underlying output stream.
Method Detail

writeBoolean

public void writeBoolean(boolean v)
                  throws IOException
Write the given Boolean value to this data output stream. One byte is written, either 0 (if v is false) or 1 (if v is true).

Parameters:
v - Boolean value.
Throws:
IOException - Thrown if an I/O error occurred.

writeInt

public void writeInt(int v)
              throws IOException
Write the given integer value to this data output stream. This method can be used to write values of type byte, short, char, or int. From one to five bytes are written, in big-endian order, as follows:

Parameters:
v - Integer value.
Throws:
IOException - Thrown if an I/O error occurred.

writeUnsignedInt

public void writeUnsignedInt(int v)
                      throws IOException
Write the given unsigned integer value to this data output stream. This method can be used to write values of type byte, short, char, or int. From one to five bytes are written, in big-endian order, as follows:

Parameters:
v - Integer value.
Throws:
IOException - Thrown if an I/O error occurred.

writeLong

public void writeLong(long v)
               throws IOException
Write the given long value to this data output stream. From one to nine bytes are written, in big-endian order, as follows:

Parameters:
v - Integer value.
Throws:
IOException - Thrown if an I/O error occurred.

writeUnsignedLong

public void writeUnsignedLong(long v)
                       throws IOException
Write the given unsigned long value to this data output stream. From one to nine bytes are written, in big-endian order, as follows:

Parameters:
v - Integer value.
Throws:
IOException - Thrown if an I/O error occurred.

writeFloat

public void writeFloat(float v)
                throws IOException
Write the given float value to this data output stream. Four bytes are written in big-endian order containing Float.floatToRawIntBits(v).

Parameters:
v - Float value.
Throws:
IOException - Thrown if an I/O error occurred.

writeDouble

public void writeDouble(double v)
                 throws IOException
Write the given double value to this data output stream. Eight bytes are written in big-endian order containing Double.doubleToRawLongBits(v).

Parameters:
v - Double value.
Throws:
IOException - Thrown if an I/O error occurred.

writeString

public void writeString(String v)
                 throws IOException
Write the given string value to this data output stream. The length of the string is written using writeUnsignedInt(), then each character of the string is written using writeUnsignedInt().

Parameters:
v - String value.
Throws:
IOException - Thrown if an I/O error occurred.


Copyright © 2005-2012 by Alan Kaminsky. All rights reserved. Send comments to ark­@­cs.rit.edu.