File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes  any help  on reading data please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Reply locked New topic

any help on reading data please

tomerict erictomt

Joined: Nov 12, 2003
Posts: 2
I am trying to read data into ByteBuffer using SocketChannel.
The following code working fine but the problem is with ByteBuffer size.
The data varies from request to request .
How do I read data into ByteBuffer with out specifing size .
The problem with size is some records has more than 500 bytes and some are less than 500 bytes.
How do i solve this problem ..
Is there any way to read data dynamically from socket channel ...
Any help on this please ...
Any sample I will aprricate ..
The data I have to read is in the following format ..
XXX ... yyyyy
etc ...

Here is my Code:
public ByteBuffer getResponse(byte[] request) throws Exception {
int lenth = 500;
InetSocketAddress isa = new InetSocketAddress("ip", port);
SocketChannel channel =;
ByteBuffer requestBuffer = ByteBuffer.wrap(request);
ByteBuffer responseByteBuffer = ByteBuffer.allocate(length);
int num = 0;
while (num == 0 ) {
num =;
return responseByteBuffer;

Please can any one tell me what changes do I need to read the data .
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Please don't post duplicates Tom, it clutters the board and generally makes people less likely to help.
Also, I was fine with the Tom Eric name, but tomerict erictomt violates the "obviously fictitious" rule we have in our naming policy. Please change your display name.
I agree. Here's the link:
subject: any help on reading data please
It's not a secret anymore!