how to decide ByteBuffer size created for SocketChannel.read(ByteBuffer)

Hi,

Is there any way by which we can know the size of data ready to be read at the SocketChannel?

When the "SelectionKey" is discovered to be "isReadable()" I would like to create the ByteBuffer of the required size and use it for the SocketChannel.read(ByteBuffer) method.

Right now I have to statically fix the ByteBuffer size to some safe maximum limit for this "read" method, which somehow is not looking efficient technique to me.

Thanks in advance

Amit

[497 byte] By [amitsax76@yahoo.coma] at [2007-11-27 6:16:52]
# 1

Java doesn't tell you how much data is pending, but allocating the ByteBuffer 'statically' in advance is a better technique anyway. ByteBuffers are potentially expensive to create so you should allocate them just once per channel. The size is generally 8k or above depending on how many clients you expect to service at the same time and the amount of memory you can spare per client and in total.

ejpa at 2007-7-12 17:29:03 > top of Java-index,Java Essentials,Java Programming...