Packages  This Package  Prev  Next  Index  

§2.4 Class ByteArrayOutputStream

public  class  java.io.ByteArrayOutputStream
    extends  java.io.OutputStream  (I-§2.15)
{
        // Fields
    protected byte buf[];	§2.4.1
    protected int count;	§2.4.2

        // Constructors
    public ByteArrayOutputStream();	§2.4.3
    public ByteArrayOutputStream(int  size);	§2.4.4

        // Methods
    public void reset();	§2.4.5
    public int size();	§2.4.6
    public byte[] toByteArray();	§2.4.7
    public String toString();	§2.4.8
    public String toString(int  hibyte);	§2.4.9
    public void write(byte  b[], int  off, int  len);	§2.4.10
    public void write(int  b);	§2.4.11
    public void writeTo(OutputStream  out);	§2.4.12
}
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.


Fields

buf

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

count

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

Constructors

ByteArrayOutputStream

public ByteArrayOutputStream()
Creates a new byte array output stream.

ByteArrayOutputStream

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

Methods

reset

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

size

public int size()
Returns:
the value of the count field (I-§2.4.2), which is the number of valid bytes in this output stream.

toByteArray

public byte[] toByteArray()
Creates a newly allocated byte array whose size is the current size of this output stream (I-§2.4.6) and into which the valid contents of the buffer have been copied.

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

toString

public String toString()
Creates a newly allocated string whose size is the current size of this output stream (I-§2.4.6) and into which the valid contents of the buffer have been copied. Each character c in the resulting string is constructed from the corresponding element b in the byte array such that

toString

public String toString(int hibyte)
Creates a newly allocated string whose size is the current size of the output stream (I-§2.4.6) and into which the valid contents of the buffer have been copied. Each character c in the resulting string is constructed from the corresponding element b in the byte array such that

write

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.
Parameters:
b - the data
off - the start offset in the data
len - the number of bytes to write
Overrides:
write in class OutputStream (I-§2.15.5).

write

public void write(int b)
Writes the specified byte to this byte array output stream.
Parameters:
b - the byte to be written
Overrides:
write in class OutputStream (I-§2.15.6).

writeTo

public 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 (I-§2.29)
If an I/O error occurs.

Packages  This Package  Prev  Next  Index
Java API Document (HTML generated by dkramer on April 22, 1996)
Copyright © 1996 Sun Microsystems, Inc. All rights reserved
Please send any comments or corrections to doug.kramer@sun.com