com.tffenterprises.io
Class ByteArrayOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.ByteArrayOutputStream
          extended by com.tffenterprises.io.ByteArrayOutputStream
All Implemented Interfaces:
AccountingOutput, java.io.Closeable, java.io.DataOutput, java.io.Flushable

public class ByteArrayOutputStream
extends java.io.ByteArrayOutputStream
implements AccountingOutput, java.io.DataOutput

com.tffenterprises.io.ByteArrayOutputStream is intended to be a drop-in replacement for java.io.ByteArrayOutputStream. It functions in exactly the same way as its superclass, with the advantage that the method write(byte[] b) throws no exceptions (except in the writeTo(OutputStream out) method, in which the exception potentially comes from the parameter).

This class implements an output stream in which the data is written into a byte array. The buffer automatically grows as data is written to it. The data can be retrieved using toByteArray() and toString().

Version:
1.0d1 $Date: 2000/06/20 02:52:25 $
Author:
Guillaume Lessard

Field Summary
private  byte[] buf
          private buffer to assist with the more complicated DataOutput methods.
 
Fields inherited from class java.io.ByteArrayOutputStream
count
 
Constructor Summary
ByteArrayOutputStream()
          Creates a ByteArrayOutputStream.
ByteArrayOutputStream(int size)
          Creates a new ByteArrayOutputStream, with a buffer capacity of the specified size, in bytes.
 
Method Summary
 void flush()
          Flushes this output stream and forces any buffered output bytes to be written out.
 void write(byte[] b)
          Writes b.length bytes from the specified byte array.
 void writeBoolean(boolean bool)
          Writes a boolean value to this output stream.
 void writeByte(int b)
          Writes a byte value to this output stream.
 void writeBytes(java.lang.String s)
          Writes the low-order-byte values of the given String parameter's chars, effectively writing the characters as ISO-8859-1 encoding.
 void writeChar(int c)
          Writes a char value to this output stream.
 void writeChars(java.lang.String s)
          Writes a String as a sequence of chars (2-byte values).
 void writeDouble(double d)
          Writes a double value to this output stream.
 void writeFloat(float f)
          Writes a float value to this output stream.
 void writeInt(int i)
          Writes an int value to this output stream.
 void writeLong(long l)
          Writes a long value to this output stream.
 void writeShort(int s)
          Writes a short value to this output stream.
 void writeUTF(java.lang.String s)
          Writes a String encoded as UTF-8.
 long written()
          Returns the number of bytes that have been written to the output buffer.
 
Methods inherited from class java.io.ByteArrayOutputStream
close, reset, size, toByteArray, toString, toString, toString, write, write, writeTo
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.io.DataOutput
write, write
 

Field Detail

buf

private byte[] buf
private buffer to assist with the more complicated DataOutput methods.

Constructor Detail

ByteArrayOutputStream

public ByteArrayOutputStream()
Creates a ByteArrayOutputStream. The buffer capacity is initially 32 bytes, but will grow as necessary.


ByteArrayOutputStream

public ByteArrayOutputStream(int size)
                      throws java.lang.IllegalArgumentException
Creates a new ByteArrayOutputStream, with a buffer capacity of the specified size, in bytes.

Parameters:
size - the initial size.
Throws:
java.lang.IllegalArgumentException - if size is negative.
Method Detail

write

public void write(byte[] b)
Writes b.length bytes from the specified byte array.

This is equivalent to using write(b, 0, b.length).

Specified by:
write in interface java.io.DataOutput
Overrides:
write in class java.io.OutputStream
Parameters:
b - the data

flush

public void flush()
Flushes this output stream and forces any buffered output bytes to be written out.

The general contract of flush() is that calling it is an indication that, if any bytes previously written have been buffered by the output stream object, such bytes should immediately be written to their intended destination.

The flush() method of ByteArrayOutputStream does nothing.

Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.OutputStream

written

public long written()
Returns the number of bytes that have been written to the output buffer.

Specified by:
written in interface AccountingOutput
Returns:
the number of bytes that have been written out.

writeBoolean

public void writeBoolean(boolean bool)
Writes a boolean value to this output stream.

Specified by:
writeBoolean in interface java.io.DataOutput
Parameters:
bool - the boolean value to be written out.

writeByte

public void writeByte(int b)
Writes a byte value to this output stream.

Specified by:
writeByte in interface java.io.DataOutput
Parameters:
b - the byte value to be written out.

writeShort

public void writeShort(int s)
Writes a short value to this output stream.

Specified by:
writeShort in interface java.io.DataOutput
Parameters:
s - the short value to be written out.

writeChar

public void writeChar(int c)
Writes a char value to this output stream.

Specified by:
writeChar in interface java.io.DataOutput
Parameters:
c - the char value to be written out.

writeInt

public void writeInt(int i)
Writes an int value to this output stream.

Specified by:
writeInt in interface java.io.DataOutput
Parameters:
i - the int value to be written out.

writeLong

public void writeLong(long l)
Writes a long value to this output stream.

Specified by:
writeLong in interface java.io.DataOutput
Parameters:
l - the long value to be written out.

writeFloat

public void writeFloat(float f)
Writes a float value to this output stream.

Specified by:
writeFloat in interface java.io.DataOutput
Parameters:
f - the float value to be written out.

writeDouble

public void writeDouble(double d)
Writes a double value to this output stream.

Specified by:
writeDouble in interface java.io.DataOutput
Parameters:
d - the double value to be written out.

writeBytes

public void writeBytes(java.lang.String s)
Writes the low-order-byte values of the given String parameter's chars, effectively writing the characters as ISO-8859-1 encoding.

Specified by:
writeBytes in interface java.io.DataOutput
Parameters:
s - the String to be written out as ISO-8859-1.

writeChars

public void writeChars(java.lang.String s)
Writes a String as a sequence of chars (2-byte values).

Specified by:
writeChars in interface java.io.DataOutput
Parameters:
s - the String to be written out to the stream.

writeUTF

public void writeUTF(java.lang.String s)
              throws java.io.UTFDataFormatException
Writes a String encoded as UTF-8.

Specified by:
writeUTF in interface java.io.DataOutput
Parameters:
s - the String to be written out to the stream as UTF-8.
Throws:
java.io.UTFDataFormatException - if the required number of output bytes exceeds 65535.