java.io
Class ByteArrayOutputStream

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

public class ByteArrayOutputStream
extends OutputStream sample code for java.io.OutputStream definition code for java.io.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.

Since:
JDK1.0

Field Summary
protected  byte[] buf sample code for java.io.ByteArrayOutputStream.buf definition code for java.io.ByteArrayOutputStream.buf
          The buffer where data is stored.
protected  int count sample code for java.io.ByteArrayOutputStream.count definition code for java.io.ByteArrayOutputStream.count
          The number of valid bytes in the buffer.
 
Constructor Summary
ByteArrayOutputStream sample code for java.io.ByteArrayOutputStream.ByteArrayOutputStream() definition code for java.io.ByteArrayOutputStream.ByteArrayOutputStream() ()
          Creates a new byte array output stream.
ByteArrayOutputStream sample code for java.io.ByteArrayOutputStream.ByteArrayOutputStream(int) definition code for java.io.ByteArrayOutputStream.ByteArrayOutputStream(int) (int size)
          Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
 
Method Summary
 void close sample code for java.io.ByteArrayOutputStream.close() definition code for java.io.ByteArrayOutputStream.close() ()
          Closing a ByteArrayOutputStream has no effect.
 void reset sample code for java.io.ByteArrayOutputStream.reset() definition code for java.io.ByteArrayOutputStream.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.
 int size sample code for java.io.ByteArrayOutputStream.size() definition code for java.io.ByteArrayOutputStream.size() ()
          Returns the current size of the buffer.
 byte[] toByteArray sample code for java.io.ByteArrayOutputStream.toByteArray() definition code for java.io.ByteArrayOutputStream.toByteArray() ()
          Creates a newly allocated byte array.
 String sample code for java.lang.String definition code for java.lang.String toString sample code for java.io.ByteArrayOutputStream.toString() definition code for java.io.ByteArrayOutputStream.toString() ()
          Converts the buffer's contents into a string, translating bytes into characters according to the platform's default character encoding.
 String sample code for java.lang.String definition code for java.lang.String toString sample code for java.io.ByteArrayOutputStream.toString(int) definition code for java.io.ByteArrayOutputStream.toString(int) (int hibyte)
          Deprecated. This method does not properly convert bytes into characters. As of JDK 1.1, the preferred way to do this is via the toString(String enc) method, which takes an encoding-name argument, or the toString() method, which uses the platform's default character encoding.
 String sample code for java.lang.String definition code for java.lang.String toString sample code for java.io.ByteArrayOutputStream.toString(java.lang.String) definition code for java.io.ByteArrayOutputStream.toString(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  enc)
          Converts the buffer's contents into a string, translating bytes into characters according to the specified character encoding.
 void write sample code for java.io.ByteArrayOutputStream.write(byte[], int, int) definition code for java.io.ByteArrayOutputStream.write(byte[], int, int) (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 sample code for java.io.ByteArrayOutputStream.write(int) definition code for java.io.ByteArrayOutputStream.write(int) (int b)
          Writes the specified byte to this byte array output stream.
 void writeTo sample code for java.io.ByteArrayOutputStream.writeTo(java.io.OutputStream) definition code for java.io.ByteArrayOutputStream.writeTo(java.io.OutputStream) (OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out)
          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 sample code for java.io.OutputStream definition code for java.io.OutputStream
flush sample code for java.io.OutputStream.flush() definition code for java.io.OutputStream.flush() , write sample code for java.io.OutputStream.write(byte[]) definition code for java.io.OutputStream.write(byte[])
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Field Detail

buf sample code for java.io.ByteArrayOutputStream.buf

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


count sample code for java.io.ByteArrayOutputStream.count

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

Constructor Detail

ByteArrayOutputStream sample code for java.io.ByteArrayOutputStream() definition code for java.io.ByteArrayOutputStream()

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


ByteArrayOutputStream sample code for java.io.ByteArrayOutputStream(int) definition code for java.io.ByteArrayOutputStream(int)

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.
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if size is negative.
Method Detail

write sample code for java.io.ByteArrayOutputStream.write(int) definition code for java.io.ByteArrayOutputStream.write(int)

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

Specified by:
write sample code for java.io.OutputStream.write(int) definition code for java.io.OutputStream.write(int) in class OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream
Parameters:
b - the byte to be written.

write sample code for java.io.ByteArrayOutputStream.write(byte[], int, int) definition code for java.io.ByteArrayOutputStream.write(byte[], int, int)

public 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 sample code for java.io.OutputStream.write(byte[], int, int) definition code for java.io.OutputStream.write(byte[], int, int) in class OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream
Parameters:
b - the data.
off - the start offset in the data.
len - the number of bytes to write.

writeTo sample code for java.io.ByteArrayOutputStream.writeTo(java.io.OutputStream) definition code for java.io.ByteArrayOutputStream.writeTo(java.io.OutputStream)

public void writeTo(OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out)
             throws IOException sample code for java.io.IOException definition code for java.io.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 sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.

reset sample code for java.io.ByteArrayOutputStream.reset() definition code for java.io.ByteArrayOutputStream.reset()

public 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.

See Also:
ByteArrayInputStream.count sample code for java.io.ByteArrayInputStream.count definition code for java.io.ByteArrayInputStream.count

toByteArray sample code for java.io.ByteArrayOutputStream.toByteArray() definition code for java.io.ByteArrayOutputStream.toByteArray()

public 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.
See Also:
size() sample code for java.io.ByteArrayOutputStream.size() definition code for java.io.ByteArrayOutputStream.size()

size sample code for java.io.ByteArrayOutputStream.size() definition code for java.io.ByteArrayOutputStream.size()

public 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.
See Also:
count sample code for java.io.ByteArrayOutputStream.count definition code for java.io.ByteArrayOutputStream.count

toString sample code for java.io.ByteArrayOutputStream.toString() definition code for java.io.ByteArrayOutputStream.toString()

public String sample code for java.lang.String definition code for java.lang.String  toString()
Converts the buffer's contents into a string, translating bytes into characters according to the platform's default character encoding.

Overrides:
toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() in class Object sample code for java.lang.Object definition code for java.lang.Object
Returns:
String translated from the buffer's contents.
Since:
JDK1.1

toString sample code for java.io.ByteArrayOutputStream.toString(java.lang.String) definition code for java.io.ByteArrayOutputStream.toString(java.lang.String)

public String sample code for java.lang.String definition code for java.lang.String  toString(String sample code for java.lang.String definition code for java.lang.String  enc)
                throws UnsupportedEncodingException sample code for java.io.UnsupportedEncodingException definition code for java.io.UnsupportedEncodingException 
Converts the buffer's contents into a string, translating bytes into characters according to the specified character encoding.

Parameters:
enc - a character-encoding name.
Returns:
String translated from the buffer's contents.
Throws:
UnsupportedEncodingException sample code for java.io.UnsupportedEncodingException definition code for java.io.UnsupportedEncodingException - If the named encoding is not supported.
Since:
JDK1.1

toString sample code for java.io.ByteArrayOutputStream.toString(int) definition code for java.io.ByteArrayOutputStream.toString(int)

@Deprecated
public String sample code for java.lang.String definition code for java.lang.String  toString(int hibyte)
Deprecated. This method does not properly convert bytes into characters. As of JDK 1.1, the preferred way to do this is via the toString(String enc) method, which takes an encoding-name argument, or the toString() method, which uses the platform's default character encoding.

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.
See Also:
size() sample code for java.io.ByteArrayOutputStream.size() definition code for java.io.ByteArrayOutputStream.size() , toString(String) sample code for java.io.ByteArrayOutputStream.toString(java.lang.String) definition code for java.io.ByteArrayOutputStream.toString(java.lang.String) , toString() sample code for java.io.ByteArrayOutputStream.toString() definition code for java.io.ByteArrayOutputStream.toString()

close sample code for java.io.ByteArrayOutputStream.close() definition code for java.io.ByteArrayOutputStream.close()

public void close()
           throws IOException sample code for java.io.IOException definition code for java.io.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.

Specified by:
close sample code for java.io.Closeable.close() definition code for java.io.Closeable.close() in interface Closeable sample code for java.io.Closeable definition code for java.io.Closeable
Overrides:
close sample code for java.io.OutputStream.close() definition code for java.io.OutputStream.close() in class OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.