View Javadoc

    1   package org.codehaus.xfire.attachments;
    2
    3   import java.awt.Image;
    4   import java.awt.Toolkit;
    5   import java.awt.datatransfer.DataFlavor;
    6   import java.awt.datatransfer.UnsupportedFlavorException;
    7   import java.io.ByteArrayOutputStream;
    8   import java.io.IOException;
    9   import java.io.InputStream;
    10  import java.io.OutputStream;
    11
    12  import javax.activation.DataContentHandler;
    13  import javax.activation.DataSource;
    14
    15  /***
    16   * @author <a href="mailto:dan@envoisolutiosn.com">Dan Diephouse</a>
    17   */
    18  public class ImageDataContentHandler
    19      implements DataContentHandler
    20  {
    21      public Object getContent(DataSource ds)
    22          throws IOException
    23      {
    24          ByteArrayOutputStream bos = new ByteArrayOutputStream();
    25
    26          copy(ds.getInputStream(), bos, 8096);
    27
    28          Image image = Toolkit.getDefaultToolkit().createImage(bos.toByteArray());
    29
    30          return image;
    31      }
    32
    33      public void copy(final InputStream input,
    34                       final OutputStream output,
    35                       final int bufferSize)
    36          throws IOException
    37      {
    38          final byte[] buffer = new byte[bufferSize];
    39
    40          int n = 0;
    41          while (-1 != (n = input.read(buffer)))
    42          {
    43              output.write(buffer, 0, n);
    44          }
    45      }
    46
    47      public Object getTransferData(DataFlavor arg0, DataSource arg1)
    48          throws UnsupportedFlavorException, IOException
    49      {
    50          return null;
    51      }
    52
    53      public DataFlavor[] getTransferDataFlavors()
    54      {
    55          return null;
    56      }
    57
    58      public void writeTo(Object obj, String contentTyp, OutputStream out)
    59          throws IOException
    60      {
    61          Image image = (Image) obj;
    62      }
    63
    64  }