Edges
Edges

Reputation: 317

Java inputstream faster than outputstream

I'm bit confused about Java I/O stream. 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).

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?

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

Upvotes: 0

Views: 672

Answers (1)

Lucian
Lucian

Reputation: 301

Yes, the OutpuStream will block until the write to the underlying system (filesystem, network socket, etc.) has finished. 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.

Upvotes: 1

Related Questions