Java Io:bytearray and filter

Source: Internet
Author: User

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.
02
03 InputStream input = newByteArrayInputStream(bytes);
04
05 intdata = input.read();
06
07 while(data != -1) {
08
09     //do something with data
10
11     data = input.read();
12
13 }
14
15 input.close();

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();
2
3 //write data to output stream
4
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.