Home arrow static arrow Java Programming [Archive] - inputstream coupled with buffered reader
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - inputstream coupled with buffered reader
This topic has 9 replies on 1 page.

Posts:40
Registered: 5/6/02
inputstream coupled with buffered reader  
Aug 2, 2004 12:01 AM



 
I am using buffered reader coupled with inputstream reading from a socket.
i use following code to read the message from the bufferedreader.

char[] buff = new char[msgLength];
is.read(buff, 0, msgLength);

it works in most of the cases , but fails when being used with firewall which divides the message into packets. if i change the code to read it line by line, it solves the issue, but that degardes the performance.
whats the ideal solution? should i just remove the buffered reader and read it from the input stream directly?

any suggestions?

thanks
 

Posts:2,909
Registered: 13.8.2003
Re: inputstream coupled with buffered reader  
Aug 2, 2004 12:17 AM (reply 1 of 9)



 
What do you mean "fails"?
Are you expecting the read to return exactly msgLength amount of characters?
The contract of read() states that it can return less, so code accordingly.
 

Posts:40
Registered: 5/6/02
Re: inputstream coupled with buffered reader  
Aug 2, 2004 12:20 AM (reply 2 of 9)



 
by fails i mean, it returns half the information and because of which the logic fails.
when you say "code accordingly", what do you suggest?
do u mean i should read line by line ?

thanks
 

Posts:40
Registered: 5/6/02
Re: inputstream coupled with buffered reader  
Aug 2, 2004 12:20 AM (reply 3 of 9)



 
by fails i mean, it returns half the information and because of which the logic fails.
when you say "code accordingly", what do you suggest?
do u mean i should read line by line ?

thanks
 

Posts:40
Registered: 5/6/02
Re: inputstream coupled with buffered reader  
Aug 2, 2004 12:20 AM (reply 4 of 9)



 
by fails i mean, it returns half the information and because of which the logic fails.
when you say "code accordingly", what do you suggest?
do u mean i should read line by line ?

thanks
 

Posts:40
Registered: 5/6/02
Re: inputstream coupled with buffered reader  
Aug 2, 2004 12:26 AM (reply 5 of 9)



 
by fails i mean, it returns half the information and because of which the logic fails.
when you say "code accordingly", what do you suggest?
do u mean i should read line by line ?

thanks
 

Posts:2,909
Registered: 13.8.2003
Re: inputstream coupled with buffered reader  
Aug 2, 2004 1:58 AM (reply 6 of 9)



 
I meant that read() is not guaranteed to return exactly the amount of bytes/chars whatever.
You can make a loop that reads until enough has been read and then process it.
 

Posts:40
Registered: 5/6/02
Re: inputstream coupled with buffered reader  
Aug 2, 2004 2:04 AM (reply 7 of 9)



 
the problem here is ,if i wait to receive the EOF character as the API doc says, it takes hell lot of time.
thats why i don't want to wait for it to come.

i did the same thing i.e. created a loop land kept on reading , but the performance goes for a toss coz some how EOF takes a ot of time to come.

thanks
 

Posts:2,909
Registered: 13.8.2003
Re: inputstream coupled with buffered reader  
Aug 2, 2004 2:19 AM (reply 8 of 9)



 
Well, if you're getting the data as small packages it's obivious that it's slower. Are you reading a big amount of data and then processing it?

Explain a bit better or show some code.
 

Posts:40
Registered: 5/6/02
Re: inputstream coupled with buffered reader  
Aug 2, 2004 2:46 AM (reply 9 of 9)



 
i tried using 2 ways, one repeating the AOU readLine in a loop and the other repeating the read operation
using buffer in a loop. here is the code for both

import java.io.*;
import java.net.*;
import java.util.*;
public class testTcp{

public Socket soc;
public ServerSocket Serversoc;

public testTcp(){
createSocket();
}

public static void main(String[] args){
testTcp tcpSoc = new testTcp();
}

public void createSocket(){

try{
Serversoc = new ServerSocket(8075);
while(true){
soc = Serversoc.accept();
BufferedReader is = null;
is = new BufferedReader(new InputStreamReader(soc.getInputStream()));
System.out.println("***********Reading Data**********");
try{

String request="",temp="";
while (request!=null)
{
request = is.readLine();
System.out.println("next line");
temp=temp + request;

}

System.out.println("request is"+temp);

DataOutputStream out = null;
String reply = "test reply";
String updatedMsg = "success";
System.out.println("***********Sending Reply**********");
System.out.println("Reply Data : " + reply);
String msg = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><PropertySet ErrorCode=\""
+ "0" + "\" ErrorMessage=\"" + updatedMsg + "\"></PropertySet>";

out = new DataOutputStream(soc.getOutputStream());
System.out.println(msg);
out.writeBytes(msg);

soc.close();
}catch(Exception e1){}

}
}catch(SocketException e){
e.printStackTrace();
}catch(Exception e2){
e2.printStackTrace();
}

}

}

the other i tried was repeating the read operation using the api read as shown below

do
{
x=is.read(buff, 0, msgLength);
System.out.println("value of x: " + x);
}while(x>0);

 
This topic has 9 replies on 1 page.