Java-based inputstream. Read (byte [] B, int off, int Len) algorithm Learning

Source: Internet
Author: User

Java-based inputstream. Read (byte [] B, int off, int Len) algorithm Learning

public int read(byte[] b,                int off,                int len)         throws IOException
Maximum number of input streams lenData bytes are read into the byte array. Try to read up lenBytes, but may read a small number. Returns the actual number of bytes read as an integer.

This method is always blocked until the input data is available, the end of the stream is detected, or an exception is thrown.

IfbIsnull, Then throwNullPointerException.

IfoffIs negative, orlenIs negative, oroff+lenGreater than ArraybWill throwIndexOutOfBoundsException.

IflenIf the value is 0, no Bytes are readable and return0Otherwise, try to read at least one byte. If the stream is at the end of the file and there are no available bytes, the returned value is-1Otherwise, at least one byte can be read and stored inb.

Store the first byte read in the elementb[off]Inb[off+1]And so on. The maximum number of bytes read is equallen. LetKThe number of bytes actually read. These bytes are stored in the elementb[off]Tob[off+K-1]And other elementsb[off+K]Tob[off+len-1]Not affected.

Elementb[0]Tob[off]And Elementb[off+len]Tob[b.length-1]Will not be affected.

If the first byte cannot be read because the stream is not at the end of the fileIOException. In particular, if the input stream is disabledIOException.

ClassInputStreamOfread(b, off, len)The method is called repeatedly.read(). If the first such call causesIOExceptionFromread(b, off, len)This exception is returned when a method is called. Ifread()Any subsequent callIOException, The exception will be caught and the location where the exception occurred will be considered as the end of the file; the bytes read at this point are stored inbReturns the number of bytes read before an exception occurs. We recommend that subclass provide more effective implementation of this method.

 

Parameters:
b-Buffer for reading data.
off-The array where data is written b.
len-Maximum number of bytes to read.
Return Value:
The total number of bytes read into the buffer. If no data exists at the end of the stream -1.
Throw:
IOException-If an I/O error occurs.
NullPointerException-If bIs null.

 

Read is a good reader. It is a good algorithm for streaming! Example:
Public static final int initial_size = 100000;
Private byte buffer [] = new byte [initial_size];
Private int Index = 0;

Private int capacity (){
Return (buffer. Length-index );
}

Public void read (inputstream in, int max) throws ioexception {
Long K = 0;

Do {
Int size;

// Only read up to the max size, if the max size was
// Specified
If (max! =-1 ){
Size = math. Min (capacity (), Max );
} Else {
Size = capacity ();
}

// Actually read the block
K = in. Read (buffer, index, capacity ());

// Quit if we hit EOF
If (k <0 ){
Break;
}

// Adjust capacity if needed
Index + = K;

If (capacity () <10 ){
Expand ();
}

// See if we hit the max length
If (max! =-1 ){
Max-= L;
If (max <= 0 ){
Break;
}
}
} While (K! = 0 );
}

 

Algorithm

 

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.