Package java.io Previous
Previous
Java API
Java API
Index
Index
Next
Next

Class ByteArrayOutputStream

Fields , Constructors , Methods

public  class  java.io.ByteArrayOutputStream
    extends  java.io.OutputStream  
{
        // Fields
    protected byte buf[];	
    protected int count;	

        // Constructors
    public ByteArrayOutputStream();	
    public ByteArrayOutputStream(int  size);	

        // Methods
    public void reset();	
    public int size();	
    public byte[] toByteArray();	
    public String toString();	
    public String toString(int  hibyte);	
    public void write(byte  b[], int  off, int  len);	
    public void write(int  b);	
    public void writeTo(OutputStream  out);	
}

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.

ParameterDescription
size the initial size


Methods


reset

public void reset() 

Resets the count field 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() 

Return Value:

Returns the value of the count field , 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 and into which the valid contents of the buffer have been copied.

Return Value:

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

      	c == (char)(b & 0xff) 

Return Value:

Returns the current contents of this output stream, as a string.

Overrides:

toString in class Object .


toString

public String toString(int  hibyte) 

Creates a newly allocated string whose size is the current size of the output stream 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

      	c == (char)(((hibyte &amp; 0xff) << 8) | (b &amp; 0xff)) 

Parameters:

hibyte - 

the bits set

Return Value:

Returns the current contents of the output stream, as a string.


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.

ParameterDescription
b the data
off the start offset in the data
len the number of bytes to write

Overrides:

write in class OutputStream .


write

public void write(int  b) 

Writes the specified byte to this byte array output stream.

ParameterDescription
b the byte to be written

Overrides:

write in class OutputStream .


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

ParameterDescription
out the output stream to which to write the data

Throw:

IOException

If an I/O error occurs.



Top© 1996 Sun Microsystems, Inc. All rights reserved.