Class OutputStream


  extended by Object
      extended by OutputStream

public class OutputStream
extends Object

The OutputStream class provides for converting data from any of the primitive types to a series of bytes and writing these bytes to a binary stream. There is also a facility for converting a String into Java modified UTF-8 format and writing the resulting series of bytes.

See Also:
InputStream, ByteArray, Value, IS_BINARY

Constructor Summary
OutputStream(int initialCapacity)
          Creates a new output stream that will write into memory buffer.
 
Method Summary
 ByteArray toByteArray()
          Returns the contents of memory buffer that is backing up this OutputStream.
 void write(ByteArray b)
          Writes b.length() bytes from the specified byte array to this output stream.
 void write(ByteArray b, int off, int len)
          Writes len bytes from the specified byte array starting at offset off to this output stream.
 void write(int b)
          Writes the specified byte to this output stream.
 void writeBoolean(boolean v)
          Writes a boolean value to this output stream.
 void writeByte(int v)
          Writes to the output stream the eight low- order bits of the argument v.
 void writeChar(char v)
          Writes a char value, which is comprised of two bytes, to the output stream.
 void writeInt(int v)
          Writes an int value, which is comprised of four bytes, to the output stream.
 void writeLong(long v)
          Writes an long value, which is comprised of four bytes, to the output stream.
 void writeShort(int v)
          Writes two bytes to the output stream to represent the value of the argument.
 void writeUTF(String str)
          Writes two bytes of length information to the output stream, followed by the Java modified UTF representation of every character in the string s.
 void writeValue(Value value)
          Serializes specified structure into this output stream.
 
Methods inherited from class Object
toString, equals, hashCode
 
Methods inherited from
equals, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

OutputStream

public OutputStream(int initialCapacity)
Creates a new output stream that will write into memory buffer.

Parameters:
initialCapacity - initial size of memory buffer, buffer will expand as needed.
Method Detail

write

public void write(ByteArray b)
Writes b.length() bytes from the specified byte array to this output stream. The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length()).

Parameters:
b - the data.
See Also:
write(ByteArray, int, int)

write

public void write(ByteArray b,
                  int off,
                  int len)
Writes len bytes from the specified byte array starting at offset off to this output stream. The general contract for write(b, off, len) is that some of the bytes in the array b are written to the output stream in order; element b[off] is the first byte written and b[off+len-1] is the last byte written by this operation.

The write method of OutputStream calls the write method of one argument on each of the bytes to be written out. Subclasses are encouraged to override this method and provide a more efficient implementation.

If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown.

Parameters:
b - the data.
off - the start offset in the data.
len - the number of bytes to write.

write

public void write(int b)
Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

Parameters:
b - the byte.

writeBoolean

public void writeBoolean(boolean v)
Writes a boolean value to this output stream. If the argument v is true, the value (byte)1 is written; if v is false, the value (byte)0 is written. The byte written by this method may be read by the InputStream.readBoolean(), which will then return a boolean equal to v.

Parameters:
v - the boolean to be written.

writeByte

public void writeByte(int v)
Writes to the output stream the eight low- order bits of the argument v. The 24 high-order bits of v are ignored. (This means that writeByte does exactly the same thing as write for an integer argument.) The byte written by this method may be read by the InputStream.readByte(), which will then return a byte equal to (byte)v.

Parameters:
v - the byte value to be written.

writeChar

public void writeChar(char v)
Writes a char value, which is comprised of two bytes, to the output stream. The byte values to be written, in the order shown, are:


 (byte)(0xff & (v >> 8))
 (byte)(0xff & v)
 

The bytes written by this method may be read by the InputStream.readChar(), which will then return a char equal to (char)v.

Parameters:
v - the char value to be written.

writeInt

public void writeInt(int v)
Writes an int value, which is comprised of four bytes, to the output stream. The byte values to be written, in the order shown, are:


 (byte)(0xff & (v >> 24))
 (byte)(0xff & (v >> 16))
 (byte)(0xff & (v >>    8))
 (byte)(0xff & v)
 

The bytes written by this method may be read by the readInt InputStream.readInt(), which will then return an int equal to v.

Parameters:
v - the int value to be written.

writeLong

public void writeLong(long v)
Writes an long value, which is comprised of four bytes, to the output stream. The byte values to be written, in the order shown, are:


 (byte)(0xff & (v >> 48))
 (byte)(0xff & (v >> 40))
 (byte)(0xff & (v >> 32))
 (byte)(0xff & (v >> 24))
 (byte)(0xff & (v >> 16))
 (byte)(0xff & (v >>  8))
 (byte)(0xff & v)
 

The bytes written by this method may be read by the InputStream.readLong(), which will then return a long equal to v.

Parameters:
v - the long value to be written.

writeShort

public void writeShort(int v)
Writes two bytes to the output stream to represent the value of the argument. The byte values to be written, in the order shown, are:


 (byte)(0xff & (v >> 8))
 (byte)(0xff & v)
  

The bytes written by this method may be read by the InputStream.readShort(), which will then return a short equal to (short)v.

Parameters:
v - the short value to be written.

writeUTF

public void writeUTF(String str)
Writes two bytes of length information to the output stream, followed by the Java modified UTF representation of every character in the string s.

Parameters:
str - the string value to be written.

writeValue

public void writeValue(Value value)
Serializes specified structure into this output stream.

Parameters:
value - Value to write

toByteArray

public ByteArray toByteArray()
Returns the contents of memory buffer that is backing up this OutputStream.

Returns:
ByteArray instance that contains all the bytes written.