jaxws/src/java.xml.soap/share/classes/com/sun/xml/internal/messaging/saaj/soap/GifDataContentHandler.java
changeset 28977 d7609b65606b
parent 28976 8c912c147654
parent 28344 722378bc599e
child 28978 8431abc709c0
equal deleted inserted replaced
28976:8c912c147654 28977:d7609b65606b
     1 /*
       
     2  * Copyright (c) 1997, 2013, 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  */
       
    25 
       
    26 package com.sun.xml.internal.messaging.saaj.soap;
       
    27 
       
    28 import java.awt.datatransfer.DataFlavor;
       
    29 import java.io.*;
       
    30 import java.awt.*;
       
    31 
       
    32 import javax.activation.*;
       
    33 
       
    34 /**
       
    35  * DataContentHandler for image/gif.
       
    36  *
       
    37  * @author Ana Lindstrom-Tamer
       
    38  */
       
    39 public class GifDataContentHandler extends Component implements DataContentHandler {
       
    40     private static ActivationDataFlavor myDF =
       
    41         new ActivationDataFlavor(
       
    42             java.awt.Image.class,
       
    43             "image/gif",
       
    44             "GIF Image");
       
    45 
       
    46     protected ActivationDataFlavor getDF() {
       
    47         return myDF;
       
    48     }
       
    49 
       
    50     /**
       
    51      * Return the DataFlavors for this <code>DataContentHandler</code>.
       
    52      *
       
    53      * @return The DataFlavors
       
    54      */
       
    55     public DataFlavor[] getTransferDataFlavors() { // throws Exception;
       
    56         return new DataFlavor[] { getDF()};
       
    57     }
       
    58 
       
    59     /**
       
    60      * Return the Transfer Data of type DataFlavor from InputStream.
       
    61      *
       
    62      * @param df The DataFlavor
       
    63      * @param ins The InputStream 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     }
       
    75 
       
    76     public Object getContent(DataSource ds) throws IOException {
       
    77         InputStream is = ds.getInputStream();
       
    78         int pos = 0;
       
    79         int count;
       
    80         byte buf[] = new byte[1024];
       
    81 
       
    82         while ((count = is.read(buf, pos, buf.length - pos)) != -1) {
       
    83             pos += count;
       
    84             if (pos >= buf.length) {
       
    85                 int size = buf.length;
       
    86                 if (size < 256*1024)
       
    87                     size += size;
       
    88                 else
       
    89                     size += 256*1024;
       
    90                 byte tbuf[] = new byte[size];
       
    91                 System.arraycopy(buf, 0, tbuf, 0, pos);
       
    92                 buf = tbuf;
       
    93             }
       
    94         }
       
    95         Toolkit tk = Toolkit.getDefaultToolkit();
       
    96         return tk.createImage(buf, 0, pos);
       
    97     }
       
    98 
       
    99     /**
       
   100      * Write the object to the output stream, using the specified MIME type.
       
   101      */
       
   102     public void writeTo(Object obj, String type, OutputStream os)
       
   103                         throws IOException {
       
   104         if (obj != null && !(obj instanceof Image))
       
   105             throw new IOException("\"" + getDF().getMimeType() +
       
   106                 "\" DataContentHandler requires Image object, " +
       
   107                 "was given object of type " + obj.getClass().toString());
       
   108 
       
   109         throw new IOException(getDF().getMimeType() + " encoding not supported");
       
   110     }
       
   111 
       
   112 
       
   113 }