Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxws/src/share/jaf_classes/javax/activation/URLDataSource.java
38877 views
/*1* Copyright (c) 1997, 2005, 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 javax.activation;2627import java.net.URL;28import java.net.URLConnection;29import java.io.InputStream;30import java.io.OutputStream;31import java.io.IOException;3233/**34* The URLDataSource class provides an object that wraps a <code>URL</code>35* object in a DataSource interface. URLDataSource simplifies the handling36* of data described by URLs within the JavaBeans Activation Framework37* because this class can be used to create new DataHandlers. <i>NOTE: The38* DataHandler object creates a URLDataSource internally,39* when it is constructed with a URL.</i>40*41* @see javax.activation.DataSource42* @see javax.activation.DataHandler43*44* @since 1.645*/46public class URLDataSource implements DataSource {47private URL url = null;48private URLConnection url_conn = null;4950/**51* URLDataSource constructor. The URLDataSource class will52* not open a connection to the URL until a method requiring it53* to do so is called.54*55* @param url The URL to be encapsulated in this object.56*/57public URLDataSource(URL url) {58this.url = url;59}6061/**62* Returns the value of the URL content-type header field.63* It calls the URL's <code>URLConnection.getContentType</code> method64* after retrieving a URLConnection object.65* <i>Note: this method attempts to call the <code>openConnection</code>66* method on the URL. If this method fails, or if a content type is not67* returned from the URLConnection, getContentType returns68* "application/octet-stream" as the content type.</i>69*70* @return the content type.71*/72public String getContentType() {73String type = null;7475try {76if (url_conn == null)77url_conn = url.openConnection();78} catch (IOException e) { }7980if (url_conn != null)81type = url_conn.getContentType();8283if (type == null)84type = "application/octet-stream";8586return type;87}8889/**90* Calls the <code>getFile</code> method on the URL used to91* instantiate the object.92*93* @return the result of calling the URL's getFile method.94*/95public String getName() {96return url.getFile();97}9899/**100* The getInputStream method from the URL. Calls the101* <code>openStream</code> method on the URL.102*103* @return the InputStream.104*/105public InputStream getInputStream() throws IOException {106return url.openStream();107}108109/**110* The getOutputStream method from the URL. First an attempt is111* made to get the URLConnection object for the URL. If that112* succeeds, the getOutputStream method on the URLConnection113* is returned.114*115* @return the OutputStream.116*/117public OutputStream getOutputStream() throws IOException {118// get the url connection if it is available119url_conn = url.openConnection();120121if (url_conn != null) {122url_conn.setDoOutput(true);123return url_conn.getOutputStream();124} else125return null;126}127128/**129* Return the URL used to create this DataSource.130*131* @return The URL.132*/133public URL getURL() {134return url;135}136}137138139