1 21 22 27 28 package com.sun.mail.dsn; 29 30 import java.io.*; 31 import java.awt.datatransfer.DataFlavor ; 32 import javax.activation.*; 33 import javax.mail.*; 34 import javax.mail.internet.*; 35 36 41 public class text_rfc822headers implements DataContentHandler { 42 private static ActivationDataFlavor myDF = new ActivationDataFlavor( 43 MessageHeaders.class, 44 "text/rfc822-headers", 45 "RFC822 headers"); 46 private static ActivationDataFlavor myDFs = new ActivationDataFlavor( 47 java.lang.String .class, 48 "text/rfc822-headers", 49 "RFC822 headers"); 50 51 56 public DataFlavor [] getTransferDataFlavors() { 57 return new DataFlavor [] { myDF, myDFs }; 58 } 59 60 67 public Object getTransferData(DataFlavor df, DataSource ds) 68 throws IOException { 69 if (myDF.equals(df)) 72 return getContent(ds); 73 else if (myDFs.equals(df)) 74 return getStringContent(ds); 75 else 76 return null; 77 } 78 79 public Object getContent(DataSource ds) throws IOException { 80 try { 81 return new MessageHeaders(ds.getInputStream()); 82 } catch (MessagingException mex) { 83 throw new IOException("Exception creating MessageHeaders: " + mex); 85 } 86 } 87 88 private Object getStringContent(DataSource ds) throws IOException { 89 String enc = null; 90 InputStreamReader is = null; 91 92 try { 93 enc = getCharset(ds.getContentType()); 94 is = new InputStreamReader(ds.getInputStream(), enc); 95 } catch (IllegalArgumentException iex) { 96 104 throw new UnsupportedEncodingException(enc); 105 } 106 107 int pos = 0; 108 int count; 109 char buf[] = new char[1024]; 110 111 while ((count = is.read(buf, pos, buf.length - pos)) != -1) { 112 pos += count; 113 if (pos >= buf.length) { 114 int size = buf.length; 115 if (size < 256*1024) 116 size += size; 117 else 118 size += 256*1024; 119 char tbuf[] = new char[size]; 120 System.arraycopy(buf, 0, tbuf, 0, pos); 121 buf = tbuf; 122 } 123 } 124 return new String (buf, 0, pos); 125 } 126 127 130 public void writeTo(Object obj, String type, OutputStream os) 131 throws IOException { 132 if (obj instanceof MessageHeaders) { 133 MessageHeaders mh = (MessageHeaders)obj; 134 try { 135 mh.writeTo(os); 136 } catch (MessagingException mex) { 137 Exception ex = mex.getNextException(); 138 if (ex instanceof IOException) 139 throw (IOException)ex; 140 else 141 throw new IOException("Exception writing headers: " + mex); 142 } 143 return; 144 } 145 if (!(obj instanceof String )) 146 throw new IOException("\"" + myDFs.getMimeType() + 147 "\" DataContentHandler requires String object, " + 148 "was given object of type " + obj.getClass().toString()); 149 150 String enc = null; 151 OutputStreamWriter osw = null; 152 153 try { 154 enc = getCharset(type); 155 osw = new OutputStreamWriter(os, enc); 156 } catch (IllegalArgumentException iex) { 157 165 throw new UnsupportedEncodingException(enc); 166 } 167 168 String s = (String )obj; 169 osw.write(s, 0, s.length()); 170 osw.flush(); 171 } 172 173 private String getCharset(String type) { 174 try { 175 ContentType ct = new ContentType(type); 176 String charset = ct.getParameter("charset"); 177 if (charset == null) 178 charset = "us-ascii"; 180 return MimeUtility.javaCharset(charset); 181 } catch (Exception ex) { 182 return null; 183 } 184 } 185 } 186 | Popular Tags |