1 19 20 package gnu.mail.handler; 21 22 import java.awt.datatransfer.DataFlavor ; 23 import java.awt.datatransfer.UnsupportedFlavorException ; 24 import java.io.*; 25 import javax.activation.*; 26 import javax.mail.internet.ContentType ; 27 import javax.mail.internet.MimeUtility ; 28 import javax.mail.internet.ParseException ; 29 30 37 public abstract class Application 38 implements DataContentHandler 39 { 40 41 44 protected DataFlavor flavor; 45 46 51 protected Application(String mimeType, String description) 52 { 53 flavor = new ActivationDataFlavor(byte[].class, mimeType, 54 description); 55 } 56 57 62 public DataFlavor [] getTransferDataFlavors() 63 { 64 DataFlavor [] flavors = new DataFlavor [1]; 65 flavors[0] = flavor; 66 return flavors; 67 } 68 69 77 public Object getTransferData(DataFlavor flavor, DataSource source) 78 throws UnsupportedFlavorException , IOException 79 { 80 if (this.flavor.equals(flavor)) 81 return getContent(source); 82 return null; 83 } 84 85 92 public Object getContent(DataSource source) 93 throws IOException 94 { 95 InputStream in = source.getInputStream(); 96 ByteArrayOutputStream out = new ByteArrayOutputStream(); 97 byte[] buf = new byte[4096]; while (true) 99 { 100 int len = in.read(buf); 101 if (len > -1) 102 out.write(buf, 0, len); 103 else 104 break; 105 } 106 return out.toByteArray(); 107 } 108 109 116 public void writeTo(Object object, String mimeType, OutputStream out) 117 throws IOException 118 { 119 byte[] bytes = null; 121 if (object instanceof byte[]) 122 bytes = (byte[])object; 123 out.write(bytes); 124 out.flush(); 125 } 126 127 } 128 | Popular Tags |