FileDocCategorySizeDatePackage
ByteArrayDataSource.javaAPI DocJavaMail 1.4.36316Tue Nov 17 10:38:12 GMT 2009javax.mail.util

ByteArrayDataSource

public class ByteArrayDataSource extends Object implements DataSource
A DataSource backed by a byte array. The byte array may be passed in directly, or may be initialized from an InputStream or a String.
since
JavaMail 1.4
author
John Mani
author
Bill Shannon
author
Max Spivak

Fields Summary
private byte[]
data
private int
len
private String
type
private String
name
Constructors Summary
public ByteArrayDataSource(InputStream is, String type)
Create a ByteArrayDataSource with data from the specified InputStream and with the specified MIME type. The InputStream is read completely and the data is stored in a byte array.

param
is the InputStream
param
type the MIME type
exception
IOException errors reading the stream

	DSByteArrayOutputStream os = new DSByteArrayOutputStream();
	byte[] buf = new byte[8192];
	int len;
	while ((len = is.read(buf)) > 0)
	    os.write(buf, 0, len);
	this.data = os.getBuf();
	this.len = os.getCount();

	/*
	 * ByteArrayOutputStream doubles the size of the buffer every time
	 * it needs to expand, which can waste a lot of memory in the worst
	 * case with large buffers.  Check how much is wasted here and if
	 * it's too much, copy the data into a new buffer and allow the
	 * old buffer to be garbage collected.
	 */
	if (this.data.length - this.len > 256*1024) {
	    this.data = os.toByteArray();
	    this.len = this.data.length;	// should be the same
	}
        this.type = type;
    
public ByteArrayDataSource(byte[] data, String type)
Create a ByteArrayDataSource with data from the specified byte array and with the specified MIME type.

param
data the data
param
type the MIME type

        this.data = data;
	this.type = type;
    
public ByteArrayDataSource(String data, String type)
Create a ByteArrayDataSource with data from the specified String and with the specified MIME type. The MIME type should include a charset parameter specifying the charset to be used for the string. If the parameter is not included, the default charset is used.

param
data the String
param
type the MIME type
exception
IOException errors reading the String

	String charset = null;
	try {
	    ContentType ct = new ContentType(type);
	    charset = ct.getParameter("charset");
	} catch (ParseException pex) {
	    // ignore parse error
	}
	if (charset == null)
	    charset = MimeUtility.getDefaultJavaCharset();
	// XXX - could convert to bytes on demand rather than copying here
	this.data = data.getBytes(charset);
	this.type = type;
    
Methods Summary
public java.lang.StringgetContentType()
Get the MIME content type of the data.

return
the MIME type

        return type;
    
public java.io.InputStreamgetInputStream()
Return an InputStream for the data. Note that a new stream is returned each time this method is called.

return
the InputStream
exception
IOException if no data has been set

	if (data == null)
	    throw new IOException("no data");
	if (len < 0)
	    len = data.length;
	return new SharedByteArrayInputStream(data, 0, len);
    
public java.lang.StringgetName()
Get the name of the data. By default, an empty string ("") is returned.

return
the name of this data

        return name;
    
public java.io.OutputStreamgetOutputStream()
Return an OutputStream for the data. Writing the data is not supported; an IOException is always thrown.

exception
IOException always

	throw new IOException("cannot do this");
    
public voidsetName(java.lang.String name)
Set the name of the data.

param
name the name of this data

	this.name = name;