how to determine the buffer size beforehand
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   zhebin_cong
Posted On:   Thursday, August 5, 2004 08:40 PM

hello one the difference between old io and new io is that the old io read data BYTE-BY-BYTE,but the new io read data IN CHUNKS. sample code snippet about the nio socket reading as following: ByteBuffer buffer=ByteBuffer.allocate(1024); ....... ....... socketChannel.read(buffer);. the socket read the data in chunk to the buffer,not like the old io that read data by byte-by-byte loop.this machanism can be efficent.but also introduce a new problem,that is:how can i determine the buffer size in the developing time,because i don't know the message length that come from the socket,it is RANDOM,as you can see.i assign a 1024-   More>>

hello


one the difference between old io and new io is that the old io read data BYTE-BY-BYTE,but the new io read data IN CHUNKS.
sample code snippet about the nio socket reading as following:



ByteBuffer buffer=ByteBuffer.allocate(1024);


.......


.......


socketChannel.read(buffer);.



the socket read the data in chunk to the buffer,not like the old io that read data by byte-by-byte loop.this machanism can be efficent.but also introduce a new problem,that is:how can i determine the buffer size in the developing time,because i don't know the message length that come from the socket,it is RANDOM,as you can see.i assign a 1024-byte buffer in the code snippet,but if the message length is 2048 in the run time,my application will throw the overflow exception,who have a good idea to deal with such problem?


thank you    <<Less
About | Sitemap | Contact