0

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 ();
}
1
  • 1
    First of all, it is no surprise that reading is usually faster than writing. And secondly, please make up your mind what you want to ask. The question title and the first paragraph seem to ask something different than the second paragraph and the code. Commented Jan 24, 2017 at 16:28

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.