1 How to use
The bytearrayinputstream contains an internal buffer that contains the bytes read from the stream. The internal counter tracks the next byte to be provided by the Read method. Bytearrayoutputstream implements an output stream in which data is written to a byte array. The buffer automatically grows as the data is being written. You can use Tobytearray () and toString () to get the data. 1.1 Method Introduction
Bytearrayoutputstream provides the following APIs:
Constructor
Bytearrayoutputstream ()
bytearrayoutputstream (int size)
void Close ()//closing byte stream
synchronized void Reset ()//Reset counter
int size ()//Get current Count
synchronized byte[] Tobytearray ()// Converts a stream of bytes to byte array
string tostring (int hibyte)//convert a byte stream to string string tostring (String charsetname)
String toString ()
synchronized void write (byte[] buffer, int offset, int len)//write byte array buffer to Word stream , offset is the starting position of the buffer
synchronized void write (int onebyte)//write a byte to stream
synchronized void WriteTo (OutputStream out)/write output stream to other output stream out
}
1.2 Using the example
public void Testbytearrayoutputstream () {byte [] letter = {' h ', ' I ', ' j ', ' K '};
New byte stream Bytearrayoutputstream outputstream = new Bytearrayoutputstream (); Write ABCDEFG int i = ' a ';
A while (I < ' h ') {outputstream.write (i);
i++;
System.out.println ("The contents of the current byte stream are:" + outputstream.tostring ());
Write multiple Outputstream.write (letter, 1, 3);
System.out.println ("written in the letter array of the 2nd, 3, 4 alphanumeric bytes in the contents are:" + outputstream.tostring ());
System.out.println ("The number of bytes in the current output stream is:" + outputstream.size ());
byte [] Bytearr = Outputstream.tobytearray ();
i = 0;
System.out.print ("Byte array content is:");
while (I < bytearr.length) {System.out.print (bytearr[i++] + "");
} System.out.println ();
OutputStream cloneout = new Bytearrayoutputstream ();
try {outputstream.writeto (cloneout);
System.out.println ("Cloneout content is:" + cloneout.tostring ());
catch (IOException e) {e.printstacktrace (); }
}
The results of the operation are as follows:
The contents of the current byte stream are as follows: ABCDEFG
writes the 2nd, 3, 4-byte stream in the letter array: Abcdefgijk The
number of bytes in the current output stream is:
97 98 99
the contents of 102 Cloneout are: Abcdefgijk
2 Source Analysis
2.1 Constructors
Bytearrayoutputstream has two constructors, and the difference is that the initial size is different.
/**
* Creates a new byte array output stream. The buffer capacity
is * Initially bytes and though its size increases if necessary.
* * Public
Bytearrayoutputstream () {this
;
}
/**
* Creates a new byte array output stream, with a buffer capacity of
* The specified size, in bytes.
*
* @param size the initial size.
* @exception illegalargumentexception if size is negative.
*/Public
bytearrayoutputstream (int size) {
if (size < 0) {
throw new IllegalArgumentException (" Negative Initial Size: "
+ size);
}
BUF = new Byte[size];
}
2.2 Write Method
/**
* Writes the specified byte to this byte array output stream.
*
* @param b The byte to be written.
* * Public
synchronized void write (int b) {
ensurecapacity (count + 1);//increase capacity, not enough capacity double
buf[count] = (byte) b; /write byte
count = 1;
}
/**
* writes <code>len</code> bytes from the specified byte array
* starting at offset <code>o Ff</code> to this byte array output stream.
*
* @param b The data.
* @param off the "start offset in" data.
* @param len the number of bytes to write.
* * Public
synchronized void write (byte b[], int out, int len) {
if (Off < 0) | | (Off > B.length) | | (Len < 0) | |
((off + len)-b.length > 0)) {
throw new indexoutofboundsexception ();
}
Ensurecapacity (count + len); Increase capacity, not enough capacity double
system.arraycopy (b, off, buf, Count, Len);//write byte array
count = Len;
2.3 WriteTo Method
/**
* Writes the complete contents of this byte array output stream
to * specified output stream argument, as If by calling the output
* stream ' s write method using <code>out.write (buf, 0, Count) </code>.
*
* @param out of the output stream to which to write the data.
* @exception IOException If an I/O error occurs.
* * Public
synchronized void WriteTo (OutputStream out) throws IOException {Out.write
(buf, 0, count);//will be current OUTP The contents of Utstream's buf are written to out
2.4 toString, Tobytearray method
/** * Creates a newly allocated byte array.
Its size was the current * size of that output stream and the valid contents of the buffer * have been copied into it.
* @return The current contents of this output stream, as a byte array. * @see java.io.bytearrayoutputstream#size () */Public synchronized Byte Tobytearray () [] {return arrays.copyof (bu F, Count); Returns the/** * Converts the buffer ' contents into a string decoding bytes using the * platform ' s default character s Et.
The length of the new <tt>String</tt> * is a function of the character set, and hence may isn't equal to the
* Size of the buffer. * <p> This method always replaces Malformed-input and unmappable-character * Sequences with the default Replacem ENT string for the platform ' s * default character set. The {@linkplain Java.nio.charset.CharsetDecoder} * class should be used while the decoding process is *
Required. * * @return String decoded from the BUFfer ' s contents. * @since JDK1.1 */Public synchronized string toString () {returns new string (BUF, 0, count);//Return String Object}
Reference:
[1] http://www.cnblogs.com/skywang12345/p/io_02.html
[2] Http://www.cnblogs.com/skywang12345/p/io_03.html
[3] http://blog.csdn.net/rcoder/article/details/6118313