user724861
user724861

Reputation: 1074

advanced socket byte reading for slow connection

i just want to ask a mere noob question probably... if the connection is slow and it's not recommended to read the whole byte at a time... then what would be the best socket reading algorithm that will work?

since i've experienced couple of times when reading a socket, sometimes the byte stream weren't completely transferred and make it an invalid data...

currently i'm reading my socket like this:

byte[] message = new byte[lengthOftheByte];
byte buffer = 0;
int count = 0;
while(count < lengthOftheByte) {
  int read = myInputStream.read();
  buffer = (byte) read;
  message[count] = buffer;
  count++;
}

it read single byte then put it into an array's element it works on single read process but not within a thread... :(

Upvotes: 0

Views: 316

Answers (1)

user207421
user207421

Reputation: 311028

Use either read(byte[]) or a BufferedInputStream. However this technique is recommended for all connections, not just slow connections. If you have a slow connection so you are input-bound, nothing you can do to the code can possibly help.

Upvotes: 1

Related Questions