aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.messaging.saaj.soap; aoqi@0: aoqi@0: import java.awt.datatransfer.DataFlavor; aoqi@0: import java.io.*; aoqi@0: import java.awt.*; aoqi@0: aoqi@0: import javax.activation.*; aoqi@0: aoqi@0: /** aoqi@0: * DataContentHandler for image/gif. aoqi@0: * aoqi@0: * @author Ana Lindstrom-Tamer aoqi@0: */ aoqi@0: public class GifDataContentHandler extends Component implements DataContentHandler { aoqi@0: private static ActivationDataFlavor myDF = aoqi@0: new ActivationDataFlavor( aoqi@0: java.awt.Image.class, aoqi@0: "image/gif", aoqi@0: "GIF Image"); aoqi@0: aoqi@0: protected ActivationDataFlavor getDF() { aoqi@0: return myDF; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the DataFlavors for this DataContentHandler. aoqi@0: * aoqi@0: * @return The DataFlavors aoqi@0: */ aoqi@0: public DataFlavor[] getTransferDataFlavors() { // throws Exception; aoqi@0: return new DataFlavor[] { getDF()}; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the Transfer Data of type DataFlavor from InputStream. aoqi@0: * aoqi@0: * @param df The DataFlavor aoqi@0: * @param ins The InputStream corresponding to the data aoqi@0: * @return String object aoqi@0: */ aoqi@0: public Object getTransferData(DataFlavor df, DataSource ds) aoqi@0: throws IOException { aoqi@0: // use myDF.equals to be sure to get ActivationDataFlavor.equals, aoqi@0: // which properly ignores Content-Type parameters in comparison aoqi@0: if (getDF().equals(df)) aoqi@0: return getContent(ds); aoqi@0: else aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Object getContent(DataSource ds) throws IOException { aoqi@0: InputStream is = ds.getInputStream(); aoqi@0: int pos = 0; aoqi@0: int count; aoqi@0: byte buf[] = new byte[1024]; aoqi@0: aoqi@0: while ((count = is.read(buf, pos, buf.length - pos)) != -1) { aoqi@0: pos += count; aoqi@0: if (pos >= buf.length) { aoqi@0: int size = buf.length; aoqi@0: if (size < 256*1024) aoqi@0: size += size; aoqi@0: else aoqi@0: size += 256*1024; aoqi@0: byte tbuf[] = new byte[size]; aoqi@0: System.arraycopy(buf, 0, tbuf, 0, pos); aoqi@0: buf = tbuf; aoqi@0: } aoqi@0: } aoqi@0: Toolkit tk = Toolkit.getDefaultToolkit(); aoqi@0: return tk.createImage(buf, 0, pos); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Write the object to the output stream, using the specified MIME type. aoqi@0: */ aoqi@0: public void writeTo(Object obj, String type, OutputStream os) aoqi@0: throws IOException { aoqi@0: if (obj != null && !(obj instanceof Image)) aoqi@0: throw new IOException("\"" + getDF().getMimeType() + aoqi@0: "\" DataContentHandler requires Image object, " + aoqi@0: "was given object of type " + obj.getClass().toString()); aoqi@0: aoqi@0: throw new IOException(getDF().getMimeType() + " encoding not supported"); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: }