aspose file tools*
The moose likes I/O and Streams and the fly likes how to read binary data from socket Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "how to read binary data from socket" Watch "how to read binary data from socket" New topic
Author

how to read binary data from socket

ram budigi
Greenhorn

Joined: Sep 01, 2006
Posts: 3
Hi,

The lenght coming from socket varies for each request. The data coming from socket is binary data. I am using the following code and it taking more than 45 seconds to read the binary data. How do I read the data fast?
Any help on how to read the binary data fast from socket ?

try {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
outputStream = new DataOutputStream(socket.getOutputStream());
outputStream.write(request);
String line = reader.readLine();
sbResponse = new StringBuffer();
while(line != null) {
sbResponse.append(line).append("\n");
line = reader.readLine();
}
}catch (IOException e) { }
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8873
    
    8

It's a bad idea to use a Reader to read binary data. Readers are used to read encoded text and will trim the data to match the default encoding.
As for the amount of time to move data, that depends on how much you are moving and the speed of your connection. There's good information on improving IO throughput in the Java Platform Performance online book.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
ram budigi
Greenhorn

Joined: Sep 01, 2006
Posts: 3
Tahnks for your quick response. Could you provide some sample code to read the entire data from stream into a String or StringBuffer.

InputStream inStream = new BufferedInputStream(socket.getInputStream());
OutputStream outStream = new BufferedOutputStream(socket.getOutputStream());
outStream.write(request);
outStream.flush();
byte[] array = new byte[100];
inStream.read(array,0,100);
String str = new String(array);
System.out.println("str =="+str);
ram budigi
Greenhorn

Joined: Sep 01, 2006
Posts: 3
This code is taking more than 40 seconds to read response from socket client. If I use streams or readers it is taking same time.

InputStream inStream = new BufferedInputStream(socket.getInputStream());
OutputStream outStream = new BufferedOutputStream(socket.getOutputStream());
outStream.write(request);
outStream.flush();
byte[] array = new byte[100];
while(true) {
int a = inStream.read(array,0,100);
if(a == -1) break;
str = new String(array);
sbResponse.append(str);
array = new byte[100];
}
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You can play with larger and smaller buffers. If the stream is pretty big go for 16k or 64k and see what happens.

Are you sure it's not just taking the other end 40 seconds to send the data?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to read binary data from socket