How does BufferedOutputStream work at a low level?

I understand the theory behind BufferedOutputStream . Bytes are written to the buffer array until it is full, and then written (flushed) to the base stream - the idea is that it is faster than writing bytes, since there are fewer OS calls.

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

I think this is so because:

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

Please, can someone clarify what is actually happening, and how is this faster?

+4
source share
4 answers

From your link:

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

...

  /** * 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(); } 

As you can see, flush() writes the entire contents of the buffer in a single pass to the base stream and then cascades the flush. BufferedOutputStream then reimplementes write(byte b[], int off, int len) and void write(int b) (the main methods in the class that each record is delegated to) to write to the buffer, washing it if necessary.

+1
source

When data is cleared, it is like 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 others redefine OutputStream.write () to efficiently manage data blocks.

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 } 
+1
source

The code indicates:

 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 } 

This is a record of all buffered bytes currently. Not byte by bytes.

0
source

The idea is that the BufferedOutputStream user does not need to wait until every byte is actually sent. The user simply can push the larger block onto the output stream and continue even if the connection is very slow. So it’s faster on this side. The output stream itself is trying to be as fast as possible.

0
source

Source: https://habr.com/ru/post/1396478/


All Articles