I've started working with Java and sockets and I'm having some problems with DataInputStream. I'm receiving a telegram which contains the message length in the first 4 bytes of the message itself, so at the first iteration I just read this memory portion. When I read again the incoming message I've noticed that the first 4 bytes are gone, so I need to subtract those 4 bytes in the method I created to compute the message length itself. The question is: does the buffer of the incoming data lose the bytes I've already read? I can't find anything in the Java doc, but I'm probably missing something due to my inexperience.
This is the method for the data reading:
/**
* It receives data from a socket.
*
* @param socket The communication socket.
* @param lengthArea The area of the header containing the length of the message to be received.
* @return The received data as a string.
*/
static String receiveData(Socket socket, int lengthArea) {
byte[] receivedData = new byte[lengthArea];
try {
DataInputStream dataStream = new DataInputStream(socket.getInputStream());
int bufferReturn = dataStream.read(receivedData, 0, lengthArea);
System.out.println("Read Data: " + bufferReturn);
} catch (IOException e) {
// Let's fill the byte array with '-1' for debug purpose.
Arrays.fill(receivedData, (byte) -1);
System.out.println("IO Exception.");
}
return new String(receivedData);
}
And this one is the method I use to compute the message length:
/**
* It converts the message length from number to string, decreasing the calculated length by the size of the message
* read in the header. The size is defined in 'Constants.java'.
*
* @param length The message size.
* @return The message size as an integer.
*/
static int calcLength(String length) {
int num;
try {
num = Integer.parseInt(length) + 1 - MESSAGE_LENGTH_AREA_FROM_HEADER;
} catch (Exception e) {
num = -1;
}
return num;
}
Constants.java
MESSAGE_LENGTH_AREA_FROM_HEADER = 4;
does the buffer of the incoming data lose the bytes I've already read
Yes, of course it does. TCP presents a byte stream. You consume part of it, it's gone. No different from reading from a file.
You should be using DataInputStream.readInt()
to read the length word if it's in binary, and then DataInputStream.readFully()
to read the data.