Java IO Bytearrayinputstream/bytearrayoutputstream
@author Ixenos
Bytearrayinputstream
Contains an internal buffer (byte array byte[]) that contains bytes read from the stream.
Closing bytearrayinputstream is not valid. The methods in this class can still be called after the stream is closed, without generating any ioexception.
# Constructors
Bytearrayinputstream (byte[] buf)//using the Creator preset buf as its buffer array, usually BUF is the data source Bytearrayinputstream (byte[] buf, int Offset, int lenght)//usebuf
# Overloads a Read method (another read () and the InputStream definition)
int read(byte[] b, int off, int len)
Reads each byte into the specified byte array starting at the given offset from this byte input stream.
Returns the number of bytes read (this time), or returns if the end of the stream has been reached -1 . And the return of Read () is the next byte
# for each byte-type element that is read, it is converted to the int type (the Read method returns the int type)and the data is collected by an int container (4B), so the int output will be different from the byte input at this point.
Bytearrayoutputstream
Creates a byte array buffer in memory, and all data sent to the output stream is saved in the byte array buffer .
The buffer grows automatically as data is written continuously. The default construct is 32
data can be used toByteArray() and toString() obtained .
Closing bytearrayoutputstream is not valid. The methods in this class can still be called after the stream is closed, without generating any ioexception.
# Field protected the number of valid bytes in the INT count buffer, which represents the number of bytes of data in the current buffer
#public void Reset () Resets the field of this byte array output stream count to zero, discarding any output currently accumulated in the output stream. You can use the output stream again by re-using the allocated buffer space.
# Constructors
OutputStream bOut = new Bytearrayoutputstream (); Creates a 32-byte (default size) buffer, internally this (32), which calls another constructor outputstream BOut = new Bytearrayoutputstream (int a); Create a buffer of size a byte, which is new Byte[a]
# Methods
Public byte[] Tobytearray ()
Creates a newly allocated byte array. The size of the array and the size of the current output stream, where the content is a copy of the current output stream. |
Public String toString ()
Converts the contents of a buffer to a string, converting bytes into characters based on the default character encoding of the platform. |
public void Write (int w) writes The specified bytes to this byte array output stream. |
public void Write (byte []b, int of, int len)
Writes bytes in the specified byte array from off the offset to len this byte array output stream. |
public void WriteTo (OutputStream outst)
Writes the entire contents of this byte array output stream to the specified output stream parameters. |
About internal buffers
The buffers are not associated when they are used at the same time, but the two principles are consistent, as intermediate caches
#ByteArrayInputStream First provide a buffer to hold the required data, the array equivalent to the data source, read, remove sender from the array----->buf------>you
1//reading a byte of data through the input stream is actually read in the buffer buf2//BUF-can be thought of as a data source3 Public synchronized intRead () {4return(Pos < count)? (buf[pos++] & 0xff): 1; 5 } 6 7//reads the buffer's data through the input stream and reads it into the storage array B8//B-storage of buffers that read into data9 Public synchronized intReadbyteB[],intOffintLen) {10if(b = =NULL) {11Throw Newnullpointerexception ();12}Else if(Off < 0 | | Len < 0 | | len > B.length-off) {13Throw Newindexoutofboundsexception ();14 }15 16if(Pos >=count) {17return-1;18 }19 20intAvail = count-Pos;21stif(Len >avail) {Len =avail;23 }24if(Len <= 0) {25return0;26 }System.arraycopy (buf, POS, B, off, Len);//because it is an array of arrays, copy it directly past+ pos + =Len;29returnLen;30}View Code
The data sent #ByteArrayOutputStream The data source is temporarily saved in the internal buffer, and when written out, you------>buf from the array------>receiver
When we use the output stream, we are the data source, and we can decide what output to make, which may not be very related to what the input stream gives. For example, write (int w) writes a Byte (byte) W to the buffer, and the writes eventually go into the buffer.
To get a buffer byte array , either return a copy of the current output stream via Bytearrayoutputstream's Tobytearray (), or ToString () to convert the buffer array contents to a string by default encoding
Public synchronized byte Tobytearray () [] { return arrays.copyof (buf, count);}
Public synchronized string toString () { returnnew String (buf, 0, Count);}
Java IO Bytearrayinputstream/bytearrayoutputstream