Javanio (i) (io basic Concept Literacy Article)

Source: Internet
Author: User

first, the basic IO concept

1. What is stream io?

1.1 Basic concepts

A stream is an abstract concept that represents the unstructured transfer of data . The input and output are carried out as a stream, and the data is treated as an unstructured byte sequence or a sequence of characters. The operation to get data from a stream is called an extract operation, and the operation to add data to the stream is called an insert Operation. The stream used for the input and output operation is called the IO Stream. In other words, an IO stream is a stream of input and Output. Input and output (IO) refers to the transfer of data between the computer and any external devices. Common input are files, keyboards, printers, screens, and so On. Data can be passed in the form of records (or blocks of Data) or Streams.

1.2 Image metaphor

  The concept of stream is derived from the concept of piping (pipe) in unix. In unix, a pipeline is an uninterrupted stream of bytes used to communicate between programs or processes, or to read and write peripherals, external files, and so On. a stream, which must have both the source and destination, can be some area of computer memory, a disk file, or even a URL on the Internet. The direction of the flow is important, depending on the direction of the flow, the flow can be divided into two categories: the input stream and the output Stream. The user can read the information from the input stream, but cannot write it. instead, the output stream can only be written to the input stream, not read it.
In fact, the source and destination of a stream can simply be seen as a producer and consumer of bytes, and for an input stream, you don't have to care what its source is, simply read the data from the stream, and on the output stream, you don't know its destination, just simply write the data to the Stream. image metaphor-water flow, file ====== program, file and program connect a pipe, the water flow is formed between, naturally there is a direction: can flow into, can also outflow. Easy to understand, so define the Flow: a stream is a pipe with flowing water, and this pipe connects files and Programs.

2. The difference between a byte stream and a character stream

   The main difference between a byte stream and a character stream is their way of handling.

Byte stream is the most basic, all InputStream and OutputStream subclasses are, mainly used in processing binary data, it is processed by Byte. But in fact, a lot of data is text, and put forward the concept of character stream, it is the virtual machine encode to deal with, that is, to do the conversion of the character set between the two through Inputstreamreader,outputstreamwriter () to correlate, is actually associated with byte[] and String. The problem of Chinese characters in the actual development is actually caused by the non-unification between the character stream and the byte Stream.

public string (byte bytes[], string Charsetname)

When converting from byte to stream, it is actually byte[] when converted to string, there is a key parameter character set encoding, which is usually omitted, and the system uses the operating system Lang. The same is true of byte[] string.getbytes (string Charsetname) when the character stream is converted into a byte flow, which is actually a string conversion to byte[]. As for java.io, there are many other streams that are mainly designed to improve performance and ease of use, such as Bufferedinputstream,pipedinputstream.

Note:InputStreamReader Source code

//It's a byte stream That's changed to character streams, in some sort of coded way.public InputStreamReader (inputstream In) {super (in); try {// The Streamdecoder source here does not see SD = Streamdecoder.forinputstreamreader (in, this, (String) Span style= "color: #0000ff;" >null); // # # check lock object}  (unsupportedencodingexception e) {// The default encoding should always be available throw new  Error (e);} } 

Personal understanding:

1, in terms of Io flow, the "stream" is always a non-structured sequence of bytes, the difference between a byte stream and a character stream is that they are processing these bytes based on different "protocols":

2, byte stream----in accordance with byte-oriented processing of data, support 8-bit structure of bytes, not very good processing of 16-bit Unicode Characters. Can only be used to process images, audio and other files;

The character stream----handle data in a character-oriented way, and can handle 16-bit Unicode characters (such as char types) better and handle character Types. (increase Efficiency)

3, when processing the data source, try to use character Stream. In addition, a large number of adorner patterns are used in the IO stream: This is also the processing of Single-class objects, but to create so many Objects. Improve Productivity.

2. What is block io?

2.1 Basic Concepts

The so-called records refer to data blocks with internal Structures. In addition to the actual data that needs to be processed, the internal records may contain additional information, which is usually a description of the data in this Record.

3. What is the difference between Java stream IO and block io?

    The operating system is not unable to transfer data quickly, so that Java has something to do, instead, the JVM itself is inefficient in i/o.There are some mismatches between the operating system and the Java stream-based I/O model . The operating system is moving large chunks of data (buffers), which are often done with the assistance of hardware direct memory access (DMA). The I/O classes of the JVM like to manipulate small pieces of data--single bytes, a few lines of Text. As a result, the operating system sends the whole buffer data, java.io the stream data class to spend a lot of time to split them into small pieces, often copying a small piece will be to and from several layers of objects. The operating system likes to ship data to the truck, and the Java.io class likes to process the data with a shovel and a shovel. With NIO, you can easily back up a Truck's data to where you can use it directly (Bytebuffer objects ).
1) System for stream i/o: processing one byte of data at a Time. An input stream reads one byte of data at a time, and an output stream consumes one byte of data at a Time. For streaming data, It's easy to create Filters. Can be relatively simple to connect a few filters together, each filter to complete its own work, but also by byte filtering, fine processing Mechanism. On the other hand, flow-oriented I/O communication tends to be slow.
2) block i/o-oriented systems: processing data in blocks. Each action step generates or consumes a block of Data. The processing of data in blocks is much faster than a byte stream. however, block i/o-oriented communication lacks elegance and simplicity compared to flow-oriented I/O Traffic. however, There is higher data throughput (the number of successful data transfers per unit of time for a network, device, port, virtual circuit, or other facility).

Personal understanding:

Nio:nio model, no longer uses the original IO model (read and write data to the stream), but to read and write data from the buffer to the Channel. Then someone asked again, my Io stream object after Bufferinputstream packaging, can not also press "buffer" read it?

however,The underlying implementation of the NIO buffer connects the buffer Bytebuffer directly to the hardware or memory, while the IO is the operating system memory that copies the data into the JVM memory and then operates on the Jvm's memory.

4, Stream and channel difference: from a programming point of view, the stream is byte-based, and the channel is Block-based.

1. From the basic concept, the flow is designed to transmit one byte of data at a Time. The channel transmits the block of data in the Buffer. Before the bytes can read and write to the channel, these bytes must exist in the buffer and read and write one buffer at a Time.

2, the channel and buffer support for the same object read and write, the program can read and write the same channel, and the stream, divided into Read/write two.

3. In addition to the data list, each buffer also records 4 key parts of the Information:

The position (POS) buffer will be read or written to the next Location.

The maximum number of elements that the capacity (cap) buffer can hold.

The limit (lim) buffer can access the end position of the Data.

The index of the token (mark) buffer client Guideline.

4, Unlike reading inputstream, the read buffer does not actually alter the data in the buffer in any way. You can only set the position forward or backward, allowing you to start reading (flexible) from a specific position in the Buffer. The IO stream can only begin to read and write (fixed) from one location to Another. buffer is essentially an array of implementations that act as a memory, or a segmented transport area, and provide structured access to the data, and can also track the read/write process of the System.

second, synchronous/asynchronous blocking/non-blocking

(thorough version: Http://www.zhihu.com/question/19732473)

(detailed version: HTTP://WWW.CNBLOGS.COM/ZHUYEARS/ARCHIVE/2012/09/28/2690194.HTML)

The personal understanding of these two posts:

1, Synchronous and asynchronous (message notification mechanism):

Synchronization: when a thread goes to invoke a resource, if the resource is not ready, the thread waits, and when the resource is ready, the thread invokes the resource and returns the resource;

Async: the thread is only responsible for issuing a call notification, and I return immediately regardless of the resource being Prepared. When resources are ready, they will return in other Ways. (kernel return, callback is used in Linux)

2, blocking and non-blocking: the status of the Thread's wait call result:

Blocking: If you are not ready to tune a resource, the thread is suspended, waits until the resource is ready, returns the identity that can be called, and the thread is "enabled".

Non-blocking: if the resource is ready when the resource is tuned, the thread can directly pull the resource, and if the resource is not ready, then the thread will handle something else and occasionally come to see if the resource is ready (the flag bit returned by the Resource)

Ps: the usual easy to confuse things to record down, think of anything added, so some chaos ... The back will formally step into javanio.

Reference Blog:

Http://www.cnblogs.com/pepcod/archive/2013/01/20/2913435.html

Baidu Encyclopedia

http://my.oschina.net/OutOfMemory/blog/95853

The fourth edition of Java Network programming

Javanio (i) (io basic Concept Literacy Article)

Related Article

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.