A third party programm allows me to ask data from his ip:port. I ask the stuff via this classic code. Here is the constructor of my connection class:
public TcpConnection(String adress, Integer port) {
this.adress = adress;
this.port = port;
try {
socket = new Socket(adress, port);
System.out.println("Opening connection");
out = new PrintWriter(socket.getOutputStream(), true);
InputStream r = new DataInputStream(socket.getInputStream());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (UnknownHostException e) {
// TODO: handle exception
} catch (IOException e) {
e.printStackTrace();
}
}
Then I read my socket with in.read()
in a while loop.
It allows me to read the socket int by int,but I need a finner granularity. (Some info in the answer are encoded in 2 bytes, some in bit, some in 64 bytes ....). So i need to read 2 byte, then a bit. then another bit, then 1 byte and inspect each bit of this byte.
First I was thinking : "Fine, I convert the int I get in a String Binary representation",
( via Integer.toBinaryString(whatIget)
)
buts it's just stupid and error prone.
I'm sure I'm missing something to address this casual problem. Some idea ?
Edit2 : i remove the "when i read a int, i read 32 bit ( 4 bytes )" , because it's wrong and not the point of the question. Thus, Reader.read() read a int, if this method only read a byte, she's reading a 1/4 int ??!?