I'm trying create http proxy server on android device. When i trying read response from HTTP server (example1.com) ( example1.com contains content-length in header) If HTTP server contains content-length, then i'm read bytes from content-length else i'm read all bytes of response
byte[] bytes = IOUtils.toByteArray(inFromServer);
The problem is that, when response contains content-length
the response
reads quickly.
If response not contains content-length
the response read slowly.
this my code
DataInputStream in = new DataInputStream(inFromServer);
//BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = "";
String str = "";
Integer len = 0;
while(true) {
line = in.readLine();
if (line.indexOf("Content-Length") != -1)
{
len = Integer.parseInt( line.split("\\D+")[1] );
//System.out.println("LINEE="+len);
}
out.println(line);
str = str + line + '\n';
if(line.isEmpty()) break;
}
int i = Integer.valueOf(len);
String body= "";
System.out.println("i="+i);
if (i>0) {
byte[] buf = new byte[i];
in.readFully(buf);
out.write(buf);
for (byte b:buf) {
body = body + (char)b;
}
}else{
byte[] bytes = IOUtils.toByteArray(inFromServer);
out.write(bytes);
}
out - outStream to browser