Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/java/net/ContentHandler.java
41152 views
1
/*
2
* Copyright (c) 1995, 2020, 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 up by a call to
41
* {@link URLConnection#setContentHandlerFactory(ContentHandlerFactory)
42
* setContentHandlerFactory} is called with a {@code String} giving the
43
* MIME type of the 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 {@linkplain URLConnection#getContent() found},
48
* URLConnection will look for a content handler in a user-definable set of places.
49
* Users can define a vertical-bar delimited set of class prefixes
50
* to search through by defining the <i>{@value java.net.URLConnection#contentPathProp}</i>
51
* property. The class name must be of the form:
52
* <blockquote>
53
* <i>{package-prefix}.{major}.{minor}</i>
54
* <p>
55
* where <i>{major}.{minor}</i> is formed by taking the
56
* content-type string, replacing all slash characters with a
57
* {@code period} ('.'), and all other non-alphanumeric characters
58
* with the underscore character '{@code _}'. The alphanumeric
59
* characters are specifically the 26 uppercase ASCII letters
60
* '{@code A}' through '{@code Z}', the 26 lowercase ASCII
61
* letters '{@code a}' through '{@code z}', and the 10 ASCII
62
* digits '{@code 0}' through '{@code 9}'.
63
* <p>
64
* e.g.
65
* YoyoDyne.experimental.text.plain
66
* </blockquote>
67
* If no user-defined content handler is found, then the system
68
* tries to load a specific <i>content-type</i> handler from one
69
* of the built-in handlers, if one exists.
70
* <p>
71
* If the loading of the content handler class would be performed by
72
* a classloader that is outside of the delegation chain of the caller,
73
* the JVM will need the RuntimePermission "getClassLoader".
74
*
75
* @author James Gosling
76
* @see java.net.ContentHandler#getContent(java.net.URLConnection)
77
* @see java.net.ContentHandlerFactory
78
* @see java.net.URL#getContent()
79
* @see java.net.URLConnection
80
* @see java.net.URLConnection#getContent()
81
* @see java.net.URLConnection#setContentHandlerFactory(java.net.ContentHandlerFactory)
82
* @since 1.0
83
*/
84
public abstract class ContentHandler {
85
86
/**
87
* Constructor for subclasses to call.
88
*/
89
public ContentHandler() {}
90
91
/**
92
* Given a URL connect stream positioned at the beginning of the
93
* representation of an object, this method reads that stream and
94
* creates an object from it.
95
*
96
* @param urlc a URL connection.
97
* @return the object read by the {@code ContentHandler}.
98
* @throws IOException if an I/O error occurs while reading the object.
99
*/
100
public abstract Object getContent(URLConnection urlc) throws IOException;
101
102
/**
103
* Given a URL connect stream positioned at the beginning of the
104
* representation of an object, this method reads that stream and
105
* creates an object that matches one of the types specified.
106
*
107
* The default implementation of this method should call
108
* {@link #getContent(URLConnection)}
109
* and screen the return type for a match of the suggested types.
110
*
111
* @param urlc a URL connection.
112
* @param classes an array of types requested
113
* @return the object read by the {@code ContentHandler} that is
114
* the first match of the suggested types or
115
* {@code null} if none of the requested are supported.
116
* @throws IOException if an I/O error occurs while reading the object.
117
* @since 1.3
118
*/
119
@SuppressWarnings("rawtypes")
120
public Object getContent(URLConnection urlc, Class[] classes) throws IOException {
121
Object obj = getContent(urlc);
122
123
for (Class<?> c : classes) {
124
if (c.isInstance(obj)) {
125
return obj;
126
}
127
}
128
return null;
129
}
130
}
131
132