Skip to content

Commit b3a29c1

Browse files
committed
URLConnection upgrade
1 parent 10b12bb commit b3a29c1

5 files changed

Lines changed: 1135 additions & 1079 deletions

File tree

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package java.net;
27+
28+
import java.io.IOException;
29+
30+
/**
31+
* The abstract class {@code ContentHandler} is the superclass
32+
* of all classes that read an {@code Object} from a
33+
* {@code URLConnection}.
34+
* <p>
35+
* An application does not generally call the
36+
* {@code getContent} method in this class directly. Instead, an
37+
* application calls the {@code getContent} method in class
38+
* {@code URL} or in {@code URLConnection}.
39+
* The application's content handler factory (an instance of a class that
40+
* implements the interface {@code ContentHandlerFactory} set
41+
* up by a call to {@code setContentHandler}) is
42+
* called with a {@code String} giving the MIME type of the
43+
* object being received on the socket. The factory returns an
44+
* instance of a subclass of {@code ContentHandler}, and its
45+
* {@code getContent} method is called to create the object.
46+
* <p>
47+
* If no content handler could be found, URLConnection will
48+
* look for a content handler in a user-defineable set of places.
49+
* By default it looks in sun.net.www.content, but users can define a
50+
* vertical-bar delimited set of class prefixes to search through in
51+
* addition by defining the java.content.handler.pkgs property.
52+
* The class name must be of the form:
53+
* <pre>
54+
* {package-prefix}.{major}.{minor}
55+
* e.g.
56+
* YoyoDyne.experimental.text.plain
57+
* </pre>
58+
* If the loading of the content handler class would be performed by
59+
* a classloader that is outside of the delegation chain of the caller,
60+
* the JVM will need the RuntimePermission "getClassLoader".
61+
*
62+
* @author James Gosling
63+
* @see java.net.ContentHandler#getContent(java.net.URLConnection)
64+
* @see java.net.ContentHandlerFactory
65+
* @see java.net.URL#getContent()
66+
* @see java.net.URLConnection
67+
* @see java.net.URLConnection#getContent()
68+
* @see java.net.URLConnection#setContentHandlerFactory(java.net.ContentHandlerFactory)
69+
* @since JDK1.0
70+
*/
71+
abstract public class ContentHandler {
72+
/**
73+
* Given a URL connect stream positioned at the beginning of the
74+
* representation of an object, this method reads that stream and
75+
* creates an object from it.
76+
*
77+
* @param urlc a URL connection.
78+
* @return the object read by the {@code ContentHandler}.
79+
* @exception IOException if an I/O error occurs while reading the object.
80+
*/
81+
abstract public Object getContent(URLConnection urlc) throws IOException;
82+
83+
/**
84+
* Given a URL connect stream positioned at the beginning of the
85+
* representation of an object, this method reads that stream and
86+
* creates an object that matches one of the types specified.
87+
*
88+
* The default implementation of this method should call getContent()
89+
* and screen the return type for a match of the suggested types.
90+
*
91+
* @param urlc a URL connection.
92+
* @param classes an array of types requested
93+
* @return the object read by the {@code ContentHandler} that is
94+
* the first match of the suggested types.
95+
* null if none of the requested are supported.
96+
* @exception IOException if an I/O error occurs while reading the object.
97+
* @since 1.3
98+
*/
99+
public Object getContent(URLConnection urlc, Class[] classes) throws IOException {
100+
Object obj = getContent(urlc);
101+
102+
for (int i = 0; i < classes.length; i++) {
103+
if (classes[i].isInstance(obj)) {
104+
return obj;
105+
}
106+
}
107+
return null;
108+
}
109+
110+
}

sources/net.sf.j2s.java.core/src/java/net/HttpURLConnection.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
* redirected host/URL.
6565
*
6666
* @see java.net.HttpURLConnection#disconnect()
67-
* @since JDK1.1
6867
*/
6968
abstract public class HttpURLConnection extends URLConnection {
7069
/* instance variables */

sources/net.sf.j2s.java.core/src/java/net/URL.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ public Object getContent() throws IOException {
11131113
/**
11141114
* The URLStreamHandler factory.
11151115
*/
1116-
static URLStreamHandlerFactory factory;
1116+
public static URLStreamHandlerFactory 秘factory;
11171117

11181118
/**
11191119
* Sets an application's <code>URLStreamHandlerFactory</code>. This method can
@@ -1142,15 +1142,15 @@ public Object getContent() throws IOException {
11421142
*/
11431143
public static void setURLStreamHandlerFactory(URLStreamHandlerFactory fac) {
11441144
synchronized (streamHandlerLock) {
1145-
if (factory != null) {
1145+
if (秘factory != null) {
11461146
throw new Error("factory already defined");
11471147
}
11481148
SecurityManager security = System.getSecurityManager();
11491149
if (security != null) {
11501150
security.checkSetFactory();
11511151
}
11521152
handlers.clear();
1153-
factory = fac;
1153+
秘factory = fac;
11541154
}
11551155
}
11561156

@@ -1173,20 +1173,19 @@ static URLStreamHandler getURLStreamHandler(String protocol) {
11731173
URLStreamHandler handler = handlers.get(protocol);
11741174
if (handler == null) {
11751175
// Use the factory (if any)
1176-
if (factory == null) {
1176+
if (秘factory == null) {
11771177
// SwingJS -- we always use javajs.util.AjaxURLStreamHandlerFactory
11781178
try {
11791179
URL.setURLStreamHandlerFactory((URLStreamHandlerFactory) Class.forName("javajs.util.AjaxURLStreamHandlerFactory").newInstance());
11801180
} catch (Exception e) {
11811181

11821182
}
11831183
}
1184-
if (factory != null) {
1185-
handler = factory.createURLStreamHandler(protocol);
1184+
if (秘factory != null) {
1185+
handler = 秘factory.createURLStreamHandler(protocol);
11861186
}
11871187

11881188
}
1189-
11901189
return handler;
11911190

11921191
}

0 commit comments

Comments
 (0)