Those things in JAVA IO

Source: Internet
Author: User

Topic One: Coding/transcoding

We know that in the network transmission process, in the final analysis, is to transmit the word stream, character stream (Writer/reader) is only in the byte stream (Inputstream/outputstream) on the basis of a package, is Java on the grammatical level to do something to us.


Let's take a look at the code first:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/74/D7/wKioL1YrPTqCuY0dAAHxAGvG_sU621.jpg "title=" Sogou _ 2015-10-24_16-11-54.png "alt=" Wkiol1yrptqcuy0daahxagvg_su621.jpg "/>


Operation Result:

Get Info: Trickle policy zoweer Optech Co. 锛 Imperial quantity untreated lian?

Get info: World, you??



Analysis:


First of all, S1 is garbled, this is good understanding, but why S2 is also garbled it?


First, S to form byte data by UTF-8 encoding B


Second, the receiver to get B, through the GBK to Transcode, get S1, it will obviously garbled.


More importantly, it is possible that B does not have any mappings at all in the GBK character set, so at this point GBK will specify some special symbols instead, such as.


Thirdly, the idea of using S1 to restore the original stream of bytes might fail!


Because in the second of the above, some bytes of data through the GBK transcoding, may be made, and? The byte data in GBK is probably different from the original byte data! So S2 was garbled.



This shows that in the process of encoding conversion, it is very likely to not turn back!



Topic Two: IO process of network exchange data

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/78/81/wKioL1Z-erijYrS_AAA2tUp6-xI359.png "title=" Sogou _ 2015-12-26_19-32-24.png "alt=" Wkiol1z-erijyrs_aaa2tup6-xi359.png "/>

As can be seen from the above figure, Java itself does not participate in the transmission of data on the network, Java just do is, if you want to send data, then the stream of bytes to kernel, if you need to read the data, then read the byte stream from the kernel.


Send and receive, if you take a consistent encoding, then it will not garbled!


Transmission in the network, should be a protocol, but with a byte stream expression, for example, the byte stream encoding, size, bytes content.


Topic Three: JAVA BIO

Java BIO, that is, the traditional IO, blocking IO, is also the most commonly used, the most basic IO, below to analyze it!


    • Input/output

Input: Enter the data from the disk, keyboard, network, memory input into the program;

Output: Outputs the data in the program to disk, monitor, network, memory;

Can say, without IO, there is no result, there is no value!


    • Byte Stream VS character stream

The source of the byte stream is: Inputstream/outputstream

The source of the character stream is: Reader/writer


First of all, we must forget the character stream, but understand IO from the point of view of the byte stream.


There is a character stream because, after all, the byte is not so visualized, thus providing a way to convert the stream of bytes into a character stream.


We can focus on the sub-categories of Reader/writer:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/78/87/wKiom1Z_RsmiB2eiAAATKDisQrQ181.png "title=" Sogou _ 2015-12-27_10-02-49.png "alt=" Wkiom1z_rsmib2eiaaatkdisqrq181.png "/>


650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/78/86/wKioL1Z_R0Cw-8_GAAASUbJR9Z0058.png "title=" Sogou _ 2015-12-27_10-04-29.png "alt=" Wkiol1z_r0cw-8_gaaasubjr9z0058.png "/>


It is clear to see that in the construction method provided by Inputstreamreader/outputstreamwriter, a byte stream is accepted as well as encoded. In fact, this is the word stream into a character stream bridge. We need to note that in this transformation process, you need to pay particular attention to coding problems, preferably manual, explicit coding, rather than the default environment coding what.


    • Datainputstream/dataoutputstream

One feature of this data flow is that it provides a variety of readxxx/writexxx methods, as follows:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/78/87/wKiom1Z_SsDgL4XJAAA4eM9PR64233.png "title=" Sogou _ 2015-12-27_10-19-11.png "alt=" Wkiom1z_ssdgl4xjaaa4em9pr64233.png "/>

Imagine, if we want to send an int to each other through the network, do we have to turn int into 4 bytes to send? This is too much trouble, and DataInputStream has already provided a lot of data format operation. In the case of socket communication, we can consider using this data stream when we need to encapsulate the IO operation of the socket.


    • Bytearrayinputstream/bytearrayoutputstream

The memory byte stream, which will wrap the bytes in memory into a stream, extending to the point where a string can get byte[] before it is converted into this memory byte stream.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/78/87/wKiom1Z_TZ2ipBPcAAAG7R8NSHg480.png "title=" Sogou _ 2015-12-27_10-32-33.png "alt=" Wkiom1z_tz2ipbpcaaag7r8nshg480.png "/>


    • Objectinputstream/objectoutputstream

Object byte stream, the idea of analogy, is to wrap the object into a byte stream, which can then be written to disk (persisted), can be sent over the network (the other party accepts, the byte stream is deserialized to get the object).


    • Compressed stream

Zipinputstream/gzinputstream/jarinputstream ...

These compressed streams can be nested on a file byte stream or on a network byte stream (to save network traffic).


    • Bufferedinputstream/bufferedoutputstream

If we want the buffer function, we can set a buffer stream, buffer will tend to hurry up, this is why?


Let's take a quick look at the implementation of Bufferedoutputstream:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/78/86/wKioL1Z_UaTBpxWVAAAEfYc6N48524.png "title=" Sogou _ 2015-12-27_10-49-39.png "alt=" Wkiol1z_uatbpxwvaaaefyc6n48524.png "/>

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/78/86/wKioL1Z_UcOh8gWUAAAKdQDFdAc565.png "title=" Sogou _ 2015-12-27_10-50-12.png "alt=" Wkiol1z_ucoh8gwuaaakdqdfdac565.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/78/86/wKioL1Z_USKRssWaAAAWEDZ_MJQ518.png "title=" Sogou _ 2015-12-27_10-47-29.png "alt=" Wkiol1z_uskrsswaaaawedz_mjq518.png "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/78/86/wKioL1Z_UUyypRrQAAAR3By3SyA028.png "title=" Sogou _ 2015-12-27_10-48-09.png "alt=" Wkiol1z_uuyyprrqaaar3by3sya028.png "/>

You can see that a byte[] is maintained inside the Bufferedoutputstream, and the size is 8192 in the case of the default construct.

If write, no more than byte[] size, then write byte[], this process is of course quite fast, if it is beyond the byte[] size, then Flushbuffer, and Flushbuffer is the real place to write, and is a one-time write byte[].

In fact, using the buffer function, the random write into a sequential write, of course, fast!


    • PrintStream

Print byte stream, we often use the System.out,system.err is the PrintStream object, provides a lot of data format Print method. In redirection, the use of System.setout/seterr is actually set to a printstream.


Topic Four: The idea of decoration in IO

The IO stream involves so many classes, each of which has its own characteristics, and if you want multiple functions/features to be clustered together, then the packaging and decorating functions are necessary. We can analyze the decorating idea with the simplest buffer function.

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/78/88/wKioL1Z_f6yBMgFSAAAbLj3oWMg281.png "title=" Sogou _ 2015-12-27_14-05-47.png "alt=" Wkiol1z_f6ybmgfsaaablj3owmg281.png "/>


By reading the source code of the Filteroutputstream class, we find that the Filteroutputstream class holds a reference to the OutputStream class, and that each of its methods is not doing anything, is to invoke the corresponding method for the reference to the OutputStream that is held.


Bufferedoutputstream extends Filteroutputstream, it internally through its own byte[], the implementation of the buffer function.


Here, we seem to have found a clue, and began to have some speculation:

Those features of the IO class, is not through the extends Filterxxxstream, and then within their own "play some means" to achieve specific functions?


650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/78/88/wKioL1Z_go3iS-kwAABlEmJUvlg281.png "title=" Sogou _ 2015-12-27_14-18-30.png "alt=" Wkiol1z_go3is-kwaablemjuvlg281.png "/>


Through, it can be found that there are some characteristics of the IO class extends Filterxxxstream, such as Dataxxxstream/bufferedxxxstream/printstream.


Filterxxxstream is like a proxy, although it does nothing, just invoke the corresponding method by reference, it seems useless, it is not, for example, want to implement the buffer function, only need extends Filterxxxstream, Then just rewrite the Read/write method, so that many of the code does not need to be written, because it has been obtained from the Filterxxxstream. Java such a design, ingenious!


Topic Five: Flush ()/close ()

Flow ran out, to close the flow, we often think of flush, to close, let's analyze and analyze them!


650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/78/89/wKiom1Z_h2zxiv0cAAAJbD4eLlk375.png "title=" Sogou _ 2015-12-27_14-39-59.png "alt=" Wkiom1z_h2zxiv0caaajbd4ellk375.png "/>

2 labeled interfaces, Closeable requires close,flushable to flush.


In OutputStream, they are actually empty methods:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/78/88/wKioL1Z_iLPwCRfyAAAIEyZFZSw969.png "title=" Sogou _ 2015-12-27_14-45-07.png "alt=" Wkiol1z_ilpwcrfyaaaieyzfzsw969.png "/>

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/78/89/wKiom1Z_iKbxJcA3AAAG_JwvX44604.png "title=" Sogou _ 2015-12-27_14-45-20.png "alt=" Wkiom1z_ikbxjca3aaag_jwvx44604.png "/>


It is clear that it is hoped that the subclass will go flush,close itself. If the buffer mechanism is used, especially if the function of the modifier has the characteristics of bufferedxxxstream, then it is important to pay attention to flush/close, which is actually to dispose of the data that is not processed in byte[].


Note that Filexxxstream does not rewrite the Flush method, and if the extends Filexxxstream class does not provide the implementation of flush, then an empty flush method in OutputStream is called! For example Socketoutputstream extends FileOutputStream, but did not provide a rewrite of the Flush method, that in the socket communication, we do not need to call the Flush method, it is at the kernel level buffer, Rather than Java-level.


So, for an IO class, do not flush, or close, we have to see if it extends who? Is there a buffer mechanism?


Topic Six: Java BIO vs NIO vs AIO

About Java Bio/nio, in the previous blog has been introduced, we can refer to the following:

http://zhangfengzhe.blog.51cto.com/8855103/1715530

http://zhangfengzhe.blog.51cto.com/8855103/1726488

http://zhangfengzhe.blog.51cto.com/8855103/1712845


Java BIO: Xiao Zhang classmate has a courier, so he went to the logistics station to pick up goods, he was there waiting, waiting until the express, during this period, he did not go.


Java NIO: Xiao Zhang, every day to a trip to the logistics station, the detection is not express delivery, if it comes, bring back, if not then return, tomorrow continue.


Java AIO: Xiao Zhang students do not go to the Logistics Transit station, if the goods arrived, then courier "Door-to-gate."


The above example, the image of the description of the Bio/nio/aio characteristics and differences, fat Brother said good, existence is valuable, AIO is the newest thing, the idea is good, but not necessarily suitable. For example, in the JDBC program, we sent a SQL, but this is an AIO, then the trouble, after issuing this SQL, the program immediately down, a period of time after the SQL results are sent, this time, we have to save the time to send out the SQL some information. Think about it, if such a lot of AIO, the program will be much more complex, save a lot of information, callback many methods.


This again illustrates a truth, If it is simple, then it must be very complex inside, it is because of its inherent complexity, resulting in the external simple!


Here, the JAVA io thing is over, have you learned it?






This article is from the "Boundless Mind Infinite" blog, please be sure to keep this source http://zhangfengzhe.blog.51cto.com/8855103/1728780

Those things in JAVA IO

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.