1 21 22 27 28 package com.sun.mail.handlers; 29 30 import java.io.*; 31 import java.awt.*; 32 import java.awt.datatransfer.DataFlavor ; 33 import javax.activation.*; 34 import javax.mail.internet.*; 35 36 39 public class image_gif implements DataContentHandler { 40 private static ActivationDataFlavor myDF = new ActivationDataFlavor( 41 java.awt.Image .class, 42 "image/gif", 43 "GIF Image"); 44 45 protected ActivationDataFlavor getDF() { 46 return myDF; 47 } 48 49 54 public DataFlavor [] getTransferDataFlavors() { return new DataFlavor [] { getDF() }; 56 } 57 58 65 public Object getTransferData(DataFlavor df, DataSource ds) 66 throws IOException { 67 if (getDF().equals(df)) 70 return getContent(ds); 71 else 72 return null; 73 } 74 75 public Object getContent(DataSource ds) throws IOException { 76 InputStream is = ds.getInputStream(); 77 int pos = 0; 78 int count; 79 byte buf[] = new byte[1024]; 80 81 while ((count = is.read(buf, pos, buf.length - pos)) != -1) { 82 pos += count; 83 if (pos >= buf.length) { 84 int size = buf.length; 85 if (size < 256*1024) 86 size += size; 87 else 88 size += 256*1024; 89 byte tbuf[] = new byte[size]; 90 System.arraycopy(buf, 0, tbuf, 0, pos); 91 buf = tbuf; 92 } 93 } 94 Toolkit tk = Toolkit.getDefaultToolkit(); 95 return tk.createImage(buf, 0, pos); 96 } 97 98 101 public void writeTo(Object obj, String type, OutputStream os) 102 throws IOException { 103 if (!(obj instanceof Image)) 104 throw new IOException("\"" + getDF().getMimeType() + 105 "\" DataContentHandler requires Image object, " + 106 "was given object of type " + obj.getClass().toString()); 107 108 throw new IOException(getDF().getMimeType() + " encoding not supported"); 109 } 110 } 111 | Popular Tags |