BufferedOutputStream は、書き込みがバッファー サイズ (8 KB など) より小さい場合に役立ちます。大規模な書き込みの場合、それは役に立ちませんし、さらに悪化させることもありません。すべての書き込みがバッファ サイズよりも大きい場合、またはすべての書き込み後に常に flush() を実行する場合、バッファは使用しません。ただし、書き込みの大部分がバッファ サイズよりも小さく、毎回 flush() を使用しない場合は、使用する価値があります。
バッファー サイズを 32 KB 以上に増やすと、わずかに改善されるか、悪化することがあります。YMMV
BufferedOutputStream.write のコードが役立つ場合があります。
/**
* Writes <code>len</code> bytes from the specified byte array
* starting at offset <code>off</code> to this buffered output stream.
*
* <p> Ordinarily this method stores bytes from the given array into this
* stream's buffer, flushing the buffer to the underlying output stream as
* needed. If the requested length is at least as large as this stream's
* buffer, however, then this method will flush the buffer and write the
* bytes directly to the underlying output stream. Thus redundant
* <code>BufferedOutputStream</code>s will not copy data unnecessarily.
*
* @param b the data.
* @param off the start offset in the data.
* @param len the number of bytes to write.
* @exception IOException if an I/O error occurs.
*/
public synchronized void write(byte b[], int off, int len) throws IOException {
if (len >= buf.length) {
/* If the request length exceeds the size of the output buffer,
flush the output buffer and then write the data directly.
In this way buffered streams will cascade harmlessly. */
flushBuffer();
out.write(b, off, len);
return;
}
if (len > buf.length - count) {
flushBuffer();
}
System.arraycopy(b, off, buf, count, len);
count += len;
}