patternjavaMinor
Method to read the number of lines in a web page
Viewed 0 times
numberthemethodreadpageweblines
Problem
Needing a way to read the number of lines on a web page for the purpose of a progress bar the following method is created:
Now, of course, it works but is it optimal? Can anything be done to improve the speed?
Java 8 is available, if pertinent.
private static int countPageLines(String url) throws IOException {
URL page = null;
BufferedReader in = null;
int count = 0;
try {
page = new URL(url);
in = new BufferedReader(new InputStreamReader(page.openStream()));
while ((in.readLine()) != null) {
count++;
}
} catch(IOException ioe) {
ioe.printStackTrace();
} finally {
if (in != null) {
in.close();
}
}
return count;
}Now, of course, it works but is it optimal? Can anything be done to improve the speed?
Java 8 is available, if pertinent.
Solution
There are a bunch of things which you should consider revising, including the basic concept itself.
Here your concept is to read each line from the URL, count it, and throw it away. Your code does nothing with the data? What's the point?
Regardless, even if the sole purpose of the code is to count a URL's lines, the code should be revised to take advantage of Java Best practices.
First up, it's a cheap shot to treat the
Your method is listed as throwing an
Next try-with-resources is your friend. Resources treated that way will not need the
Finally, Java 8 does have some fantastic InputStream-to-Stream methods, use them. Specifically, the
Your code could be something like:
As a side note, you have tagged your question with performance. The limiting factor here in terms of performance is your network... I cannot suggest a way to make your code above improve the performance of your network without knowing more about your setup ;-)
Here your concept is to read each line from the URL, count it, and throw it away. Your code does nothing with the data? What's the point?
Regardless, even if the sole purpose of the code is to count a URL's lines, the code should be revised to take advantage of Java Best practices.
First up, it's a cheap shot to treat the
new URL(...) constructor the way you do. Yes, it throws a MalformedURLException, and yes, that is a subclass of an IOException, but they really should not be lumped together.Your method is listed as throwing an
IOException anyway, so why handle the exception inside the method and throw nothing? The calling code will need to handle the IOException anyway. Just throw the exception and be done.... but also throw the MalFormedURLException.Next try-with-resources is your friend. Resources treated that way will not need the
finally block at all.Finally, Java 8 does have some fantastic InputStream-to-Stream methods, use them. Specifically, the
BufferedReader.lines() method:Your code could be something like:
private static int countPageLines(String url) throws IOException {
URL page = new URL(url);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(page.openStream()))) {
long count = reader.lines().count();
return count > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)count;
}
}As a side note, you have tagged your question with performance. The limiting factor here in terms of performance is your network... I cannot suggest a way to make your code above improve the performance of your network without knowing more about your setup ;-)
Code Snippets
private static int countPageLines(String url) throws IOException {
URL page = new URL(url);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(page.openStream()))) {
long count = reader.lines().count();
return count > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)count;
}
}Context
StackExchange Code Review Q#111664, answer score: 2
Revisions (0)
No revisions yet.