src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/soap/JpegDataContentHandler.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

     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.messaging.saaj.soap;
    28 import java.awt.*;
    29 import java.awt.datatransfer.DataFlavor;
    30 import java.awt.image.BufferedImage;
    31 import java.io.*;
    33 import javax.activation.*;
    35 //import com.sun.image.codec.jpeg.*;
    36 import javax.imageio.ImageIO;
    38 /**
    39  * JAF data handler for Jpeg content
    40  *
    41  * @author Ana Lindstrom-Tamer
    42  */
    44 public class JpegDataContentHandler
    45     extends Component
    46     implements DataContentHandler {
    47     public static final String STR_SRC = "java.awt.Image";
    49     /**
    50      * return the DataFlavors for this <code>DataContentHandler</code>
    51      * @return The DataFlavors.
    52      */
    53     public DataFlavor[] getTransferDataFlavors() { // throws Exception;
    54         DataFlavor flavors[] = new DataFlavor[1];
    56         try {
    57             flavors[0] =
    58                 new ActivationDataFlavor(
    59                     Class.forName(STR_SRC),
    60                     "image/jpeg",
    61                     "JPEG");
    62         } catch (Exception e) {
    63             System.out.println(e);
    64         }
    66         return flavors;
    67     }
    69     /**
    70      * return the Transfer Data of type DataFlavor from InputStream
    71      * @param df The DataFlavor.
    72      * @param ins The InputStream corresponding to the data.
    73      * @return The constructed Object.
    74      */
    75     public Object getTransferData(DataFlavor df, DataSource ds) {
    77         // this is sort of hacky, but will work for the
    78         // sake of testing...
    79         if (df.getMimeType().startsWith("image/jpeg")) {
    80             if (df.getRepresentationClass().getName().equals(STR_SRC)) {
    81                 InputStream inputStream = null;
    82                 BufferedImage jpegLoadImage = null;
    84                 try {
    85                     inputStream = ds.getInputStream();
    86                     jpegLoadImage = ImageIO.read(inputStream);
    88                 } catch (Exception e) {
    89                     System.out.println(e);
    90                 }
    92                 return jpegLoadImage;
    93             }
    94         }
    95         return null;
    96     }
    98     /**
    99      *
   100      */
   101     public Object getContent(DataSource ds) { // throws Exception;
   102         InputStream inputStream = null;
   103         BufferedImage jpegLoadImage = null;
   105         try {
   106             inputStream = ds.getInputStream();
   107             jpegLoadImage = ImageIO.read(inputStream);
   109         } catch (Exception e) {
   110         }
   112         return (Image) jpegLoadImage;
   113     }
   115     /**
   116      * construct an object from a byte stream
   117      * (similar semantically to previous method, we are deciding
   118      *  which one to support)
   119      */
   120     public void writeTo(Object obj, String mimeType, OutputStream os)
   121         throws IOException {
   122         if (!mimeType.equals("image/jpeg"))
   123             throw new IOException(
   124                 "Invalid content type \""
   125                     + mimeType
   126                     + "\" for ImageContentHandler");
   128         if (obj == null) {
   129             throw new IOException("Null object for ImageContentHandler");
   130         }
   132         try {
   133             BufferedImage bufImage = null;
   134             if (obj instanceof BufferedImage) {
   135                 bufImage = (BufferedImage) obj;
   137             } else {
   138                 Image img = (Image) obj;
   139                 MediaTracker tracker = new MediaTracker(this);
   140                 tracker.addImage(img, 0);
   141                 tracker.waitForAll();
   142                 if (tracker.isErrorAny()) {
   143                         throw new IOException("Error while loading image");
   144                 }
   145                 bufImage =
   146                     new BufferedImage(
   147                         img.getWidth(null),
   148                         img.getHeight(null),
   149                         BufferedImage.TYPE_INT_RGB);
   151                 Graphics g = bufImage.createGraphics();
   152                 g.drawImage(img, 0, 0, null);
   153             }
   154             ImageIO.write(bufImage, "jpeg", os);
   156         } catch (Exception ex) {
   157             throw new IOException(
   158                 "Unable to run the JPEG Encoder on a stream "
   159                     + ex.getMessage());
   160         }
   161     }
   162 }

mercurial