since some people have said they will be using this, i feel it is my duty to suggest improvements because now its being used in the 'real world'.
Code:
String thisLine;
URL u = new URL("http://world81.runescape.com/a2,m0,j0,o0");
DataInputStream theHTML = new DataInputStream(u.openStream());
while ((thisLine = theHTML.readLine()) != null) {
HTMLsource = HTMLsource + thisLine;
this is a horrible way of doing it,
java strings are immutable, when you add them like that, you dont modify them but make a whole new string.
with the way you're doing it now, you will use 10x more memory then you should and take much longer (even though speed isnt the biggest issue)
also, HTMLSource should be called htmlSource or something that follows the java naming convention
also, readLine() doesnt return the line break itself, so you've lost one or two characters per line
iv seen some people try to compensate with something like this
Code:
while ((thisLine = theHTML.readLine()) != null) {
HTMLsource = HTMLsource + thisLine + '\n';
this isnt ideal either, because you dont know if readLine() found a "\n" char, or "\r" or even "\r\n"
also, you dont close 'theHTML' stream
i would suggest doing it this way
Code:
URL u = new URL("http://world81.runescape.com/a2,m0,j0,o0");
URLConnection urlcon = u.openConnection();
DataInputStream theHTML = new DataInputStream(urlcon.getInputStream());
byte[] buffer = new byte[urlcon.getContentLength()];
theHTML.readFully(buffer);
theHTML.close();
String htmlSource = new String(buffer);
dont feel bad though, a very similar technique is found in smart too
Code:
private String downloadHTML(String address) {
try {
URL url = new URL(address);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuilder builder = new StringBuilder();
String inputLine;
while((inputLine = in.readLine()) != null)
builder.append(inputLine).append("\n");
in.close();
return builder.toString();
}
catch(Exception e) {
e.printStackTrace();
}
return null;
}
</potshot@ben>
Code:
public final URL getDocumentBase() {
try {
return new URL("http://world81.runescape.com/");
} catch (Exception e) {
return null;
}
}
this is wrong, not what a normal browser returns
you should change it to this
new URL("http://world81.runescape.com/a2,m0,j0,o0");
you'll be pleased to know that your getCodeBase() is correct though
Code:
public final AppletContext getAppletContext() {
return null;
}
you really should write your own implementation of AppletContext, no real browser returns null on this method.
you dont have to actually do anything in those methods, since the applet has no way of knowing if you really opened a web page or whatever.
just make them return sane values