Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 294
ZZ Coder Avatar asked Sep 10 '09 21:09

ZZ Coder


People also ask

Which method is used for writing bytes to an OutputStream in Java?

The write(int b) method of OutputStream class is used to write the specified bytes to the output stream. The bytes to be written are the eight low-order bits of the argument b. The 24 high-order bits of b are ignored. Subclass of OutputStream must provide an implementation for this method.

How does OutputStream write work?

Methods of OutputStreamwrite() - writes the specified byte to the output stream. write(byte[] array) - writes the bytes from the specified array to the output stream. flush() - forces to write all data present in output stream to the destination. close() - closes the output stream.

How does OutputStream work java?

An output stream accepts output bytes and sends them to some sink. Applications that need to define a subclass of OutputStream must always provide at least a method that writes one byte of output. Methods: void close() : Closes this output stream and releases any system resources associated with this stream.

How do you write OutputStream to string?

Example: Convert OutputStream to String This is done using stream's write() method. Then, we simply convert the OutputStream to finalString using String 's constructor which takes byte array. For this, we use stream's toByteArray() method.


2 Answers

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

like image 156
sfish Avatar answered Oct 11 '22 08:10

sfish


Actually I've been working with bytes a bit lately and they can be annoying. They up-convert to ints at the slightest provocation and there is no designation to turn a number into a byte--for instance, 8l will give you a long value 8, but for byte you have to say (byte)8

On top of that, they will (pretty much) always be stored internally as ints unless you are using an array (and maybe even then.. not sure).

I think they just pretty much assume that the only reason to use a byte is i/o where you actually need 8 bits, but internally they expect you to always use ints.

By the way, a byte can perform worse since it always has to be masked...

At least I remember reading that years ago, could have changed by now.

As an example answer for your specific question, if a function (f) took a byte, and you had two bytes (b1 and b2), then:

f(b1 & b2) 

wouldn't work, because b1 & b2 would be up-converted to an int, and the int couldn't be down-converted automatically (loss of precision). So you would have to code:

f( (byte)(b1 & b2) ) 

Which would get irritating.

And don't bother asking WHY b1 & b2 up-converts--I've been cussing at that a bit lately myself!

like image 20
Bill K Avatar answered Oct 11 '22 07:10

Bill K