FileDocCategorySizeDatePackage
text_plain.javaAPI DocJavaMail 1.4.35813Tue Nov 17 10:38:10 GMT 2009com.sun.mail.handlers

text_plain

public class text_plain extends Object implements DataContentHandler
DataContentHandler for text/plain.

Fields Summary
private static ActivationDataFlavor
myDF
Constructors Summary
Methods Summary
private java.lang.StringgetCharset(java.lang.String type)

	try {
	    ContentType ct = new ContentType(type);
	    String charset = ct.getParameter("charset");
	    if (charset == null)
		// If the charset parameter is absent, use US-ASCII.
		charset = "us-ascii";
	    return MimeUtility.javaCharset(charset);
	} catch (Exception ex) {
	    return null;
	}
    
public java.lang.ObjectgetContent(javax.activation.DataSource ds)

	String enc = null;
	InputStreamReader is = null;
	
	try {
	    enc = getCharset(ds.getContentType());
	    is = new InputStreamReader(ds.getInputStream(), enc);
	} catch (IllegalArgumentException iex) {
	    /*
	     * An unknown charset of the form ISO-XXX-XXX will cause
	     * the JDK to throw an IllegalArgumentException.  The
	     * JDK will attempt to create a classname using this string,
	     * but valid classnames must not contain the character '-',
	     * and this results in an IllegalArgumentException, rather than
	     * the expected UnsupportedEncodingException.  Yikes.
	     */
	    throw new UnsupportedEncodingException(enc);
	}

	try {
	    int pos = 0;
	    int count;
	    char buf[] = new char[1024];

	    while ((count = is.read(buf, pos, buf.length - pos)) != -1) {
		pos += count;
		if (pos >= buf.length) {
		    int size = buf.length;
		    if (size < 256*1024)
			size += size;
		    else
			size += 256*1024;
		    char tbuf[] = new char[size];
		    System.arraycopy(buf, 0, tbuf, 0, pos);
		    buf = tbuf;
		}
	    }
	    return new String(buf, 0, pos);
	} finally {
	    try {
		is.close();
	    } catch (IOException ex) { }
	}
    
protected javax.activation.ActivationDataFlavorgetDF()


       
	return myDF;
    
public java.lang.ObjectgetTransferData(java.awt.datatransfer.DataFlavor df, javax.activation.DataSource ds)
Return the Transfer Data of type DataFlavor from InputStream.

param
df The DataFlavor
param
ds The DataSource corresponding to the data
return
String object

	// use myDF.equals to be sure to get ActivationDataFlavor.equals,
	// which properly ignores Content-Type parameters in comparison
	if (getDF().equals(df))
	    return getContent(ds);
	else
	    return null;
    
public java.awt.datatransfer.DataFlavor[]getTransferDataFlavors()
Return the DataFlavors for this DataContentHandler.

return
The DataFlavors

	return new DataFlavor[] { getDF() };
    
public voidwriteTo(java.lang.Object obj, java.lang.String type, java.io.OutputStream os)
Write the object to the output stream, using the specified MIME type.

	if (!(obj instanceof String))
	    throw new IOException("\"" + getDF().getMimeType() +
		"\" DataContentHandler requires String object, " +
		"was given object of type " + obj.getClass().toString());

	String enc = null;
	OutputStreamWriter osw = null;

	try {
	    enc = getCharset(type);
	    osw = new OutputStreamWriter(os, enc);
	} catch (IllegalArgumentException iex) {
	    /*
	     * An unknown charset of the form ISO-XXX-XXX will cause
	     * the JDK to throw an IllegalArgumentException.  The
	     * JDK will attempt to create a classname using this string,
	     * but valid classnames must not contain the character '-',
	     * and this results in an IllegalArgumentException, rather than
	     * the expected UnsupportedEncodingException.  Yikes.
	     */
	    throw new UnsupportedEncodingException(enc);
	}

	String s = (String)obj;
	osw.write(s, 0, s.length());
	osw.flush();