InputStream do not read properly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • danibangelov
    New Member
    • May 2007
    • 4

    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.printl n(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.dt d">
    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 that page can to display corect in the browser.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by danibangelov
    [code=java]byte[] buf = new byte[4096];
    int bytesIn = 0;
    while (((bytesIn = in.read(buf)) >= 0))
    {
    String sss1 = new String(buf);
    out.write(buf, 0, bytesIn);
    }[/code]

    So,when I look in sss1 contents,there have extra information like: <snip>
    You're building a string out of the *entire* buffer 'buf'. Think what would happen
    if you have read less than 4096 bytes. You do anticipate for a partially filled
    buffer when you write the bytes to the 'out' stream. Do the same when you
    create the string. (read the API docs for an applicable String constructor).

    kind regards,

    Jos

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Update the line while
      [code=java]
      //while(((bytesIn = in.read(buf)) >= 0)) ==== Wrong
      while (((bytesIn = in.read(buf)) > 0)) //====Right
      [/code]
      Best of luck.

      Kind regards,
      Dmjpro.

      Comment

      • danibangelov
        New Member
        • May 2007
        • 4

        #4
        I found the problem.
        This happends when in the header is set "Transfer-Encoding: chunked".
        So that extra info is from chunk.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by danibangelov
          I found the problem.
          This happends when in the header is set "Transfer-Encoding: chunked".
          So that extra info is from chunk.
          I think you found part of your problem; the other part is that you're still using
          the entire buffer to build String sss1 out of it. The buffer might not have been
          filled completly, so only the first part of it contains valid data.

          kind regards,

          Jos

          Comment

          Working...