java. io. Is buffer * stream different from ordinary stream?

1) How do buffered streams work in the background, how are they different from normal streams, and what are the advantages of using them?

2) Datainputstream is also byte based But it has a way to readline() What are the main points here?

Solution

From bufferedinputstream Javadoc:

Internally, use a buffer array instead of reading bytes separately from the underlying input stream. Read enough bytes to fill the buffer This usually results in faster performance because the underlying input stream requires fewer reads

The opposite is true for buffered output stream

Mark () and reset () can be used as follows:

1 BufferedInputStream bis = new BufferedInputStream(is);
2 byte[] b = new byte[4];
3 bis.read(b); // read 4 bytes into b
4 bis.mark(10); // mark the stream at the current position - we can read 10 bytes before the mark point becomes invalid
5 bis.read(b); // read another 4 bytes into b
6 bis.reset(); // resets the position in the stream back to when mark was called
7 bis.read(b); // re-read the same 4 bytes as line 5 into b

Explain mark / reset some more

Bufferinputstream internally remembers the current position in the buffer When you read a byte, the position is incremented The phone marked (10) will save the current location Subsequent read calls will continue to increase the current position, but when mark is called, the call reset will set the current position to its value

The parameter of the tag specifies the number of bytes that can be read after the tag is invalid before the mark position is invalid. Once the marker position is invalid, you cannot call reset to return

For example, if mark (2) is used in line 4, an IOException will be thrown when reset () is called in line 6, because the mark position will be invalid because we read more than 2 bytes

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>