|
Chapter 11 The java.io Package |
|
ByteArrayOutputStream
Name
ByteArrayOutputStream
- Class Name:
-
java.io.ByteArrayOutputStream
- Superclass:
-
java.io.FilterOutputStream
- Immediate Subclasses:
-
None
- Interfaces Implemented:
-
None
- Availability:
-
JDK 1.0 or later
A ByteArrayOutputStream
is a stream whose data is written to an internal
byte array. None of the methods
of this class throws an IOException
because the data is written to an array instead of an actual I/O
device.
The data for a ByteArrayOutputStream
can be sent to another OutputStream
using the writeTo() method.
A copy of the array can be obtained using the toCharArray()
method.
public class java.io.ByteArrayOutputStream extends java.io.OutputStream {
// Variables
protected byte[] buf;
protected int count;
// Constructors
public ByteArrayOutputStream();
public ByteArrayOutputStream(int size);
// Instance Methods
public synchronized void reset();
public int size( );
public synchronized byte[] toByteArray();
public String toString();
public String toString(int hibyte); // Deprecated in 1.1
public String toString(String enc); // New in 1.1
public synchronized void write(int b);
public synchronized void write(byte[] b, int off, int len);
public synchronized void writeTo(OutputStream out);
}
- Description
-
The buffer that holds data for this stream.
- Description
-
A placeholder that marks the end of the data in the buffer.
- Description
-
This constructor creates a ByteArrayOutputStream
with an internal buffer that has a default size of 32 bytes. The buffer
grows automatically as data is written to the stream.
- Parameters
-
- size
-
The initial buffer
size.
- Description
-
This constructor creates a ByteArrayOutputStream
with an internal buffer that has a size of size
bytes. The buffer grows automatically as data is written to the stream.
- Description
-
This method discards the current contents of the buffer and
resets the position of the stream to zero. Subsequent data
is written starting at the beginning of the array.
- Description
-
This method returns the number of bytes currently stored in this object's
internal buffer. It is a count of the number of bytes that have been written
to the stream.
- Returns
-
A copy of the data that has been written to this ByteArrayOutputStream.
- Description
-
This method copies the data in the internal array and returns a reference
to the copy. The returned array is as long as the data that has been written
to the stream, i.e., the same as size().
- Returns
-
A copy of the data that has been written to this ByteArrayOutputStream.
- Overrides
-
Object.toString()
- Description
-
This method returns a reference to a String object that
contains a copy of the bytes currently stored in this object's internal
buffer. The bytes are assumed to represent characters in the encoding
that is customary for the native platform, so the bytes are converted
to Unicode characters based on that assumption.
- Availability
-
Deprecated as of JDK 1.1
- Parameters
-
- hibyte
-
A value to use as
the high byte of each character.
- Returns
-
A copy of the data that has been written to this ByteArrayOutputStream,
where each character in the string has a high byte of hibyte
and a low byte taken from the corresponding byte in the array.
- Description
-
This method provides a way to convert from bytes to characters. As of 1.1,
it is deprecated and replaced with toString(String).
- Availability
-
New as of JDK 1.1
- Parameters
-
- enc
-
The encoding scheme
to use.
- Returns
-
A copy of the data that has been written to this ByteArrayOutputStream,
converted from bytes to characters via the named encoding scheme enc.
- Throws
-
- UnsupportedEncodingException
-
The
specified encoding is not supported.
- Description
-
This method returns a Java String
created from the byte array of this stream. The conversion is performed
according to the encoding scheme enc.
- Parameters
-
- b
-
The value to write.
- Overrides
-
OutputStream.write(int)
- Description
-
This method writes the low-order 8 bits of the
given value into the internal array. If the array
is full, a larger array is allocated.
- Parameters
-
- b
-
The array to copy from.
- off
-
Offset into the byte array.
- len
-
Number of bytes to write.
- Overrides
-
OutputStream.write(byte[], int, int)
- Description
-
This method copies len bytes
to this object's internal array from b,
starting oset elements from
the beginning of the supplied array b.
If the internal array is full, a larger array is allocated.
- Parameters
-
- out
-
The destination stream.
- Throws
-
- IOException
-
If any kind of I/O error occurs.
- Description
-
This method writes the contents of this object's internal buffer
to the given OutputStream.
All the data that has been written to this ByteArrayOutputStream
is written to out.
IOException,
OutputStream,
String,
UnsupportedEncodingException
|
|