Author: Jakob Jenkov Translator: Li Jing ([email protected])
This section briefly summarizes the input and output streams of byte arrays and filters in Java IO, mainly involving the following 4 types of streams: Bytearrayinputstream,bytearrayoutputstream,filterinputstream, Filteroutputstream. Note that in order to be clear, the necessary exception handling is ignored. For more information about exception handling, refer to Java IO exception handling.
Bytearrayinputstream
Original link
Bytearrayinputstream allows you to read byte stream data from an array of bytes, with the following code:
01 |
byte[] bytes = ... //get byte array from somewhere. |
03 |
InputStream input = newByteArrayInputStream(bytes); |
05 |
intdata = input.read(); |
09 |
//do something with data |
If the data is stored in an array, Bytearrayinputstream can easily read the data. If you have a InputStream variable and want to read the data from the array? It is very simple to pass the byte array to the Bytearrayinputstream constructor, assigning the Bytearrayinputstream to the InputStream variable ( Translator Note: InputStream is the base class for all byte input streams, and reader is the base class for all character input streams, outputstream and writer.
Bytearrayoutputstream
Original link
Bytearrayoutputstream allows you to get the data written to the output stream as an array, with the following code:
1 |
ByteArrayOutputStream output = newByteArrayOutputStream(); |
3 |
//write data to output stream |
5 |
byte[] bytes = output.toByteArray(); |
FilterInputStream
Original link
FilterInputStream is the base class for implementing a custom filter input stream, basically it just covers all the methods in InputStream.
As far as I am concerned, I have not found this kind of obvious use. In addition to the constructor taking a InputStream variable as a parameter, I don't see any filterinputstream any additions or modifications to the InputStream. If you choose to inherit FilterInputStream to implement a custom class, you can also directly inherit from InputStream to avoid additional class hierarchies.
Filteroutputstream
Original link
Content with FilterInputStream, no longer repeat.
original articles, reproduced please specify: reproduced from the Concurrent programming network –ifeve.com This article link address: Java Io:bytearray and filter
Java Io:bytearray and filter