Introduction to Bytearrayoutputstream, source analysis and examples (including outputstream)
Learn Bytearrayinputstream in front, understand the "input stream." Next, we learn the output stream corresponding to the bytearrayinputstream, that is, the bytearrayoutputstream.
In this chapter, we will first introduce the Bytearrayoutputstream, after understanding its source code, then through the example to master how to use it.
Bytearrayoutputstream Introduction
Bytearrayoutputstream is a byte array output stream. It inherits from OutputStream.
The data in the Bytearrayoutputstream 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.
OutputStream Function List
Let's look at the function interface of Bytearrayoutputstream's parent class OutputStream.
Constructor
OutputStream () void close () void flush ()
void write (byte[] buffer, int offset, int count)
void write (byte[] buffer)
abstract void write (int onebyte)
Bytearrayoutputstream Function List
Constructor
Bytearrayoutputstream ()
bytearrayoutputstream (int size)
void close ()
synchronized void Reset ()
int size ()
synchronized byte[] tobytearray ()
String toString (int Hibyte)
string tostring (String charsetname)
string tostring ()
synchronized void Write (byte[] buffer, int offset, int len)
synchronized void write (int onebyte)
synchronized void WriteTo (OutputStream out)
OutputStream and Bytearrayoutputstream Source analysis
OutputStream is the bytearrayoutputstream of the parent class, we first look at the source of OutputStream, and then learn Bytearrayoutputstream source code.
1. Outputstream.java source Analysis (based on jdk1.7.40)
package java.io;
Public abstract class OutputStream implements Closeable, flushable {//writes Byte B to output stream.
It is implemented in subclasses!
public abstract void Write (int b) throws IOException;
Writes byte array b to the byte array output stream.
public void Write (byte b[]) throws IOException {write (b, 0, b.length); //Writes byte array B to "byte array output stream" and off is "starting position of array B", Len is the length of writing written to public void write (byte b[, int off, int len) throws Ioex
ception {if (b = = null) {throw new NullPointerException (); else if (Off < 0) | | (Off > B.length) | |
(Len < 0) | | ((off + len) > b.length) | | ((off + len) < 0))
{throw new indexoutofboundsexception ();
else if (len = = 0) {return;
for (int i = 0; i < len; i++) {write (B[off + i]);
} public void Flush () throws IOException {} public void Close () throws IOException {} }