How to change internal buffer size of DataInputStream

Posted by Gaks on Stack Overflow See other posts from Stack Overflow or by Gaks
Published on 2010-04-01T11:50:30Z Indexed on 2010/04/01 11:53 UTC
Read the original article Hit count: 242

I'm using this kind of code for my TCP/IP connection:

sock = new Socket(host, port);
sock.setKeepAlive(true);
din = new DataInputStream(sock.getInputStream());
dout = new DataOutputStream(sock.getOutputStream());

Then, in separate thread I'm checking din.available() bytes to see if there are some incoming packets to read.

The problem is, that if a packet bigger than 2048 bytes arrives, the din.available() returns 2048 anyway. Just like there was a 2048 internal buffer. I can't read those 2048 bytes when I know it's not the full packet my application is waiting for. If I don't read it however - it'll all stuck at 2048 bytes and never receive more.

Can I enlarge the buffer size of DataInputStream somehow? Socket receive buffer is 16384 as returned by sock.getReceiveBufferSize() so it's not the socket limiting me to 2048 bytes.

If there is no way to increase the DataInputStream buffer size - I guess the only way is to declare my own buffer and read everything from DataInputStream to that buffer?

Regards

© Stack Overflow or respective owner

Related posts about android

Related posts about java