Unreleased Resource (unreleased Resource)-Streams (Stream), Unreleased-streams

Source: Internet
Author: User

Unreleased Resource (unreleased Resource)-Streams (Stream), Unreleased-streams

  Java abstracts different input/output sources (such as keyboards, files, and network connections) into streams ).

Java programs can use different streams to access different input/output sources. stream can be intuitively understood as such ordered data from the data Source to the data receiving (Sink.

Ps. Note that Stream is not the Scream (Scream) of the popular sports drink or the Cream of ice Cream.

Let's take a look at it as a reference. In our mind, the water drops on the way are the data in the stream, which together constitute the ordered data (Water: Stream ).

 

 

1. stream classification

1. stream direction (from the perspective of memory where the program runs)

Input stream: data can only be read from the stream and cannot be written into it. (base class InputStream, Reader)

Output stream: Only data can be written to it, and data cannot be read from it (base class OutputStream, Writer)

2. Different data units

Byte stream: the minimum data unit for operations is 8 bits. It is mainly based on InputStream and OutputSteam.

Bytes stream: the minimum data unit for an operation is a character (16 bit), mainly based on Reader and Writer.

3. Stream-based role score

Node stream: the stream that reads and writes data from/to a specific IO Device. This Stream belongs to a Low level Stream.

This stream is often directly connected to the actual (anti-theft connection: This article first from the http://www.cnblogs.com/jilodream/) data source, that is, the actual input and output node directly connected.

Stream processing (Packaging stream): used to connect and encapsulate existing streams. The encapsulated stream is used to read and write data. This type of stream often becomes an advanced stream.

This type of stream is encapsulated for different node streams, so that the program can access different data sources using identical Input and Output code.

Is a typical appearance mode. That is, the use and inheritance of the original class are not changed. You can create a packaging object to package real data. Here, the use and inheritance of the original class are node streams. The feature is extended by wrapping the stream to package real data.

 

 

Ii. concept model of stream

Stream can be understood as: the input device is abstracted into a "pipe", each "water drop" in the pipe is sorted in sequence, each water drop can be understood as the unit of input and output (that is, the bytes and characters mentioned earlier ). A sequence formed by these water drops is a stream.

The input stream uses an implicit record pointer to indicate the water drop from which the data is being read. Every time the program extracts one or more "water drops" from inputStream/Reader, the record pointer will automatically move backward.

The output stream can be understood as a pipe edge that has been directed to the target, and the program will prevent the output "water drops" from being in the pipeline to be output at a time.

 

In this way, we can understand that the stream not only allocates space in the memory, but also occupies the operating system resources.

The following is an example.

 

1 public class TempStreamModle 2 {3 public static void main (String [] args) 4 {5 PrintStream ps = null; 6 try 7 {8 ps = new PrintStream (new FileOutputStream ("out.txt"); // create the output stream 9 System. setOut (ps); // specify the output stream to ps stream 10 System. out. println ("common String"); 11 System. out. println (new TempStreamModle (); 12} 13 catch (IOException ex) 14 {15 // solve error16} 17 finally18 {19 if (ps! = Null) 20 {21 ps. close (); 22} 23} 24} 25}

 

Java GC can only recycle objects not used in jvm, and other resources allocated by the operating system cannot be recycled. Therefore, we need to actively call close ()

The advantages of calling the close () method are:

(1) ensure that the physical resources occupied by the stream can be released

(2) flush the data in the output stream buffer to the physical node. It is equivalent to actively executing the flush () method.

 

PS. when using a stream in Java, there is usually a buffer, which is considered to be a more efficient method to send data: each time the application IO needs to communicate with the device, the efficiency is very low, therefore, the buffer in order to improve efficiency, when writing to the device, will first write buffer, wait until the buffer has enough data (anti-theft connection: This article first from the http://www.cnblogs.com/jilodream, write the entire device (note that this idea is also used to ensure that the interface does not flash when drawing or dragging the GUI ). Flush () indicates that the data in the buffer zone is forcibly sent out without waiting until the buffer zone is full. Therefore, if the flush () method is not used when a stream is used, data cannot be read on the other side of the stream in many cases, especially when the data is very small.

This is equivalent to a sightseeing car in a tourist area. When people are full, the shuttle bus will depart (flush). However, no matter how many people are on the last shuttle bus, the last shuttle bus will be sent when you get off work, instead of waiting until the car is full (the buffer zone is full ).

 

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.