URLStreamHandlerpublic abstract class URLStreamHandler extends Object The abstract class {@code URLStreamHandler} is the base for all classes which
can handle the communication with a URL object over a particular protocol
type. |
Methods Summary |
---|
protected boolean | equals(java.net.URL url1, java.net.URL url2)Compares two URL objects whether they represent the same URL. Two URLs
are equal if they have the same file, host, port, protocol, query, and
reference components.
if (!sameFile(url1, url2)) {
return false;
}
String s1 = url1.getRef(), s2 = url2.getRef();
if (s1 != s2 && (s1 == null || !s1.equals(s2))) {
return false;
}
s1 = url1.getQuery();
s2 = url2.getQuery();
return s1 == s2 || (s1 != null && s1.equals(s2));
| protected int | getDefaultPort()Returns the default port of the protocol used by the handled URL. The
current implementation returns always {@code -1}.
return -1;
| private static java.lang.String | getHost(java.net.URL url)
String host = url.getHost();
if ("file".equals(url.getProtocol()) //$NON-NLS-1$
&& "".equals(host)) { //$NON-NLS-1$
host = "localhost"; //$NON-NLS-1$
}
return host;
| protected java.net.InetAddress | getHostAddress(java.net.URL url)Returns the host address of the given URL.
try {
String host = url.getHost();
if (host == null || host.length() == 0) {
return null;
}
return InetAddress.getByName(host);
} catch (UnknownHostException e) {
return null;
}
| protected int | hashCode(java.net.URL url)Returns the hashcode value for the given URL object.
return toExternalForm(url).hashCode();
| protected boolean | hostsEqual(java.net.URL url1, java.net.URL url2)Compares two URL objects whether they refer to the same host.
String host1 = getHost(url1), host2 = getHost(url2);
if (host1 == host2 || (host1 != null && host1.equalsIgnoreCase(host2))) {
return true;
}
// Compare host address if the host name is not equal.
InetAddress address1 = getHostAddress(url1);
InetAddress address2 = getHostAddress(url2);
if (address1 != null && address1.equals(address2)) {
return true;
}
return false;
| protected abstract java.net.URLConnection | openConnection(java.net.URL u)Establishes a new connection to the resource specified by the URL {@code
u}. Since different protocols also have unique ways of connecting, it
must be overwritten by the subclass.
| protected java.net.URLConnection | openConnection(java.net.URL u, java.net.Proxy proxy)Establishes a new connection to the resource specified by the URL {@code
u} using the given {@code proxy}. Since different protocols also have
unique ways of connecting, it must be overwritten by the subclass.
throw new UnsupportedOperationException(Msg.getString("K034d")); //$NON-NLS-1$
| protected void | parseURL(java.net.URL u, java.lang.String str, int start, int end)Parses the clear text URL in {@code str} into a URL object. URL strings
generally have the following format:
http://www.company.com/java/file1.java#reference
The string is parsed in HTTP format. If the protocol has a different URL
format this method must be overridden.
// For compatibility, refer to Harmony-2941
if (str.startsWith("//", start) //$NON-NLS-1$
&& str.indexOf('/", start + 2) == -1
&& end <= Integer.MIN_VALUE + 1) {
throw new StringIndexOutOfBoundsException(end - 2 - start);
}
if (end < start) {
if (this != u.strmHandler) {
throw new SecurityException();
}
return;
}
String parseString = ""; //$NON-NLS-1$
if (start < end) {
parseString = str.substring(start, end);
}
end -= start;
int fileIdx = 0;
// Default is to use info from context
String host = u.getHost();
int port = u.getPort();
String ref = u.getRef();
String file = u.getPath();
String query = u.getQuery();
String authority = u.getAuthority();
String userInfo = u.getUserInfo();
int refIdx = parseString.indexOf('#", 0);
if (parseString.startsWith("//")) { //$NON-NLS-1$
int hostIdx = 2, portIdx = -1;
port = -1;
fileIdx = parseString.indexOf('/", hostIdx);
int questionMarkIndex = parseString.indexOf('?", hostIdx);
if ((questionMarkIndex != -1)
&& ((fileIdx == -1) || (fileIdx > questionMarkIndex))) {
fileIdx = questionMarkIndex;
}
if (fileIdx == -1) {
fileIdx = end;
// Use default
file = ""; //$NON-NLS-1$
}
int hostEnd = fileIdx;
if (refIdx != -1 && refIdx < fileIdx) {
hostEnd = refIdx;
}
int userIdx = parseString.lastIndexOf('@", hostEnd);
authority = parseString.substring(hostIdx, hostEnd);
if (userIdx > -1) {
userInfo = parseString.substring(hostIdx, userIdx);
hostIdx = userIdx + 1;
}
portIdx = parseString.indexOf(':", userIdx == -1 ? hostIdx
: userIdx);
int endOfIPv6Addr = parseString.indexOf(']");
// if there are square braces, ie. IPv6 address, use last ':'
if (endOfIPv6Addr != -1) {
try {
if (parseString.length() > endOfIPv6Addr + 1) {
char c = parseString.charAt(endOfIPv6Addr + 1);
if (c == ':") {
portIdx = endOfIPv6Addr + 1;
} else {
portIdx = -1;
}
} else {
portIdx = -1;
}
} catch (Exception e) {
// Ignored
}
}
if (portIdx == -1 || portIdx > fileIdx) {
host = parseString.substring(hostIdx, hostEnd);
} else {
host = parseString.substring(hostIdx, portIdx);
String portString = parseString.substring(portIdx + 1, hostEnd);
if (portString.length() == 0) {
port = -1;
} else {
port = Integer.parseInt(portString);
}
}
}
if (refIdx > -1) {
ref = parseString.substring(refIdx + 1, end);
}
int fileEnd = (refIdx == -1 ? end : refIdx);
int queryIdx = parseString.lastIndexOf('?", fileEnd);
boolean canonicalize = false;
if (queryIdx > -1) {
query = parseString.substring(queryIdx + 1, fileEnd);
if (queryIdx == 0 && file != null) {
if (file.equals("")) { //$NON-NLS-1$
file = "/"; //$NON-NLS-1$
} else if (file.startsWith("/")) { //$NON-NLS-1$
canonicalize = true;
}
int last = file.lastIndexOf('/") + 1;
file = file.substring(0, last);
}
fileEnd = queryIdx;
} else
// Don't inherit query unless only the ref is changed
if (refIdx != 0) {
query = null;
}
if (fileIdx > -1) {
if (fileIdx < end && parseString.charAt(fileIdx) == '/") {
file = parseString.substring(fileIdx, fileEnd);
} else if (fileEnd > fileIdx) {
if (file == null) {
file = ""; //$NON-NLS-1$
} else if (file.equals("")) { //$NON-NLS-1$
file = "/"; //$NON-NLS-1$
} else if (file.startsWith("/")) { //$NON-NLS-1$
canonicalize = true;
}
int last = file.lastIndexOf('/") + 1;
if (last == 0) {
file = parseString.substring(fileIdx, fileEnd);
} else {
file = file.substring(0, last)
+ parseString.substring(fileIdx, fileEnd);
}
}
}
if (file == null) {
file = ""; //$NON-NLS-1$
}
if (host == null) {
host = ""; //$NON-NLS-1$
}
if (canonicalize) {
// modify file if there's any relative referencing
file = URLUtil.canonicalizePath(file);
}
setURL(u, u.getProtocol(), host, port, authority, userInfo, file,
query, ref);
| protected boolean | sameFile(java.net.URL url1, java.net.URL url2)Compares two URL objects whether they refer to the same file. In the
comparison included are the URL components protocol, host, port and file.
String s1 = url1.getProtocol();
String s2 = url2.getProtocol();
if (s1 != s2 && (s1 == null || !s1.equals(s2))) {
return false;
}
s1 = url1.getFile();
s2 = url2.getFile();
if (s1 != s2 && (s1 == null || !s1.equals(s2))) {
return false;
}
if (!hostsEqual(url1, url2)) {
return false;
}
int p1 = url1.getPort();
if (p1 == -1) {
p1 = getDefaultPort();
}
int p2 = url2.getPort();
if (p2 == -1) {
p2 = getDefaultPort();
}
return p1 == p2;
| protected void | setURL(java.net.URL u, java.lang.String protocol, java.lang.String host, int port, java.lang.String file, java.lang.String ref)Sets the fields of the URL {@code u} to the values of the supplied
arguments.
if (this != u.strmHandler) {
throw new SecurityException();
}
u.set(protocol, host, port, file, ref);
| protected void | setURL(java.net.URL u, java.lang.String protocol, java.lang.String host, int port, java.lang.String authority, java.lang.String userInfo, java.lang.String file, java.lang.String query, java.lang.String ref)Sets the fields of the URL {@code u} to the values of the supplied
arguments.
if (this != u.strmHandler) {
throw new SecurityException();
}
u.set(protocol, host, port, authority, userInfo, file, query, ref);
| protected java.lang.String | toExternalForm(java.net.URL url)Returns the clear text representation of a given URL using HTTP format.
StringBuffer answer = new StringBuffer(url.getProtocol().length()
+ url.getFile().length() + 16);
answer.append(url.getProtocol());
answer.append(':");
String authority = url.getAuthority();
if (authority != null && authority.length() > 0) {
answer.append("//"); //$NON-NLS-1$
answer.append(url.getAuthority());
}
String file = url.getFile();
String ref = url.getRef();
// file is never null
answer.append(file);
if (ref != null) {
answer.append('#");
answer.append(ref);
}
return answer.toString();
|
|