简体   繁体   English

Java输入流比输出流快

[英]Java inputstream faster than outputstream

I'm bit confused about Java I/O stream. 我对Java I / O流有些困惑。 I have a case where my Inputstream is very fast (like reading file from disk) but my outputstream is very slow (like writing to http servlet response outputstream). 我有一种情况,我的Inputstream速度非常快(例如从磁盘读取文件),但是我的outputstream却非常慢(例如写入http servlet响应输出流)。

What happens if my file size is very large, eventually would my outputstream (piped to inputstream of the file) would throw any memory related exception and close the stream? 如果我的文件很大,最终我的输出流(通过管道传输到文件的输入流)会抛出任何与内存相关的异常并关闭该流,该怎么办? or my outputstream write method would be blocked till outputstream data is cleared? 否则我的outputstream写入方法将被阻塞,直到清除outputstream数据为止?

Is it even possible for outputstream to be full? 输出流是否可能已满?

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 ();
}

Yes, the OutpuStream will block until the write to the underlying system (filesystem, network socket, etc.) has finished. 是的,OutpuStream将阻塞直到对基础系统(文件系统,网络套接字等)的写入完成。 If the OutpuStream is actually a BufferedOutputStream, then there would be some buffering, but in the end it would still block if the buffer is full. 如果OutpuStream实际上是BufferedOutputStream,则将有一些缓冲,但是最后,如果缓冲区已满,它将仍然阻塞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM