BufferedOutputStream如何实际工作在较低水平? [英] How does BufferedOutputStream actually work at a low level?

查看:147
本文介绍了BufferedOutputStream如何实际工作在较低水平?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解 BufferedOutputStream 背后的理论。字节被写入缓冲区数组直到它已满,然后写入(刷新)到底层流 - 这个想法是它比逐字节写入更快,因为操作系统调用更少。



然而,从查看 BufferedOutputStream 类和方法的实现( BufferedOutputStream.java ),似乎最终,缓冲区中的字节只是逐字节写入。 / p>

我认为情况是这样的:



在BufferedOutputStream.write(byte b [],int off, int len)它有行out.write(b,off,len)。由于out是OutputStream的实例,而不是BufferedOutputStream,因此它调用OutputStream.write(byte [],int,int)。这又使用for循环逐字节写入



请有人澄清实际发生了什么,以及它如何更快?

解决方案

刷新数据时,它是一个块。

  79 / **刷新内部缓冲区* / 
80 private void flushBuffer()抛出IOException {
81 if(count> 0){
82 out.write (buf,0,count);
83 count = 0;
84}
85}






FileOutputStream和许多其他覆盖OutputStream.write()来有效地处理数据块。



http://www.docjar.com/html/api/java/io/FileOutputStream.java.html

  284 
285 / **
286 *将子数组写为字节序列。
287 * @param b要写入的数据
288 * @param关闭数据中的起始偏移量
289 * @param len写入的字节数
290 * @param附加{@code true}以首先将位置提升到
291 *文件结尾
292 * @exception IOException如果发生了I / O错误。
293 * /
294 private native void writeBytes(byte b [],int off,int len,boolean append)
295 throws IOException;

308 / **
309 *写< code> len< / code>来自指定字节数组的字节
310 *从offset< code> off< / code>开始到这个文件输出流。
311 *
312 * @param b数据。
313 * @param关闭数据中的起始偏移量。
314 * @param len要写入的字节数。
315 * @exception IOException如果发生I / O错误。
316 * /
317 public void write(byte b [],int off,int len)抛出IOException {
318 writeBytes(b,off,len,append);
319}


I understand the theory behind BufferedOutputStream. Bytes are written to a buffer array until it is full, and then written (flushed) to the underlying stream - the idea being that it is faster than writing byte-by-byte as there are fewer OS calls.

However, from looking at the implementation of the BufferedOutputStream class and methods (BufferedOutputStream.java), it seems that ultimately, the bytes from the buffer are just written byte-by-byte.

I think this is the case because:

In BufferedOutputStream.write(byte b[], int off, int len) it has the line out.write(b, off, len). Since out is an instance of OutputStream, but not BufferedOutputStream, it is calling OutputStream.write(byte[], int, int). This in turn uses a for loop to write byte-by-byte

Please could someone clarify what is actually going on, and how it is faster?

解决方案

When the data is flushed, it is as a block.

79       /** Flush the internal buffer */
80       private void flushBuffer() throws IOException {
81           if (count > 0) {
82               out.write(buf, 0, count);
83               count = 0;
84           }
85       }


FileOutputStream and many other override OutputStream.write() to handle blocks of data efficiently.

http://www.docjar.com/html/api/java/io/FileOutputStream.java.html

284   
285       /**
286        * Writes a sub array as a sequence of bytes.
287        * @param b the data to be written
288        * @param off the start offset in the data
289        * @param len the number of bytes that are written
290        * @param append {@code true} to first advance the position to the
291        *     end of file
292        * @exception IOException If an I/O error has occurred.
293        */
294       private native void writeBytes(byte b[], int off, int len, boolean append)
295           throws IOException;

308       /**
309        * Writes <code>len</code> bytes from the specified byte array
310        * starting at offset <code>off</code> to this file output stream.
311        *
312        * @param      b     the data.
313        * @param      off   the start offset in the data.
314        * @param      len   the number of bytes to write.
315        * @exception  IOException  if an I/O error occurs.
316        */
317       public void write(byte b[], int off, int len) throws IOException {
318           writeBytes(b, off, len, append);
319       }

这篇关于BufferedOutputStream如何实际工作在较低水平?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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