InputStream do not read properly
Hello,
Can you say me, where I make a mistake.
I am writing a proxy.When I read from InputStream in,
like:
try {
byte[] buf = new byte[4096];
int bytesIn = 0;
while (((bytesIn = in.read(buf)) >= 0))
{
String sss1 = new String(buf);
out.write(buf, 0, bytesIn);
}
} catch (Exception e) {
String errMsg = "Error getting HTTP body: " + e;
debugOut.println(errMsg);
}
So,when I look in sss1 contents,there have extra information like:
3
79
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3a
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
2d
..................
The result is incorect page displaying.
Can you say me, where I make a mistake.
Or how I can remove this extra information, so the can to display corect in the browser.
> Hello,
> Can you say me, where I make a mistake.
> I am writing a proxy.When I read from InputStream
> in,
> like:
>try {
> byte[] buf = new byte[4096];
>int bytesIn = 0;
> while (((bytesIn = in.read(buf)) >= 0))
>
>{
> String sss1 = new String(buf);
>
> ut.write(buf, 0, bytesIn);
>}
>} catch (Exception e) {
> String errMsg =
> "Error getting HTTP body: " + e;
>
> ebugOut.println(errMsg);
>}
> here have extra information like:
>
> 3
>
>
>
>
> 79
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
> Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.d
> td">
> 3a
> <html xmlns="http://www.w3.org/1999/xhtml">
> <head>
> <title>
> 2d
> ..................
>
> The result is incorect page displaying.
> Can you say me, where I make a mistake.
> Or how I can remove this extra information, so the
> can to display corect in the browser.
There are two reasons. You must specify how many bytes to use when you create the string sss1 (since buf might contain less than 4096 bytes of valid data). You should also specify encoding.
Kaj
kajbja at 2007-7-12 17:26:19 >
