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

M

mike

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
 
K

KC Wong

<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).
 
V

Vincent Cantin

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.
 
P

Paul Lutus

Vincent said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top