5

私は背後にある理論を理解していBufferedOutputStreamます。バイトは、バッファアレイがいっぱいになるまで書き込まれ、その後、基になるストリームに書き込まれます(フラッシュされます)。これは、OS呼び出しが少ないため、バイトごとに書き込むよりも高速であるという考え方です。

ただし、BufferedOutputStreamクラスとメソッド(BufferedOutputStream.java)の実装を見ると、最終的には、バッファーからのバイトがバイトごとに書き込まれているように見えます。

私はこれが事実だと思います:

BufferedOutputStream.write(byte b []、int off、int len)には、out.write(b、off、len)という行があります。outはOutputStreamのインスタンスですが、BufferedOutputStreamではないため、OutputStream.write(byte []、int、int)を呼び出しています。これは、forループを使用してバイトごとに書き込みます

誰かが実際に何が起こっているのか、そしてそれがどのように速いのかを明確にできますか?

4

4 に答える 4

2

データがフラッシュされるとき、それはブロックとしてです。

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

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       }
于 2012-02-14T19:37:15.720 に答える
1

あなたのリンクから:

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

ご覧のとおり、flush()すべてのバッファ コンテンツを 1 回で基になるストリームに書き込み、フラッシュをカスケードします。次にand (すべての書き込みが委譲されるクラスのコア メソッド) をBufferedOutputStream再実装して、バッファに書き込み、必要に応じてフラッシュします。write(byte b[], int off, int len)void write(int b)

于 2012-02-14T19:35:47.777 に答える
0

コードには次のように記載されています。

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       }

これは、現在バッファリングされているすべてのバイトの書き込みです。バイト単位ではありません。

于 2012-02-14T19:36:07.103 に答える
0

ユーザーBufferedOutputStream、すべてのバイトが実際に送信されるのを待つ必要はありません。ユーザーは、接続自体が遅くても、より大きなブロックを出力ストリームにプッシュして続行できます。したがって、こちら側の方が高速です。出力ストリーム自体は、可能な限り高速にしようとします。

于 2012-02-14T19:36:31.483 に答える