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
len
Data bytes are read into the byte array. Try to read up
len
Bytes, 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.
Ifb
Isnull
, Then throwNullPointerException
.
Ifoff
Is negative, orlen
Is negative, oroff+len
Greater than Arrayb
Will throwIndexOutOfBoundsException
.
Iflen
If the value is 0, no Bytes are readable and return0
Otherwise, 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-1
Otherwise, 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
.
ClassInputStream
Ofread(b,
off,
len)
The method is called repeatedly.read()
. If the first such call causesIOException
Fromread(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 inb
Returns 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
b
Is
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