Java OutputStream 缓冲区大小

Java OutputStream buffer size

Java 中的 OutputStream 有一个名为 flush() 的方法。根据其文档:

Flushes this output stream and forces any buffered output bytes to be written out.

如何了解此缓冲区的多字节容量?


额外说明:我有自己的 OutputStream 来自 HttpURLConnection getOutputStream() 方法。

这取决于您使用的 OutputStream 类型。

让我们从基础开始,通过分析 OutputStream 的 flush 提议作为契约:

public void flush() throws IOException

Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.

The flush method of OutputStream does nothing.

而如果你看到OutputStream的flush方法,其实它什么也没做:

public void flush() throws IOException {
}

想法是装饰 OutputStream 的实现必须处理其刷新,然后将其级联到其他 OutputStreams,直到它到达 OS 如果是这种情况。

所以它有所作为!通过实施它的人。具体 类 将覆盖 flush 以执行诸如将数据移动到磁盘或通过网络发送数据(您的情况)之类的操作。

如果检查 BufferedOutputStream 的刷新:

/**
 * 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 the internal buffer */
private void flushBuffer() throws IOException {
    if (count > 0) {
        out.write(buf, 0, count);
        count = 0;
    }
}

您可能会看到它正在将自己缓冲区的内容写入包装的 OutputStream。你可以看到它的缓冲区的默认大小(或者你可以改变它),看到它的构造函数:

/**
 * The internal buffer where data is stored.
 */
protected byte buf[];

/**
 * Creates a new buffered output stream to write data to the
 * specified underlying output stream.
 *
 * @param   out   the underlying output stream.
 */
public BufferedOutputStream(OutputStream out) {
    this(out, 8192);
}

/**
 * Creates a new buffered output stream to write data to the
 * specified underlying output stream with the specified buffer
 * size.
 *
 * @param   out    the underlying output stream.
 * @param   size   the buffer size.
 * @exception IllegalArgumentException if size <= 0.
 */
public BufferedOutputStream(OutputStream out, int size) {
    super(out);
    if (size <= 0) {
        throw new IllegalArgumentException("Buffer size <= 0");
    }
    buf = new byte[size];
}

因此,BufferedInputStream 缓冲区的默认大小为 8192 字节。

既然您已经掌握了要点,请查看 OutputStream 代码,您可以在 HttpURLConnection 中使用它来熟悉自己及其缓冲区(如果有的话)。

在您的 java 旅程中,您最终可能会得到一些将刷新操作委托给 OS 的本机代码。在这种情况下,您可能必须检查 OS 是否正在使用某个缓冲区,以及在处理 IO 时它的大小是多少。我知道这部分答案听起来可能很宽泛,但事实就是如此。您需要知道您正在使用什么才能了解​​其背后的内容。

看看这个问题: What is the purpose of flush() in Java streams?

还有这篇文章: http://www.oracle.com/technetwork/articles/javase/perftuning-137844.html

干杯!