How does BufferedOutputStream actually work at a low level?
我了解
然而,从查看EDOCX1的0个类和方法(BuffReDeUpToStudio.java)的实现来看,似乎最终,来自缓冲区的字节仅仅是逐字节写入的。
我认为这是因为:
在bufferedOutputstream.write(byte b[],int off,int len)中,它有out.write(b,off,len)行。因为out是outputstream的实例,但不是bufferedOutputstream,所以它正在调用outputstream.write(byte[],int,int)。这反过来使用for循环逐字节写入
请有人解释一下到底发生了什么,它是如何更快的?
刷新数据时,它是一个块。
1 2 3 4 5 6 7 | 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()以有效地处理数据块。
http://www.docjar.com/html/api/java/io/fileoutputstream.java.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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 <wyn>len</wyn> bytes from the specified byte array 310 * starting at offset <wyn>off</wyn> 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 } |
从您的链接:
1 2 3 4 5 6 7 | /** Flush the internal buffer */ private void flushBuffer() throws IOException { if (count > 0) { out.write(buf, 0, count); count = 0; } } |
…
1 2 3 4 5 6 7 8 9 10 11 | /** * Flushes this buffered output stream. This forces any buffered * output bytes to be written out to the underlying output stream. * * @exception IOException if an I/O error occurs. * @see java.io.FilterOutputStream#out */ public synchronized void flush() throws IOException { flushBuffer(); out.flush(); } |
号
如您所见,
其思想是,
代码规定:
1 2 3 4 5 6 7 | 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 } |
这是对当前缓冲的所有字节的写入。不是逐字节。