src/share/jaf_classes/javax/activation/URLDataSource.java

changeset 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaf_classes/javax/activation/URLDataSource.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,137 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package javax.activation;
    1.30 +
    1.31 +import java.net.URL;
    1.32 +import java.net.URLConnection;
    1.33 +import java.io.InputStream;
    1.34 +import java.io.OutputStream;
    1.35 +import java.io.IOException;
    1.36 +
    1.37 +/**
    1.38 + * The URLDataSource class provides an object that wraps a <code>URL</code>
    1.39 + * object in a DataSource interface. URLDataSource simplifies the handling
    1.40 + * of data described by URLs within the JavaBeans Activation Framework
    1.41 + * because this class can be used to create new DataHandlers. <i>NOTE: The
    1.42 + * DataHandler object creates a URLDataSource internally,
    1.43 + * when it is constructed with a URL.</i>
    1.44 + *
    1.45 + * @see javax.activation.DataSource
    1.46 + * @see javax.activation.DataHandler
    1.47 + *
    1.48 + * @since 1.6
    1.49 + */
    1.50 +public class URLDataSource implements DataSource {
    1.51 +    private URL url = null;
    1.52 +    private URLConnection url_conn = null;
    1.53 +
    1.54 +    /**
    1.55 +     * URLDataSource constructor. The URLDataSource class will
    1.56 +     * not open a connection to the URL until a method requiring it
    1.57 +     * to do so is called.
    1.58 +     *
    1.59 +     * @param url The URL to be encapsulated in this object.
    1.60 +     */
    1.61 +    public URLDataSource(URL url) {
    1.62 +        this.url = url;
    1.63 +    }
    1.64 +
    1.65 +    /**
    1.66 +     * Returns the value of the URL content-type header field.
    1.67 +     * It calls the URL's <code>URLConnection.getContentType</code> method
    1.68 +     * after retrieving a URLConnection object.
    1.69 +     * <i>Note: this method attempts to call the <code>openConnection</code>
    1.70 +     * method on the URL. If this method fails, or if a content type is not
    1.71 +     * returned from the URLConnection, getContentType returns
    1.72 +     * "application/octet-stream" as the content type.</i>
    1.73 +     *
    1.74 +     * @return the content type.
    1.75 +     */
    1.76 +    public String getContentType() {
    1.77 +        String type = null;
    1.78 +
    1.79 +        try {
    1.80 +            if (url_conn == null)
    1.81 +                url_conn = url.openConnection();
    1.82 +        } catch (IOException e) { }
    1.83 +
    1.84 +        if (url_conn != null)
    1.85 +            type = url_conn.getContentType();
    1.86 +
    1.87 +        if (type == null)
    1.88 +            type = "application/octet-stream";
    1.89 +
    1.90 +        return type;
    1.91 +    }
    1.92 +
    1.93 +    /**
    1.94 +     * Calls the <code>getFile</code> method on the URL used to
    1.95 +     * instantiate the object.
    1.96 +     *
    1.97 +     * @return the result of calling the URL's getFile method.
    1.98 +     */
    1.99 +    public String getName() {
   1.100 +        return url.getFile();
   1.101 +    }
   1.102 +
   1.103 +    /**
   1.104 +     * The getInputStream method from the URL. Calls the
   1.105 +     * <code>openStream</code> method on the URL.
   1.106 +     *
   1.107 +     * @return the InputStream.
   1.108 +     */
   1.109 +    public InputStream getInputStream() throws IOException {
   1.110 +        return url.openStream();
   1.111 +    }
   1.112 +
   1.113 +    /**
   1.114 +     * The getOutputStream method from the URL. First an attempt is
   1.115 +     * made to get the URLConnection object for the URL. If that
   1.116 +     * succeeds, the getOutputStream method on the URLConnection
   1.117 +     * is returned.
   1.118 +     *
   1.119 +     * @return the OutputStream.
   1.120 +     */
   1.121 +    public OutputStream getOutputStream() throws IOException {
   1.122 +        // get the url connection if it is available
   1.123 +        url_conn = url.openConnection();
   1.124 +
   1.125 +        if (url_conn != null) {
   1.126 +            url_conn.setDoOutput(true);
   1.127 +            return url_conn.getOutputStream();
   1.128 +        } else
   1.129 +            return null;
   1.130 +    }
   1.131 +
   1.132 +    /**
   1.133 +     * Return the URL used to create this DataSource.
   1.134 +     *
   1.135 +     * @return The URL.
   1.136 +     */
   1.137 +    public URL getURL() {
   1.138 +        return url;
   1.139 +    }
   1.140 +}

mercurial