src/share/jaxws_classes/com/sun/xml/internal/ws/encoding/StringDataContentHandler.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 1997, 2012, 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.ws.encoding;
    28 import javax.activation.ActivationDataFlavor;
    29 import javax.activation.DataSource;
    30 import javax.activation.DataContentHandler;
    31 import java.awt.datatransfer.DataFlavor;
    32 import java.io.IOException;
    33 import java.io.InputStreamReader;
    34 import java.io.OutputStream;
    35 import java.io.OutputStreamWriter;
    36 import java.io.UnsupportedEncodingException;
    37 import java.nio.charset.Charset;
    39 /**
    40  * JavaMail's data content handler for text/plain -->String
    41  */
    42 public class StringDataContentHandler implements DataContentHandler {
    43     private static final ActivationDataFlavor myDF = new ActivationDataFlavor(
    44             java.lang.String.class, "text/plain", "Text String");
    46     protected ActivationDataFlavor getDF() {
    47         return myDF;
    48     }
    50     /**
    51      * Return the DataFlavors for this <code>DataContentHandler</code>.
    52      *
    53      * @return The DataFlavors
    54      */
    55     public DataFlavor[] getTransferDataFlavors() {
    56         return new DataFlavor[]{getDF()};
    57     }
    59     /**
    60      * Return the Transfer Data of type DataFlavor from InputStream.
    61      *
    62      * @param df The DataFlavor
    63      * @param ds The DataSource corresponding to the data
    64      * @return String object
    65      */
    66     public Object getTransferData(DataFlavor df, DataSource ds)
    67             throws IOException {
    68         // use myDF.equals to be sure to get ActivationDataFlavor.equals,
    69         // which properly ignores Content-Type parameters in comparison
    70         if (getDF().equals(df))
    71             return getContent(ds);
    72         else
    73             return null;
    74     }
    76     public Object getContent(DataSource ds) throws IOException {
    77         String enc = null;
    78         InputStreamReader is;
    80         try {
    81             enc = getCharset(ds.getContentType());
    82             is = new InputStreamReader(ds.getInputStream(), enc);
    83         } catch (IllegalArgumentException iex) {
    84             /*
    85             * An unknown charset of the form ISO-XXX-XXX will cause
    86             * the JDK to throw an IllegalArgumentException.  The
    87             * JDK will attempt to create a classname using this string,
    88             * but valid classnames must not contain the character '-',
    89             * and this results in an IllegalArgumentException, rather than
    90             * the expected UnsupportedEncodingException.  Yikes.
    91             */
    92             throw new UnsupportedEncodingException(enc);
    93         }
    95         try {
    96             int pos = 0;
    97             int count;
    98             char buf[] = new char[1024];
   100             while ((count = is.read(buf, pos, buf.length - pos)) != -1) {
   101                 pos += count;
   102                 if (pos >= buf.length) {
   103                     int size = buf.length;
   104                     if (size < 256 * 1024)
   105                         size += size;
   106                     else
   107                         size += 256 * 1024;
   108                     char tbuf[] = new char[size];
   109                     System.arraycopy(buf, 0, tbuf, 0, pos);
   110                     buf = tbuf;
   111                 }
   112             }
   113             return new String(buf, 0, pos);
   114         } finally {
   115             try {
   116                 is.close();
   117             } catch (IOException ex) {
   118                 // not much can be done
   119             }
   120         }
   121     }
   123     /**
   124      * Write the object to the output stream, using the specified MIME type.
   125      */
   126     public void writeTo(Object obj, String type, OutputStream os)
   127             throws IOException {
   128         if (!(obj instanceof String))
   129             throw new IOException("\"" + getDF().getMimeType() +
   130                     "\" DataContentHandler requires String object, " +
   131                     "was given object of type " + obj.getClass().toString());
   133         String enc = null;
   134         OutputStreamWriter osw;
   136         try {
   137             enc = getCharset(type);
   138             osw = new OutputStreamWriter(os, enc);
   139         } catch (IllegalArgumentException iex) {
   140             /*
   141             * An unknown charset of the form ISO-XXX-XXX will cause
   142             * the JDK to throw an IllegalArgumentException.  The
   143             * JDK will attempt to create a classname using this string,
   144             * but valid classnames must not contain the character '-',
   145             * and this results in an IllegalArgumentException, rather than
   146             * the expected UnsupportedEncodingException.  Yikes.
   147             */
   148             throw new UnsupportedEncodingException(enc);
   149         }
   151         String s = (String) obj;
   152         osw.write(s, 0, s.length());
   153         osw.flush();
   154     }
   156     private String getCharset(String type) {
   157         try {
   158             ContentType ct = new ContentType(type);
   159             String charset = ct.getParameter("charset");
   160             if (charset == null)
   161                 // If the charset parameter is absent, use US-ASCII.
   162                 charset = "us-ascii";
   164             return Charset.forName(charset).name();
   165             //return MimeUtility.javaCharset(charset);
   166         } catch (Exception ex) {
   167             return null;
   168         }
   169     }
   172 }

mercurial