1. The FileInputStream class directly inherits the InputStream method
Mark, marksupported, reset method
And these methods are all empty implementations
Marksupported Direct return False
Therefore, for the FileInputStream class does not support random access to files, only sequential read and write files.
2. The buffer operation in NIO:
Java.nio.Buffer in NiO almost all operations of this class are about a linear buffer, for the abstraction of buffers, this provides the following 4 indicator bits:
Invariants:mark <= Position <= limit <= capacity
Private int mark =-1;
Private int position = 0;
Private int limit;
Private int capacity;
capacity : The capacity of the buffer
The capacity is the capacity size of the buffer, which indicates the number of elements that the buffer can store. Here is not the number of bytes, but the number of element, if intbuffer, then its bytes should be:
Capacity * 4:
A Buffer ' s capacity is the number of elements it contains. The capacity of a buffer is never negative and never changes. This value will not change after the buffer has been initialized well. It can be done by:capacity()方法获得。
Position:缓冲区的当前读写位置
表示缓冲区的当前位置,当缓冲区下一次调用get或者put方法时,就从 position++的位置开始。
Final int Nextgetindex () {//Package-private
if (Position >= limit)
Throw New Bufferunderflowexception ();
return position++; The next read and write location.
}
查看:Java.nio.HeapByteBuffer put, get method,
Public Bytebuffer put (byte x) {
Hb[ix (Nextputindex ())] = x;
return this;
}
Public byte get () {
return Hb[ix (Nextgetindex ())];
}
The above code shows that the read and write operation of buffer is occurring in the position position.
Buffer provides two ways to manipulate position:
Int |
position () Returns This buffer ' s position. |
Buffer |
position (int newposition) |
Visible we can manually adjust the read and write location.
3.mark
Mark and reset are often a pair of operations, where Mark's semantics and functionality are identical to the Mark functionality provided in the InputStream stream class.
Marks the current position in this input stream. A subsequent Callto the reset method repositions this stream at the last marked position so thatsubsequent reads re-read T He same bytes.
is to record the current position in the caller's method,
This is the implementation of the mark of Buffer:
Public Final Buffer Mark () {
Mark = position;
return this;
}
This is the implementation of the Bytearrayinputstream class:
Public void mark (int readaheadlimit) {
Mark = pos;
}
Call the Reset method so that position is assigned the value of the last call to the Mark method saved in Mark:
Public Final Buffer Reset () {
int m = mark;
if (M < 0)
Throw New Invalidmarkexception ();
Position = m;
return this;
}
4. Limit
The role of limit: used to restrict the location of read and write
Public Final Buffer limit (int newlimit) {
if ((Newlimit > capacity) | | (Newlimit < 0))
Throw New IllegalArgumentException ();
Limit = Newlimit;
if (Position > Limit) position = limit;
if (Mark > Limit) Mark =-1;
return this;
}
3.java.io
The ReadLine method of the BufferedReader class calls the Fill () method---.
Fill:
n = in.read (CB, DST, CB.LENGTH-DST);
Java.io.Reader. Is the method of this class.
The Read method for this class is:
Sd.read (cbuf, offset, length);
SD = Streamdecoder. Forinputstreamreader (In, this, charsetname);
Where in is the InputStream
So ultimately it's called the Read method to read the data,
Streaming IO Read-write