Java BufferedOutputStream实际上是如何在低级别上工作的?

Java BufferedOutputStream实际上是如何在低级别上工作的?,java,buffer,flush,bufferedoutputstream,Java,Buffer,Flush,Bufferedoutputstream,我理解缓冲输出流背后的理论。字节被写入缓冲区数组,直到其满为止,然后写入(刷新)到底层流——其思想是,它比逐字节写入快,因为操作系统调用更少 然而,从BufferedOutputStream类和方法()的实现来看,似乎最终缓冲区中的字节只是逐字节写入的 我认为这是因为: 在BufferedOutputStream.write(字节b[],int off,int len)中,它有一行out.write(b,off,len)。因为out是OutputStream的实例,但不是BufferedOutp

我理解
缓冲输出流
背后的理论。字节被写入缓冲区数组,直到其满为止,然后写入(刷新)到底层流——其思想是,它比逐字节写入快,因为操作系统调用更少

然而,从
BufferedOutputStream
类和方法()的实现来看,似乎最终缓冲区中的字节只是逐字节写入的

我认为这是因为:

在BufferedOutputStream.write(字节b[],int off,int len)中,它有一行out.write(b,off,len)。因为out是OutputStream的实例,但不是BufferedOutputStream,所以它调用OutputStream.write(字节[],int,int)。这又使用for循环逐字节写入

请有人澄清一下到底发生了什么,以及它是如何更快的?

从您的链接:

   /** Flush the internal buffer */
   private void flushBuffer() throws IOException {
       if (count > 0) {
           out.write(buf, 0, count);
           count = 0;
       }
   }

如您所见,
flush()
一次性将所有缓冲区内容写入底层流,然后级联刷新
BufferedOutputStream
然后重新实现
写入(字节b[],int off,int len)
void write(int b)
(类中的核心方法,每次写入都委托给它),以便它写入缓冲区,在必要时刷新。

代码说明:

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       }

这是对所有当前缓冲字节的写入。不是逐字节。

其思想是
缓冲输出流的用户不必等待每个字节真正发送。用户只需将较大的块推送到outputstream并继续,即使连接本身很慢。所以这一边的速度更快。outputstream本身试图尽可能快。

刷新数据时,它是一个块

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和其他许多重写OutputStream.write()的方法可以有效地处理数据块


使用
out刷新缓冲区。写入(字节、偏移量、长度)
如何逐字节写入?你能说得更具体些吗?但是不能out.write(字节、偏移量、长度)调用OutputStream.java中的write(字节b[],int off,int len)方法吗?然后使用for循环将每个字节分别写入缓冲区?
OutputStream
是抽象的。大多数子类将以更高效的版本覆盖此方法。例如,在OP上的注释可能会有所帮助。如果写入(缓冲区、开始、len)未被重写,则缓冲区是无意义的。大多数流都会覆盖它。在BufferedOutputStream.write(字节b[],int off,int len)中,它具有write out.write(b,off,len)。这是指哪个写操作?它不是OutputStream.write(byte[],int,int)?它反过来使用for循环逐字节写入?如果它被重写,则不是这样,通常是这样。e、 g.在
FileOutputStream.write(byte[],int,int)
中,但不写(buf,0,count)是通过逐字节进行工作的。。。对于(inti=0;i79 /** 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 }
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       }