Question

I have connected by TCP to a socket which is constantly sending a large amount of data, which I need to read in. What I have so far is a byte buffer that is reading byte by byte in a while loop. But the test case I am using right now is about 3 MB, which takes a while to read when reading in byte by byte.

Here is my code for this explanation:

ByteBuffer buff = ByteBuffer.allocate(3200000);
while(true)
{
     int b = in.read();
     if(b == -1 || buff.remaining() == 0)
     {
           break;
     }
     buff.put((byte)b);
}

I know that byte buffers are not thread safe and I'm not sure if this could be made faster by possibly reading in multiple bytes at a time and then storing it in the buffer? What would be a way for me to speed this process up?

Was it helpful?

Solution

Use a bulk read instead of a single byte read.

byte[] buf = new byte[3200000];
int pos = 0;
while (pos < buf.length) {
  int n = in.read(buf, pos, buf.length - pos);
  if (n < 0)
    break;
  pos += n;
}
ByteBuffer buff = ByteBuffer.wrap(buf, 0, pos);

Instead of getting an InputStream from the socket, and filling a byte array to be wrapped, you can get the SocketChannel and read() directly to the ByteBuffer.

OTHER TIPS

There are several ways.

  1. Use Channels.newChannel() to get a channel from the input stream and use ReadableByteChannel.read(buffer).

  2. Get the byte[] array from the buffer with buffer.array() and read directly into that with in.read(array). Make sure the BB really does have an array of course. If it's a direct byte buffer it won't, but in that case you shouldn't be doing all this at all, you should be using a SocketChannel, otherwise there is zero benefit.

  3. Read into your own largeish byte array and then use a bulk put into the ByteBuffer, taking care to use the length returned by the read() method.

  4. Don't do it. Make up your mind as to whether you want InputStreams or ByteBuffers and don't mix your programming metaphors.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top