Final

java.io
Class ByteArrayOutputStream

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

public class ByteArrayOutputStream
extends OutputStream

ByteArrayOutputStream is a class whose underlying stream is represented by a byte array. As bytes are written to this stream, the local byte array may be expanded to hold more bytes.

See Also:
ByteArrayInputStream

Field Summary
protected  byte[] buf
          The byte array containing the bytes written.
protected  int count
          The number of bytes written.
 
Constructor Summary
ByteArrayOutputStream()
          Constructs a new ByteArrayOutputStream with a default size of 32 bytes.
ByteArrayOutputStream(int size)
          Constructs a new ByteArrayOutputStream with a default size of size bytes.
 
Method Summary
 void close()
          Close this ByteArrayOutputStream.
 void reset()
          Reset this ByteArrayOutputStream to the beginning of the underlying byte array.
 int size()
          Answers the total number of bytes written to this stream thus far.
 byte[] toByteArray()
          Answer the contents of this ByteArrayOutputStream as a byte array.
 String toString()
          Answer the contents of this ByteArrayOutputStream as a String.
 String toString(int hibyte)
          Deprecated. Use toString()
 String toString(String enc)
          Answer the contents of this ByteArrayOutputStream as a String converted using the encoding declared in enc.
 void write(byte[] buffer, int offset, int count)
          Writes count bytes from the byte array buffer starting at offset index to the ByteArrayOutputStream.
 void write(int oneByte)
          Writes the specified byte oneByte to the OutputStream.
 void writeTo(OutputStream out)
          Take the contents of this stream and write it to the output stream out.
 
Methods inherited from class java.io.OutputStream
flush, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

buf

protected byte[] buf
The byte array containing the bytes written.


count

protected int count
The number of bytes written.

Constructor Detail

ByteArrayOutputStream

public ByteArrayOutputStream()
Constructs a new ByteArrayOutputStream with a default size of 32 bytes. If more than 32 bytes are written to this instance, the underlying byte array will expand to accomodate.


ByteArrayOutputStream

public ByteArrayOutputStream(int size)
Constructs a new ByteArrayOutputStream with a default size of size bytes. If more than size bytes are written to this instance, the underlying byte array will expand to accomodate.

Parameters:
size - an non-negative integer representing the initial size for the underlying byte array.
Method Detail

close

public void close()
           throws IOException
Close this ByteArrayOutputStream. This implementation releases System resources used for this stream.

Specified by:
close in interface Closeable
Overrides:
close in class OutputStream
Throws:
IOException - If an error occurs attempting to close this OutputStream.

reset

public void reset()
Reset this ByteArrayOutputStream to the beginning of the underlying byte array. All subsequent writes will overwrite any bytes previously stored in this stream.


size

public int size()
Answers the total number of bytes written to this stream thus far.

Returns:
the number of bytes written to this Stream.

toByteArray

public byte[] toByteArray()
Answer the contents of this ByteArrayOutputStream as a byte array. Any changes made to the receiver after returning will not be reflected in the byte array returned to the caller.

Returns:
this streams current contents as a byte array.

toString

public String toString()
Answer the contents of this ByteArrayOutputStream as a String. Any changes made to the receiver after returning will not be reflected in the String returned to the caller.

Overrides:
toString in class Object
Returns:
this streams current contents as a String.

toString

@Deprecated
public String toString(int hibyte)
Deprecated. Use toString()

Answer the contents of this ByteArrayOutputStream as a String. Each byte b in this stream is converted to a character c using the following function: c == (char)(((hibyte & 0xff) << 8) | (b & 0xff)). This method is deprecated and either toString(), or toString(enc) should be used.

Parameters:
hibyte - the high byte of each resulting Unicode character
Returns:
this streams current contents as a String with the high byte set to hibyte

toString

public String toString(String enc)
                throws UnsupportedEncodingException
Answer the contents of this ByteArrayOutputStream as a String converted using the encoding declared in enc.

Parameters:
enc - A String representing the encoding to use when translating this stream to a String.
Returns:
this streams current contents as a String.
Throws:
UnsupportedEncodingException - If declared encoding is not supported

write

public void write(byte[] buffer,
                  int offset,
                  int count)
Writes count bytes from the byte array buffer starting at offset index to the ByteArrayOutputStream.

Overrides:
write in class OutputStream
Parameters:
buffer - the buffer to be written
offset - offset in buffer to get bytes
count - number of bytes in buffer to write
Throws:
NullPointerException - If buffer is null.
IndexOutOfBoundsException - If offset or count are outside of bounds.

write

public void write(int oneByte)
Writes the specified byte oneByte to the OutputStream. Only the low order byte of oneByte is written.

Specified by:
write in class OutputStream
Parameters:
oneByte - the byte to be written

writeTo

public void writeTo(OutputStream out)
             throws IOException
Take the contents of this stream and write it to the output stream out.

Parameters:
out - An OutputStream on which to write the contents of this stream.
Throws:
IOException - If an error occurs when writing to output stream

Final

Licensed Materials - Property of IBM
© Copyright IBM Corp. 2006, 2008 All Rights Reserved.