1 21 22 27 28 package com.sun.mail.handlers; 29 30 import java.io.*; 31 import java.awt.datatransfer.DataFlavor ; 32 import javax.activation.*; 33 import javax.mail.internet.*; 34 35 40 public class text_plain implements DataContentHandler { 41 private static ActivationDataFlavor myDF = new ActivationDataFlavor( 42 java.lang.String .class, 43 "text/plain", 44 "Text String"); 45 46 protected ActivationDataFlavor getDF() { 47 return myDF; 48 } 49 50 55 public DataFlavor [] getTransferDataFlavors() { 56 return new DataFlavor [] { getDF() }; 57 } 58 59 66 public Object getTransferData(DataFlavor df, DataSource ds) 67 throws IOException { 68 if (getDF().equals(df)) 71 return getContent(ds); 72 else 73 return null; 74 } 75 76 public Object getContent(DataSource ds) throws IOException { 77 String enc = null; 78 InputStreamReader is = null; 79 80 try { 81 enc = getCharset(ds.getContentType()); 82 is = new InputStreamReader(ds.getInputStream(), enc); 83 } catch (IllegalArgumentException iex) { 84 92 throw new UnsupportedEncodingException(enc); 93 } 94 95 int pos = 0; 96 int count; 97 char buf[] = new char[1024]; 98 99 while ((count = is.read(buf, pos, buf.length - pos)) != -1) { 100 pos += count; 101 if (pos >= buf.length) { 102 int size = buf.length; 103 if (size < 256*1024) 104 size += size; 105 else 106 size += 256*1024; 107 char tbuf[] = new char[size]; 108 System.arraycopy(buf, 0, tbuf, 0, pos); 109 buf = tbuf; 110 } 111 } 112 return new String (buf, 0, pos); 113 } 114 115 118 public void writeTo(Object obj, String type, OutputStream os) 119 throws IOException { 120 if (!(obj instanceof String )) 121 throw new IOException("\"" + getDF().getMimeType() + 122 "\" DataContentHandler requires String object, " + 123 "was given object of type " + obj.getClass().toString()); 124 125 String enc = null; 126 OutputStreamWriter osw = null; 127 128 try { 129 enc = getCharset(type); 130 osw = new OutputStreamWriter(os, enc); 131 } catch (IllegalArgumentException iex) { 132 140 throw new UnsupportedEncodingException(enc); 141 } 142 143 String s = (String )obj; 144 osw.write(s, 0, s.length()); 145 osw.flush(); 146 } 147 148 private String getCharset(String type) { 149 try { 150 ContentType ct = new ContentType(type); 151 String charset = ct.getParameter("charset"); 152 if (charset == null) 153 charset = "us-ascii"; 155 return MimeUtility.javaCharset(charset); 156 } catch (Exception ex) { 157 return null; 158 } 159 } 160 } 161 | Popular Tags |