This documentation differs from the official API. Jadeite adds extra features to the API including: variable font sizes, constructions examples, placeholders for classes and methods, and auto-generated “See Also” links. Additionally it is missing some items found in standard Javadoc documentation, including: generics type information, “Deprecated” tags and comments, “See Also” links, along with other minor differences. Please send any questions or feedback to bam@cs.cmu.edu.


java.io
class ByteArrayOutputStream

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

Most common way to construct:

ByteArrayOutputStream baos = new ByteArrayOutputStream();

Based on 629 examples


public class ByteArrayOutputStream
extends OutputStream

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().

Closing a ByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

See Also (auto-generated):

InputStream

File

ByteArrayInputStream


Field Summary
protected byte[] buf
          The buffer where data is stored.
protected int count
          The number of valid bytes in the buffer.
 
Constructor Summary

          Creates a new byte array output stream.

          Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
 
Method Summary
 void

          Closing a ByteArrayOutputStream has no effect.
 void

          Resets the count field of this byte array output stream to zero, so that all currently accumulated output in the output stream is discarded.
 int

          Returns the current size of the buffer.
 byte[]

          Creates a newly allocated byte array.
 String

          Converts the buffer's contents into a string decoding bytes using the platform's default character set.
 String
toString(int hibyte)

          Creates a newly allocated string.
 String
toString(String charsetName)

          Converts the buffer's contents into a string by decoding the bytes using the specified java.nio.charset.Charset.
 void
write(byte[] b, int off, int len)

          Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
 void
write(int b)

          Writes the specified byte to this byte array output stream.
 void

          Writes the complete contents of this byte array output stream to the specified output stream argument, as if by calling the output stream's write method using out.write(buf, 0, count).
 
Methods inherited from class java.io.OutputStream
close, flush, write, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

buf

protected byte[] buf
The buffer where data is stored.

count

protected int count
The number of valid bytes in the buffer.
Constructor Detail

ByteArrayOutputStream

public ByteArrayOutputStream()
Creates a new byte array output stream. The buffer capacity is initially 32 bytes, though its size increases if necessary.


ByteArrayOutputStream

public ByteArrayOutputStream(int size)
Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.

Parameters:
size - the initial size.
Method Detail

close

public void close()
           throws IOException
Closing a ByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

Overrides:
close in class OutputStream
Throws:
IOException

reset

public synchronized void reset()
Resets the count field of this byte array output stream to zero, so that all currently accumulated output in the output stream is discarded. The output stream can be used again, reusing the already allocated buffer space.


size

public synchronized int size()
Returns the current size of the buffer.

Returns:
the value of the count field, which is the number of valid bytes in this output stream.

toByteArray

public synchronized byte[] toByteArray()
Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it.

Returns:
the current contents of this output stream, as a byte array.

toString

public synchronized String toString()
Converts the buffer's contents into a string decoding bytes using the platform's default character set. The length of the new String is a function of the character set, and hence may not be equal to the size of the buffer.

This method always replaces malformed-input and unmappable-character sequences with the default replacement string for the platform's default character set. The {@linkplain java.nio.charset.CharsetDecoder} class should be used when more control over the decoding process is required.

Overrides:
toString in class Object
Returns:
String decoded from the buffer's contents.

toString

public synchronized String toString(int hibyte)
Creates a newly allocated string. Its size is the current size of the output stream and the valid contents of the buffer have been copied into it. Each character c in the resulting string is constructed from the corresponding element b in the byte array such that:
     c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))
 

Parameters:
hibyte - the high byte of each resulting Unicode character.
Returns:
the current contents of the output stream, as a string.

toString

public synchronized String toString(String charsetName)
                             throws UnsupportedEncodingException
Converts the buffer's contents into a string by decoding the bytes using the specified {@link java.nio.charset.Charset charsetName}. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.

This method always replaces malformed-input and unmappable-character sequences with this charset's default replacement string. The {@link java.nio.charset.CharsetDecoder} class should be used when more control over the decoding process is required.

Parameters:
charsetName - the name of a supported {@linkplain java.nio.charset.Charset charset}
Returns:
String decoded from the buffer's contents.
Throws:
UnsupportedEncodingException - If the named charset is not supported

write

public synchronized void write(byte[] b,
                               int off,
                               int len)
Writes len bytes from the specified byte array starting at offset off to this byte array output stream.

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

write

public synchronized void write(int b)
Writes the specified byte to this byte array output stream.

Overrides:
write in class OutputStream
Parameters:
b - the byte to be written.

writeTo

public synchronized void writeTo(OutputStream out)
                          throws IOException
Writes the complete contents of this byte array output stream to the specified output stream argument, as if by calling the output stream's write method using out.write(buf, 0, count).

Parameters:
out - the output stream to which to write the data.
Throws:
IOException - if an I/O error occurs.


This documentation differs from the official API. Jadeite adds extra features to the API including: variable font sizes, constructions examples, placeholders for classes and methods, and auto-generated “See Also” links. Additionally it is missing some items found in standard Javadoc documentation, including: generics type information, “Deprecated” tags and comments, “See Also” links, along with other minor differences. Please send any questions or feedback to bam@cs.cmu.edu.
This page displays the Jadeite version of the documention, which is derived from the offical documentation that contains this copyright notice:
Copyright 2008 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
The official Sun™ documentation can be found here at http://java.sun.com/javase/6/docs/api/.