1

I have the following code to read data from a socket.

byte[] data = new byte[dataLength];

if(dataLength > 2000000)FileManager.writeToLogFile(this.getClass(), "GetIncomingMessages", LogMessageType.DEBUG, "XXXXYYYY => 2 length: " + dataLength);

for(int i = 0 ; i < dataLength ; i++){
    byte[] temp = new byte[1];
    in.read(temp);
    data[i] = temp[0];
}

if(dataLength > 2000000)FileManager.writeToLogFile(this.getClass(), "GetIncomingMessages", LogMessageType.DEBUG, "XXXXYYYY => 3 length: " + dataLength);

This code works fine until I get a pack with the size of around 3MB (3227056 bytes to be exact). When this packet is received, no exception occurs, neither does my program crash, but the code never progresses beyond that point. Basically the last log entry that you see above is not made.

What could be the reason for this ? How could I at least figure out WHAT the error is ? (Error is not caught in any of the try-catch blocks). There are however, other receives that take place beyond this point.

5
  • 1
    Where do you get dataLength from? Another question: is this so inefficient on purpose? Commented Oct 15, 2012 at 8:55
  • dataLength is obtained by reading the the first 4 bytes of the stream. The server that sends this data appends the length of the data to beginning byte[] (length is in int, hence the 4 bytes). And no it isn't.. any feedback on how this could be made more efficient would be highly appreciated. Commented Oct 15, 2012 at 9:01
  • You may want to consider Stream.available to check for how much data is available without blocking. Secondly you may want to use int Stream.read(byte[] buffer) to read a whole chunk of data to a buffer instead of byte-by-byte. This is a comment because it is not an answer to your question. Just some tipps. Commented Oct 15, 2012 at 9:06
  • 1
    See for docu: docs.oracle.com/javase/6/docs/api/java/io/… - just use data as a buffer directly and dump temp ... and check how much data was read from the returnvalue. Commented Oct 15, 2012 at 9:27
  • @Fildor I did what you said.. and here are the results... length: 3227056 bytesRead => 104252 so only 104252 bytes were read. Would you be able to shed any light on possible reasons for this ? Commented Oct 15, 2012 at 10:36

1 Answer 1

1

Try:

if( dataLength ) ...

int readBytes = 0;

while( readBytes < dataLength )
{
   readBytes += in.read( data, readBytes, dataLength-readBytes);
}

if( dataLength ) ...

This should make for speed up.

Note that this is not really a solution to your problem. But I guess the inefficient loop to be a part of it. Hopefully we can edit this answer to come to a solution for you.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. I have tried out what you said in the comment above.. please respond if you have some time. Would really appreciate it.
So you solved your problem with above code? What was the error in the end?
Yes it did. The issue seemingly was that all the data (~3MB) was not already present when reading.. so it only read what was there and then gave up. Once i tried to force it to read the 3 million odd bytes.. a good 90% of the data was null. But your reading pattern worked. So in a nut shell, the issue was that the entire stream was not constructed by the time I wanted to read. Hence, it just read whatever was there and returned saying end of stream. But as mentioned previously, your reading pattern works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.