Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java inputstream比outputstream快_Java_Stream - Fatal编程技术网

Java inputstream比outputstream快

Java inputstream比outputstream快,java,stream,Java,Stream,我对Java I/O流有点困惑。我的Inputstream非常快(比如从磁盘读取文件),但outputstream非常慢(比如写入http servlet响应outputstream) 如果我的文件大小非常大,最终我的outputstream(通过管道传输到文件的inputstream)是否会抛出任何与内存相关的异常并关闭流?或者我的outputstream写入方法将被阻止,直到outputstream数据被清除 outputstream是否可能已满 public void pipe(Input

我对Java I/O流有点困惑。我的Inputstream非常快(比如从磁盘读取文件),但outputstream非常慢(比如写入http servlet响应outputstream)

如果我的文件大小非常大,最终我的outputstream(通过管道传输到文件的inputstream)是否会抛出任何与内存相关的异常并关闭流?或者我的outputstream写入方法将被阻止,直到outputstream数据被清除

outputstream是否可能已满

public void pipe(InputStream is, OutputStream os) throws IOException {
  int n;
  byte[] buffer = new byte[1024];
  while((n = is.read(buffer)) > -1) {
    os.write(buffer, 0, n);   // would this get blocked if outputstream is full?
  }
 os.close ();
}

是的,OutpuStream将阻塞,直到对底层系统(文件系统、网络套接字等)的写入完成。如果OutpuStream实际上是一个BufferedOutputStream,那么会有一些缓冲,但最后如果缓冲区已满,它仍然会阻塞

首先,毫不奇怪,阅读通常比写作快。第二,请决定你想问什么。问题标题和第一段似乎提出了与第二段和代码不同的问题。