Java输出流缓冲区大小

Java输出流缓冲区大小,java,buffer,httpurlconnection,outputstream,flush,Java,Buffer,Httpurlconnection,Outputstream,Flush,Java中的OutputStream有一个名为flush()的方法。根据其文件: 刷新此输出流并强制写入任何缓冲输出字节 我如何理解这个缓冲区的多字节容量 额外说明:我从一个HttpURLConnectiongetOutputStream()方法中获得了自己的OutputStream。这取决于您使用的OutputStream的类型 让我们从基础开始,通过分析flush of OutputStream作为合同的建议: 公共图书馆 抛出IOException 刷新此输出流并强制删除所有缓冲输出字节

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

刷新此输出流并强制写入任何缓冲输出字节

我如何理解这个缓冲区的多字节容量



额外说明:我从一个
HttpURLConnection
getOutputStream()
方法中获得了自己的
OutputStream

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

让我们从基础开始,通过分析flush of OutputStream作为合同的建议:

公共图书馆 抛出IOException

刷新此输出流并强制删除所有缓冲输出字节 写出来。同花顺的总合同是称其为 指示,如果以前写入的任何字节已被缓冲 输出流的实现,这样的字节应该立即 写到他们预定的目的地

如果此流的预期目标是提供的抽象 通过底层操作系统,例如文件,然后刷新 流只保证以前写入流的字节 传递到操作系统进行写入;这不能保证 它们实际上被写入了物理设备,如磁盘 开车

OutputStream的刷新方法不执行任何操作

如果您看到OutputStream的flush方法,它实际上什么也不做:

public void flush() throws IOException {
}
其思想是,装饰OutputStream的实现必须处理其刷新,然后将其级联到其他OutputStream,直到它到达操作系统(如果是这种情况)

所以它做了些什么!通过实施它的人。具体的类将覆盖flush,以执行诸如将数据移动到磁盘或通过网络发送数据(您的案例)之类的操作

如果您签出一个缓冲输出流的刷新:

/**
 * 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];
}
/**
*存储数据的内部缓冲区。
*/
受保护字节buf[];
/**
*创建新的缓冲输出流以将数据写入
*指定的基础输出流。
*
*@param输出底层输出流。
*/
公共缓冲输出流(输出流输出){
这(共8192人);
}
/**
*创建新的缓冲输出流以将数据写入
*具有指定缓冲区的指定基础输出流
*尺寸。
*
*@param输出底层输出流。
*@param size缓冲区大小。
*@exception IllegalArgumentException如果大小为0。
*/
公共缓冲输出流(输出流输出,整数大小){
超级(出局);

if(size取决于您使用的输出流的类型

让我们从基础开始,通过分析flush of OutputStream作为合同的建议:

公共图书馆 抛出IOException

刷新此输出流并强制删除所有缓冲输出字节 写出来的。同花顺的总合同是这样的,称它为 指示,如果以前写入的任何字节已被缓冲 输出流的实现,这样的字节应该立即 写到他们预定的目的地

如果此流的预期目标是提供的抽象 通过底层操作系统,例如文件,然后刷新 流只保证以前写入流的字节 传递给操作系统进行写入;它不保证 它们实际上被写入了物理设备,如磁盘 开车

OutputStream的刷新方法不执行任何操作

如果您看到OutputStream的flush方法,它实际上什么也不做:

public void flush() throws IOException {
}
其思想是,装饰OutputStream的实现必须处理其刷新,然后将其级联到其他OutputStream,直到它到达操作系统(如果是这种情况)

所以它做了一些事情!通过实现它的人。具体的类将覆盖flush来做一些事情,比如将数据移动到磁盘或通过网络发送数据(您的案例)

如果您签出一个缓冲输出流的刷新:

/**
 * 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 &lt;= 0.
 */
public BufferedOutputStream(OutputStream out, int size) {
    super(out);
    if (size <= 0) {
        throw new IllegalArgumentException("Buffer size <= 0");
    }
    buf = new byte[size];
}
/**
*存储数据的内部缓冲区。
*/
受保护字节buf[];
/**
*创建新的缓冲输出流以将数据写入
*指定的基础输出流。
*
*@param输出底层输出流。
*/
公共缓冲输出流(输出流输出){
这(共8192人);
}
/**
*创建新的缓冲输出流以将数据写入
*具有指定缓冲区的指定基础输出流
*尺寸。
*
*@param输出底层输出流。
*@param size缓冲区大小。
*@exception IllegalArgumentException如果大小为0。
*/
公共缓冲输出流(输出流输出,整数大小){
超级(出局);

如果(大小)什么是“多字节容量”?什么是“多字节容量”?感谢您的详细描述。我将检查
HttpURLConnection
中使用的
OutputStream
的实现情况,如果发现有用的内容,我将在此处发布结果。感谢您的详细描述。我将检查
OutputStream
中使用的
OutputStream
的实现情况
HttpURLConnection
,如果我发现有用的东西,将在这里发布结果。