Package java.io |
![]() Previous |
![]() Java API |
![]() Index |
![]() Next |
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.
protected byte buf[]The buffer where data is stored.
protected int countThe number of valid bytes in the buffer.
public ByteArrayOutputStream()Creates a new byte array output stream.
public ByteArrayOutputStream(int size)Creates a new byte array output stream. The buffer capacity is initially 32 bytes, though its size increases if necessary.
Parameter Description size the initial size
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.
public int size()Return Value:
Returns the value of the count field , which is the number of valid bytes in this output stream.
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.
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 .
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 & 0xff) << 8) | (b & 0xff))Parameters:
hibyte -the bits set
Return Value:
Returns the current contents of the output stream, as a string.
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.
Parameter Description b the data off the start offset in the data len the number of bytes to write Overrides:
write in class OutputStream .
public void write(int b)Writes the specified byte to this byte array output stream.
Parameter Description b the byte to be written Overrides:
write in class OutputStream .
public void writeTo(OutputStream out)throws IOExceptionWrites 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).
Parameter Description out the output stream to which to write the data Throw:
If an I/O error occurs.