import java.io.*;
import java.net.*;
public class ProtocolTester implements URLStreamHandlerFactory {
String theURL;
public static void main (String[] args) {
if (args.length == 1) {
ProtocolTester pt = new ProtocolTester(args[0]);
URL.setURLStreamHandlerFactory(pt);
pt.test();
}
else {
System.err.println("Usage: java ProtocolTester url");
}
}
public ProtocolTester(String s) {
theURL = s;
}
public void test() {
String theLine;
try {
URL u = new URL(theURL);
DataInputStream dis = new DataInputStream(u.openStream());
while ((theLine = dis.readLine()) != null) {
System.out.println(theLine);
}
}
catch (IOException e) {
System.err.println(e);
}
}
public URLStreamHandler createURLStreamHandler(String protocol) {
protocol = protocol.toLowerCase();
try {
Class ph = Class.forName(protocol + "URLStreamHandler");
Object o = ph.newInstance();
return (URLStreamHandler) o;
}
catch (Exception e) {
return null;
}
}
}
|