Why Java OutputStream.write() Takes Integer but Writes Bytes

ZZ Coder picture ZZ Coder · Sep 10, 2009 · Viewed 16.4k times · Source

I am writing an OutputStream, just noticed this in the OutputStream interface,

   public abstract void write(int b) throws IOException;

This call write one byte to the stream but why it takes integer as an argument?

Answer

sfish picture sfish · Sep 11, 2009

So you can signal EOF:

"Notice that read() returns an int value. If the input is a stream of bytes, why doesn't read() return a byte value? Using a int as a return type allows read() to use -1 to indicate that it has reached the end of the stream."

http://java.sun.com/docs/books/tutorial/essential/io/bytestreams.html