- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有一个模块负责读取、处理和写入字节到磁盘。字节通过 UDP 传入,在组装各个数据报之后,被处理并写入磁盘的最终字节数组通常在 200 字节到 500,000 字节之间。偶尔也会有字节数组,组装后超过50万字节,但是比较少见。
我目前正在使用 FileOutputStream
的write(byte\[\])
method .我也在尝试将 FileOutputStream
包装在 BufferedOutputStream
中。 ,包括使用 the constructor that accepts a buffer size as a parameter .
似乎使用 BufferedOutputStream
的性能会稍微好一些,但我才刚刚开始尝试不同的缓冲区大小。我只有一组有限的示例数据可供使用(来自示例运行的两个数据集,我可以通过我的应用程序进行管道传输)。考虑到我所知道的关于我正在写入的数据的信息,是否有一个通用的经验法则可以用来尝试计算最佳缓冲区大小以减少磁盘写入并最大限度地提高磁盘写入的性能?
最佳答案
当写入小于缓冲区大小时,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;
}
关于java - 就性能而言,在什么时候用 BufferedOutputStream 包装 FileOutputStream 才有意义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8712957/
大家好,我遇到一些错误,所以我尝试使用 spring boot 和 React js 导入 excel 文件但在我的后端我收到错误 构造函数 BufferedOutputStream(FileOutp
我正在使用 BufferedOutputStream 写入文件。在调试时,我注意到即使操作系统显示文件已获得最终大小,刷新操作也会花费很长时间。 我的理论:BufferedOutputStream告诉
我正在尝试使用 TCP 开发一个简单的 Java 文件传输应用程序。我当前的服务器代码如下: package tcp.ftp; import java.io.BufferedInputStream;
我尝试创建一个 TFTP 服务器,但当它接收到一个文件时,似乎并非所有文件都保存到服务器上(缺少一些字节)。文件创建良好,大部分数据已写入,但由于文件不完整,因此被归类为损坏且无法打开。有谁知道如何解
我有几个问题- 1. 我有两台通过套接字连接的计算机。当程序执行时 outputStream.writeInt(value); outputStream.flush(); 究竟发生了什么?程序是否等到
我正在编写下载 servlet,它读取 html 文件并写入 servletOutputStream,传输的文件末尾的问题是添加一些垃圾数据,对此有何建议, 下面是我为此使用的代码 i
我制作的函数有一点问题。我希望每次我给这个函数一个字符串,它都会将我保存到同一文件中的新行,但实际上现在只保存我给出的最后一个字符串。这就像一次又一次的覆盖,需要一些帮助 public void Wr
我了解BufferedOutputStream 背后的理论。字节被写入缓冲区数组,直到它已满,然后写入(刷新)到底层流 - 这个想法比逐字节写入更快,因为操作系统调用更少。 但是,从 Buffered
我想知道 BufferedOutputStream 的用途,使用它时的性能提升? 最佳答案 这是来自 API of BufferedOutputStream 的行: The class impleme
我正在使用以下 Java 代码将一个字节写入文件 fileoutput.txt: FileOutputStream fos=new FileOutputStream("C:/tests/fileout
在Java中,我试图将服务器制作的每个BufferedOutputStream存储到线程之外的数组中,以便向所有连接的客户端广播一些数据。 // initialisation ArrayList
我有一个文件,其中包含一个写入文件开头的序列化 String 对象,后跟我尝试提取的文件的原始字节。 这是我的代码: FileInputStream fileInputStream = new Fil
这个问题已经有答案了: Java BufferedOutputStream vs OutputStream [duplicate] (1 个回答) 已关闭 4 年前。 在 java 8 中,以下之间有
这个问题已经有答案了: Sockets: BufferedOutputStream or just OutputStream? (1 个回答) 已关闭 8 年前。 BufferedOutputStre
我有多个实现 void convert(List list, OutputStream os) 方法。 给定的资源可以重用,因此方法实现不会释放它们。不过,在某些情况下,我想用 BufferedOu
我想使用 BufferedOutputStream 发送单独的消息。所以我做 bos.write(msg1); bos.flush(); bos.write(msg2); bos.flush()。另一
我试图在两端使用 BufferedOutputStream 和 BufferedInputStream 将文件从“服务器”发送到“客户端”。问题是,虽然我在每次写入(右侧)时刷新()服务器上的 Buf
我正在用 java 读取和写入图像文件。该文件可以是任何类型,所以我在写入图像时使用了缓冲读取器和写入器,但写入不正确。图像散落。 BufferedReader bufferedRea
我对 BufferedOutputStream 有疑问。我想通过套接字连接将 kml 文件从 Android 设备发送到 java 服务器。(连接正常,我已经可以在程序的其他部分与 PrintWrit
我正在尝试使用 Robert Sedgwick 在他的算法 4E 教科书中提供的 BinaryStdOut.java 类。该类的代码可以在他的网站上免费获得,但为了便于引用,我将在此处显示它的相关片段
我是一名优秀的程序员,十分优秀!