src/share/jaxws_classes/com/sun/xml/internal/bind/v2/util/DataSourceSource.java

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 1997, 2011, 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  */
    26 package com.sun.xml.internal.bind.v2.util;
    28 import java.io.IOException;
    29 import java.io.InputStream;
    30 import java.io.InputStreamReader;
    31 import java.io.Reader;
    33 import javax.activation.DataHandler;
    34 import javax.activation.DataSource;
    35 import javax.activation.MimeType;
    36 import javax.activation.MimeTypeParseException;
    37 import javax.xml.transform.Source;
    38 import javax.xml.transform.stream.StreamSource;
    40 /**
    41  * {@link Source} implementation backed by {@link DataHandler}.
    42  *
    43  * <p>
    44  * This implementation allows the same {@link Source} to be used
    45  * mutliple times.
    46  *
    47  * <p>
    48  * {@link Source} isn't really pluggable. As a consequence,
    49  * this implementation is clunky --- weak against unexpected
    50  * usage of the class.
    51  *
    52  * @author Kohsuke Kawaguchi
    53  */
    54 public final class DataSourceSource extends StreamSource {
    55     private final DataSource source;
    57     /**
    58      * If null, default to the encoding declaration
    59      */
    60     private final String charset;
    62     // remember the value we returned so that the 2nd invocation
    63     // will return the same object, which is what's expeted out of
    64     // StreamSource
    65     private Reader r;
    66     private InputStream is;
    68     public DataSourceSource(DataHandler dh) throws MimeTypeParseException {
    69         this(dh.getDataSource());
    70     }
    72     public DataSourceSource(DataSource source) throws MimeTypeParseException {
    73         this.source = source;
    75         String ct = source.getContentType();
    76         if(ct==null) {
    77             charset = null;
    78         } else {
    79             MimeType mimeType = new MimeType(ct);
    80             this.charset = mimeType.getParameter("charset");
    81         }
    82     }
    84     @Override
    85     public void setReader(Reader reader) {
    86         throw new UnsupportedOperationException();
    87     }
    89     @Override
    90     public void setInputStream(InputStream inputStream) {
    91         throw new UnsupportedOperationException();
    92     }
    94     @Override
    95     public Reader getReader() {
    96         try {
    97             if(charset==null)   return null;
    98             if(r==null)
    99                 r = new InputStreamReader(source.getInputStream(),charset);
   100             return r;
   101         } catch (IOException e) {
   102             // argh
   103             throw new RuntimeException(e);
   104         }
   105     }
   107     @Override
   108     public InputStream getInputStream() {
   109         try {
   110             if(charset!=null)   return null;
   111             if(is==null)
   112                 is = source.getInputStream();
   113             return is;
   114         } catch (IOException e) {
   115             // argh
   116             throw new RuntimeException(e);
   117         }
   118     }
   120     public DataSource getDataSource() {
   121         return source;
   122     }
   123 }

mercurial