Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/net/ContentHandler.java
38829 views
/*1* Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.net;2627import java.io.IOException;2829/**30* The abstract class {@code ContentHandler} is the superclass31* of all classes that read an {@code Object} from a32* {@code URLConnection}.33* <p>34* An application does not generally call the35* {@code getContent} method in this class directly. Instead, an36* application calls the {@code getContent} method in class37* {@code URL} or in {@code URLConnection}.38* The application's content handler factory (an instance of a class that39* implements the interface {@code ContentHandlerFactory} set40* up by a call to {@code setContentHandler}) is41* called with a {@code String} giving the MIME type of the42* object being received on the socket. The factory returns an43* instance of a subclass of {@code ContentHandler}, and its44* {@code getContent} method is called to create the object.45* <p>46* If no content handler could be found, URLConnection will47* look for a content handler in a user-defineable set of places.48* By default it looks in sun.net.www.content, but users can define a49* vertical-bar delimited set of class prefixes to search through in50* addition by defining the java.content.handler.pkgs property.51* The class name must be of the form:52* <pre>53* {package-prefix}.{major}.{minor}54* e.g.55* YoyoDyne.experimental.text.plain56* </pre>57* If the loading of the content handler class would be performed by58* a classloader that is outside of the delegation chain of the caller,59* the JVM will need the RuntimePermission "getClassLoader".60*61* @author James Gosling62* @see java.net.ContentHandler#getContent(java.net.URLConnection)63* @see java.net.ContentHandlerFactory64* @see java.net.URL#getContent()65* @see java.net.URLConnection66* @see java.net.URLConnection#getContent()67* @see java.net.URLConnection#setContentHandlerFactory(java.net.ContentHandlerFactory)68* @since JDK1.069*/70abstract public class ContentHandler {71/**72* Given a URL connect stream positioned at the beginning of the73* representation of an object, this method reads that stream and74* creates an object from it.75*76* @param urlc a URL connection.77* @return the object read by the {@code ContentHandler}.78* @exception IOException if an I/O error occurs while reading the object.79*/80abstract public Object getContent(URLConnection urlc) throws IOException;8182/**83* Given a URL connect stream positioned at the beginning of the84* representation of an object, this method reads that stream and85* creates an object that matches one of the types specified.86*87* The default implementation of this method should call getContent()88* and screen the return type for a match of the suggested types.89*90* @param urlc a URL connection.91* @param classes an array of types requested92* @return the object read by the {@code ContentHandler} that is93* the first match of the suggested types.94* null if none of the requested are supported.95* @exception IOException if an I/O error occurs while reading the object.96* @since 1.397*/98@SuppressWarnings("rawtypes")99public Object getContent(URLConnection urlc, Class[] classes) throws IOException {100Object obj = getContent(urlc);101102for (int i = 0; i < classes.length; i++) {103if (classes[i].isInstance(obj)) {104return obj;105}106}107return null;108}109110}111112113