URLStreamHandler.java [plain text]
package java.net;
public abstract class URLStreamHandler
{
protected abstract URLConnection openConnection(URL u)
throws java.io.IOException;
protected void parseURL(URL u, String spec, int start, int limit)
{
String host = u.getHost();
int port = u.getPort();
String file = u.getFile();
if (spec.regionMatches (start, "//", 0, 2))
{
int hostEnd;
int colon;
start += 2;
int slash = spec.indexOf('/', start);
if (slash >= 0)
hostEnd = slash;
else
hostEnd = limit;
host = spec.substring (start, hostEnd);
if ((colon = host.indexOf(':')) >= 0)
{
try
{
port = Integer.parseInt(host.substring(colon + 1));
}
catch (NumberFormatException e)
{
; }
host = host.substring(0, colon);
}
file = null;
start = hostEnd;
}
else if (host == null)
host = "";
if (start < limit && spec.charAt(start) == '/')
{
file = spec.substring(start, limit);
}
else if (file == null || file.length() <= 0)
{
file = spec.substring(start, limit);
}
else if (start < limit)
{
file = file.substring(0, file.lastIndexOf('/'))
+ '/' + spec.substring(start, limit);
}
u.set(u.getProtocol(), host, port, file, u.getRef());
}
private static String canonicalizeFilename(String file)
{
int index;
while ((index = file.indexOf("/./")) >= 0)
file = file.substring(0, index) + file.substring(index + 2);
while ((index = file.indexOf("/../")) >= 0)
{
int previous = file.lastIndexOf('/', index - 1);
if (previous >= 0)
file = file.substring(0, previous) + file.substring(index + 3);
else
break;
}
return file;
}
public boolean sameFile(URL url1, URL url2)
{
if (url1 == url2)
return true;
if (url1 == null || url2 == null || url1.getPort() != url2.getPort())
return false;
String s1, s2;
s1 = url1.getProtocol();
s2 = url2.getProtocol();
if (s1 != s2 && (s1 == null || ! s1.equals(s2)))
return false;
s1 = url1.getHost();
s2 = url2.getHost();
if (s1 != s2 && (s1 == null || ! s1.equals(s2)))
return false;
s1 = canonicalizeFilename(url1.getFile());
s2 = canonicalizeFilename(url2.getFile());
if (s1 != s2 && (s1 == null || ! s1.equals(s2)))
return false;
return true;
}
protected void setURL(URL u, String protocol, String host, int port,
String file, String ref)
{
u.set(protocol, host, port, file, ref);
}
protected String toExternalForm(URL u)
{
String resStr, host, file, ref;
int port;
resStr = u.getProtocol() + ":";
host = u.getHost();
port = u.getPort();
file = u.getFile();
ref = u.getRef();
if (host == null)
host = "";
if (port >= 0 || ! (host.length() == 0))
resStr = resStr + "//" + host + (port < 0 ? "" : ":" + port);
resStr = resStr + file;
if (ref != null)
resStr = resStr + "#" + ref;
return resStr;
}
}