Java Network---Streaming

Source: Internet
Author: User

A large part of the network operation is the input and output data.

Simple generalization is uploading and downloading files. Files are also a carrier of data.

Java operations on data are merged into streams.

Therefore, the operation of the data flow defines 2 basic classes.

Java.io.OutputStream

Java.io.InputStream

One: OutputStream & InputStream

The output stream. That is, writing data, where there is an easy-to-ignore approach: flush.

     Public void throws IOException {    }

The function of this method, each time we write a character to the file or other place is very redundant, if possible, then there is a cache, when I reach a certain amount of data,

To begin the write operation.

So call:

 Public void Write (byteintintthrows

The data is not necessarily written to the file at once, and if close, the data is lost.

The function of flush is, in the buffer, regardless of whether the data reaches a critical value, forcing the data to write to the file!

The input stream, as opposed to the output stream.

There are many ways. The most important of course is read.

Reading a single character is obviously not a good way to do it.

     Public int Read (byteintintthrows  ioexception {        return iobridge.read (fd, buffer, Byteoffset, byteCount);    }

This is the way Android Java/io/fileinputstream.java read multiple characters.

Because the network is busy, it usually transmits 1024 bytes of content, it may only receive 512, and the remainder is still in transit.

Two: Buffer stream

Bufferedinputstream and Bufferedoutputstream

They will cache InputStream & OutputStream and then improve the performance of the transfer.

For a network transmission, 1 bytes are passed at a time, but it is 40 bytes of headers, so the 1K data becomes 41K.

And if packaged at once, then as long as the 1K multipoint data can be, performance will obviously be much worse.

@Override Public synchronized intRead ()throwsIOException {//Use local refs since buf and is invalidated by an//unsynchronized Close ()        byte[] Localbuf =buf; InputStream Localin=In ; if(Localbuf = =NULL|| Localin = =NULL) {            Throwstreamclosed (); }        /*is there buffered bytes available?*/ if (POS >= count && fillbuf (Localin, localbuf) = =-1) {  return -1 ; /* No, fill buffer */         }                //Localbuf May has been invalidated by Fillbuf        if(Localbuf! =buf) {Localbuf=buf; if(Localbuf = =NULL) {                Throwstreamclosed (); }        }        /*did filling the buffer fail with-1 (EOF)?*/        if(Count-pos > 0) {            returnlocalbuf[pos++] & 0xFF; }        return-1; }

The key is to fillbuf this operation, so the Read method reads a byte at a time, rather than reading it sequentially.

Private intFillbuf (InputStream Localin,byte[] localbuf)throwsIOException {if(Markpos = =-1 | | (Pos-markpos >=marklimit)) {            /*Mark position not set or exceeded Readlimit*/            intresult =Localin.read (LOCALBUF); if(Result > 0) {Markpos=-1; POS= 0; Count= result = =-1? 0: Result; }            returnresult; }        if(Markpos = = 0 && marklimit >localbuf.length) {/*increase buffer size to accommodate the readlimit*/            intNewlength = Localbuf.length * 2; if(Newlength >marklimit) {Newlength=Marklimit; }            byte[] Newbuf =New byte[Newlength]; System.arraycopy (Localbuf,0, Newbuf, 0, localbuf.length); //reassign buf, which would invalidate any local references//Fixme:what If buf was null?Localbuf = BUF =Newbuf; } Else if(Markpos > 0) {system.arraycopy (Localbuf, Markpos, Localbuf,0, Localbuf.length-Markpos); }        /*Set The new position and mark position*/POS-=Markpos; Count= Markpos = 0; intBytesread = Localin.read (Localbuf, POS, Localbuf.length-POS); Count= Bytesread <= 0? Pos:pos +Bytesread; returnBytesread; }

You can see that the fillbuf inside will call

int bytesread = Localin.read (Localbuf, POS, localbuf.length-pos);

So buffer will put the stream into an array of byte[].

Three: Compressed stream:

Deflateroutputstreaminflaterinputstreamgzipoutputstreamgzipinputstreamzipoutputstreamzipinputstream

These six are the compressed streams provided by Java, and their reading and writing is using the Read & Write method.

The output stream compresses the data and the input stream extracts the data.

IV: Digest stream

Digestinputstream,digestoutputstream

The purpose of the digest stream is primarily to verify the integrity of the data.

While programmers can write independent validation algorithms more efficiently, they can be simplified in a unified use.

Five: Reader and writer

     InputStreamReader, linenumberreader        , BufferedReader, Reader  - > bufferedwriter       , OutputStreamWriter, FileWriter

There are various character encodings in the network transmission, so there is a need for a read-write and processing tool to manipulate the flow.

FileReader & FileWrite is processing the file, the specific details will not write!

Java Network---Streaming

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.