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.

[1013 byte] By [dani_angelova] at [2007-11-27 6:15:33]
# 1
Read [url= http://forum.java.sun.com/help.jspa?sec=formatting]this[/url] and try again with [code] .... [/code] tags.
corlettka at 2007-7-12 17:26:19 > top of Java-index,Java Essentials,Java Programming...
# 2

> 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 > top of Java-index,Java Essentials,Java Programming...
# 3
sss1 is only for debug.The question is, that the extra data is here: out.write(buf, 0, bytesIn);
dani_angelova at 2007-7-12 17:26:19 > top of Java-index,Java Essentials,Java Programming...
# 4
I found the problem.This happends when in the header is set "Transfer-Encoding: chunked".So that extra info is from chunk.
dani_angelova at 2007-7-12 17:26:19 > top of Java-index,Java Essentials,Java Programming...