File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » I/O and Streams
Reply locked New topic
Author

any help on reading data please

tomerict erictomt
Greenhorn

Joined: Nov 12, 2003
Posts: 2
Hi,
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
AAA
BBBBBBBBBBBBBBBBBBBBBB
CCCCCCCCCCCCCCCCCCCC
DDDDDDDDDDDDDDDDDDDDDDd
etc ...

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

Please can any one tell me what changes do I need to read the data .
Thanks.
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.
Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: any help on reading data please
 
Similar Threads
timeout not working by the time of reading data from channel (urgent help please ..)
Urgent help please
does a nio socket buffer data internally too?
java.net.BindException: Address already in use
ServerSocketChannel.accept() seems to create a random port to send data to?