excuse me,why i cannot read the complete HTML file...thank you

Discussion in 'Java' started by mike, Sep 10, 2004.

  1. mike

    mike Guest

    the followings is my code:

    InputStream in = outSocket.getInputStream();
    InputStreamReader insr = new InputStreamReader(in);
    String myencode=insr.getEncoding();
    int available_length=in.available();
    int available_length_char=available_length/2;
    char cbuf[] = new char[available_length_char];
    System.out.println(available_length);
    System.out.println(myencode);
    insr.read(cbuf,0,available_length_char);
    in.close();
    insr.close();

    i want to read the data in the InputStream and save the data in the cbuf array,
    but by my code,i cannot read the complete HTML file,i print out the cbuf
    and cannot see the HTML file end indicator:"</html>"

    any suggestion is welcome thank you
    mike, Sep 10, 2004
    #1
    1. Advertising

  2. mike

    KC Wong Guest

    <snip>

    The cause is that available() call only returns the number of bytes you can
    read before blocking. It does not mean that's the end of the data... it is
    possible that not all the data have arrived when you call read().

    Use a while loop, and read until you get a -1 (which indicates end of
    stream).
    KC Wong, Sep 10, 2004
    #2
    1. Advertising

  3. > The cause is that available() call only returns the number of bytes you
    can
    > read before blocking. It does not mean that's the end of the data... it is
    > possible that not all the data have arrived when you call read().
    >
    > Use a while loop, and read until you get a -1 (which indicates end of
    > stream).


    Precision : available() will never return -1.
    The minimum it usually returns is 0.
    Vincent Cantin, Sep 10, 2004
    #3
  4. mike

    Paul Lutus Guest

    Vincent Cantin wrote:

    >> The cause is that available() call only returns the number of bytes you

    > can
    >> read before blocking. It does not mean that's the end of the data... it
    >> is possible that not all the data have arrived when you call read().
    >>
    >> Use a while loop, and read until you get a -1 (which indicates end of
    >> stream).

    >
    > Precision : available() will never return -1.
    > The minimum it usually returns is 0.


    The prior poster is not suggesting the use of available() at all. He
    suggests actually reading characters until -1 is returned, indicating end
    of file.

    --
    Paul Lutus
    http://www.arachnoid.com
    Paul Lutus, Sep 10, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page