I am using the following code to open an input stream from a website url. I wanted to know if there is any way to find out the size of html source before I open the stream.
try
{
URL u = new URL(url);
InputStreamReader isr = new InputStreamReader(u.openStream());
BufferedReader reader = new BufferedReader(isr);
String str;
while ((str = reader.readLine()) != null) {
webContent += str;
}
reader.close();
}
catch (IOException e)
{
Console.print(e.getMessage());
}
Try this:
URL u = new URL(url);
URLConnection conn = u.openConnection();
int length = conn.getContentLength();
InputStreamReader isr = new InputStreamReader(conn.getInputStream());
// ...
Now length will contain the Content-Length of the requested resource in bytes. Note that this will not work for chunked transfers, in which case you will need to keep reading until you reach the end of the stream to determine its length.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With