Background: I have an assignment where I'm going to pass information through sockets to a very limited extent. It can be a maximum of 10 bytes per message and I was thinking I'm gonna send just one byte (since one byte is enough to signal 256 different states in the protocol). Now I start to dig around looking for information about this and I run into a lot of questions. Please correct me where my assumptions are wrong and answer my literal questions if you can.
So there is the primitive data type byte (which is basically a number between -128 and 127 inclusive, right?). If I use
byte b = 125;
System.out.println(b);
...I get the correct number printed to the console and if I try to assign values outside the limits, the compiler complains.
Then we have the class Byte which apparently creates a Byte object from a byte data type (or int as it says in the API):
Byte b = new Byte(20);
System.out.println(b);
This also produces the expected result and 20 is printed to the console and if I try to use a higher number than 127, the compiler complains.
1. What is the difference between data type byte and class Byte? Is it mainly because the class offers a lot of methods like class Integer does for type int?
The next snippet produces weird results (to me):
import java.io.*;
public class ByteTest {
public static void main(String[] args) {
DataInputStream in = new DataInputStream(System.in);
try {
byte d;
while((d = in.readByte()) != 0) {
System.out.println(d);
}
}
catch (IOException e) {
e.printStackTrace();
}
System.exit(0);
}
}
2. The input is read and out comes the interpretation of the input as an ASCII character in decimal form (for example, 1 returns 49), followed by two more rows with numbers 13 and 10. why is this?
(It doesn't matter if I declare d as a Byte or byte, the result is the same and I've mixed around with getting the value from Byte b instead and so on but these three lines (or more) are always the result and all I want is the input coming right back at me)
Basically I'm a bit confused by this but in the end, all I want is a reasonable way for these single bytes to be sent and when I send 34, I want the other side to received 34, nothing else.
3. Let's say I refrain from using the class Byte and just want to send a type byte over a stream. All regular streams and readers seem to read nothing less than an int (which I assume means that they will block until they have at least two bytes of input where as I will only send one). Am I forced to use DataInputStream
and DataOutputStream
where I have to wrap the type byte in an object Byte or are there other ways?
All of this has made me doubt whether I can trust that an object Byte really just amounts to a byte of data and nothing more... I'm confused :(
Thanks in advance!
Yes. The Byte wrapper also allows representing a nullable byte, or storing byte values into collections and maps, for example.
You send text to the DataInputStream, and this text is encoded to bytes using your platform default encoding. Suppose it's ASCII, the first character will thus be encoded to a byte, then \r
and \n
are sent, which are also encoded using ASCII. So the 3 bytes you read are the ASCII-encoded values of your char + \r\n
.
The javadoc explains what InputStream.read()
does. It reads one byte, and converts it to an int between 0 and 255, in order to distinguish between the byte -1 and the -1 which means "end of stream". To get a byte from the returned int, check it isn't -1 (which means end of stream), and cast it to a byte: byte b = (byte) readValue;