java.io.Buffer * stream与普通流有何不同? [英] How java.io.Buffer* stream differs from normal streams?

查看:203
本文介绍了java.io.Buffer * stream与普通流有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)缓冲流如何在后台工作,它们与普通流有什么不同?使用它们的优势是什么?

1) How does buffered streams work in background, how do they differ from normal streams and what are the advantage(s) of using them?

2) DataInputStream 也是 Byte 。但它有方法 readLine()。这里有什么意义?

2) DataInputStream is also Byte based. But it is having methods to readLine(). What's the point in here?

推荐答案

来自BufferedInputStream的javadoc:

From the BufferedInputStream javadoc:


BufferedInputStream为另一个输入流添加功能 - 即缓冲输入并支持标记和重置方法的功能。创建BufferedInputStream时,会创建一个内部缓冲区数组。当读取或跳过来自流的字节时,内部缓冲区根据需要从包含的输入流中重新填充,一次多个字节。标记操作记住输入流中的一个点,并且重置操作使得从最近的标记操作开始读取的所有字节在从包含的输入流中获取新字节之前被重新读取。

A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the input and to support the mark and reset methods. When the BufferedInputStream is created, an internal buffer array is created. As bytes from the stream are read or skipped, the internal buffer is refilled as necessary from the contained input stream, many bytes at a time. The mark operation remembers a point in the input stream and the reset operation causes all the bytes read since the most recent mark operation to be reread before new bytes are taken from the contained input stream.

在内部使用缓冲区数组,而不是从底层输入流中单独读取字节,而是读取足够的字节来填充缓冲区。这通常会导致更快的性能,因为底层输入流需要更少的读取。

Internally a buffer array is used and instead of reading bytes individually from the underlying input stream enough bytes are read to fill the buffer. This generally results in faster performance as less reads are required on the underlying input stream.

BufferedOutputStream则相反。

The opposite is then true for BufferedOutputStream.

mark()和reset()可以按如下方式使用:

mark() and reset() could 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

解释标记/重置更多...

To explain mark/reset some more...

BufferInputStream在内部记住缓冲区中的当前位置。当您读取字节时,位置将递增。对标记(10)的调用将保存当前位置。对读取的后续调用将继续增加当前位置,但是当调用mark时,调用reset将把当前位置设置回其值。

The BufferInputStream internally remembers the current position in the buffer. As you read bytes the position will increment. A call to mark(10) will save the current position. Subsequent calls to read will continue to increment the current position but a call to reset will set the current position back to its value when mark was called.

标记的参数指定在标记位置失效之前调用标记后可以读取的字节数。一旦标记位置无效,您就不能再调用reset来返回它。

The argument to mark specifies how many bytes you can read after calling mark before the mark position gets invalidated. Once the mark position is invalidated you can no longer call reset to return to it.

例如,如果在第4行中使用了mark(2),则在第6行调用reset()时会抛出IOException,因为标记位置会有因为我们读取的字节超过2个字节而失效。

For example, if mark(2) had been used in line 4 above an IOException would be thrown when reset() is called on line 6 as the mark position would have been invalidated since we read more than 2 bytes.

这篇关于java.io.Buffer * stream与普通流有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆