src/share/jaxws_classes/com/sun/xml/internal/ws/encoding/ImageDataContentHandler.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.DataContentHandler;
    29 import javax.activation.ActivationDataFlavor;
    30 import javax.activation.DataSource;
    31 import javax.imageio.ImageIO;
    32 import javax.imageio.ImageWriter;
    33 import javax.imageio.stream.ImageOutputStream;
    34 import java.awt.*;
    35 import java.awt.image.BufferedImage;
    36 import java.awt.datatransfer.DataFlavor;
    37 import java.util.logging.Logger;
    38 import java.util.Iterator;
    39 import java.io.IOException;
    40 import java.io.BufferedInputStream;
    41 import java.io.OutputStream;
    42 import java.util.Arrays;
    44 /**
    45  * @author Jitendra Kotamraju
    46  */
    48 public class ImageDataContentHandler extends Component
    49     implements DataContentHandler {
    51     private static final Logger log = Logger.getLogger(ImageDataContentHandler.class.getName());
    52     private final DataFlavor[] flavor;
    54     public ImageDataContentHandler() {
    55         String[] mimeTypes = ImageIO.getReaderMIMETypes();
    56         flavor = new DataFlavor[mimeTypes.length];
    57         for(int i=0; i < mimeTypes.length; i++) {
    58             flavor[i] = new ActivationDataFlavor(Image.class, mimeTypes[i], "Image");
    59         }
    60     }
    62     /**
    63      * Returns an array of DataFlavor objects indicating the flavors the
    64      * data can be provided in. The array should be ordered according to
    65      * preference for providing the data (from most richly descriptive to
    66      * least descriptive).
    67      *
    68      * @return The DataFlavors.
    69      */
    70     public DataFlavor[] getTransferDataFlavors() {
    71         return Arrays.copyOf(flavor, flavor.length);
    72     }
    74     /**
    75      * Returns an object which represents the data to be transferred.
    76      * The class of the object returned is defined by the representation class
    77      * of the flavor.
    78      *
    79      * @param df The DataFlavor representing the requested type.
    80      * @param ds The DataSource representing the data to be converted.
    81      * @return The constructed Object.
    82      */
    83     public Object getTransferData(DataFlavor df, DataSource ds)
    84         throws IOException {
    85         for (DataFlavor aFlavor : flavor) {
    86             if (aFlavor.equals(df)) {
    87                 return getContent(ds);
    88             }
    89         }
    90         return null;
    91     }
    93     /**
    94      * Return an object representing the data in its most preferred form.
    95      * Generally this will be the form described by the first DataFlavor
    96      * returned by the <code>getTransferDataFlavors</code> method.
    97      *
    98      * @param ds The DataSource representing the data to be converted.
    99      * @return The constructed Object.
   100      */
   101     public Object getContent(DataSource ds) throws IOException {
   102         return ImageIO.read(new BufferedInputStream(ds.getInputStream()));
   103     }
   105     /**
   106      * Convert the object to a byte stream of the specified MIME type
   107      * and write it to the output stream.
   108      *
   109      * @param obj   The object to be converted.
   110      * @param type  The requested MIME type of the resulting byte stream.
   111      * @param os    The output stream into which to write the converted
   112      *          byte stream.
   113      */
   115     public void writeTo(Object obj, String type, OutputStream os)
   116         throws IOException {
   118         try {
   119             BufferedImage bufImage;
   120             if (obj instanceof BufferedImage) {
   121                 bufImage = (BufferedImage)obj;
   122             } else if (obj instanceof Image) {
   123                 bufImage = render((Image)obj);
   124             } else {
   125                 throw new IOException(
   126                     "ImageDataContentHandler requires Image object, "
   127                     + "was given object of type "
   128                     + obj.getClass().toString());
   129             }
   130             ImageWriter writer = null;
   131             Iterator<ImageWriter> i = ImageIO.getImageWritersByMIMEType(type);
   132             if (i.hasNext()) {
   133                 writer = i.next();
   134             }
   135             if (writer != null) {
   136                 ImageOutputStream stream = ImageIO.createImageOutputStream(os);
   137                 writer.setOutput(stream);
   138                 writer.write(bufImage);
   139                 writer.dispose();
   140                 stream.close();
   141             } else {
   142                 throw new IOException("Unsupported mime type:"+ type);
   143             }
   144         } catch (Exception e) {
   145             throw new IOException("Unable to encode the image to a stream "
   146                 + e.getMessage());
   147         }
   148     }
   151     private BufferedImage render(Image img) throws InterruptedException {
   153         MediaTracker tracker = new MediaTracker(this);
   154         tracker.addImage(img, 0);
   155         tracker.waitForAll();
   156         BufferedImage bufImage = new BufferedImage(img.getWidth(null),
   157             img.getHeight(null), BufferedImage.TYPE_INT_RGB);
   158         Graphics g = bufImage.createGraphics();
   159         g.drawImage(img, 0, 0, null);
   160         g.dispose();
   161         return bufImage;
   162     }
   164 }

mercurial