uk.org.retep.util.mail.BinaryArrayDataSource Maven / Gradle / Ivy
/*
* Copyright (c) 1998-2010, Peter T Mount
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of the retep.org.uk nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package uk.org.retep.util.mail;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.activation.DataSource;
/**
* A DataSource for sending binary attachments in emails.
*
* @author peter
*/
public class BinaryArrayDataSource implements DataSource
{
private byte[] data;
private ByteArrayOutputStream out;
private ByteArrayInputStream in;
private String type;
private String name;
/** Creates a new instance of BinaryArrayDataSource
* @param aArray byte array containing the attachment.
*/
public BinaryArrayDataSource(byte[] aArray)
{
this(aArray, "application/binary");
}
/** Creates a new instance of BinaryArrayDataSource
* @param aArray byte array containing the attachment.
* @param aContentType The content type
*/
public BinaryArrayDataSource(byte[] aArray,String aContentType)
{
this(aArray,aContentType,"");
}
/** Creates a new instance of BinaryArrayDataSource
* @param aArray byte array containing the attachment.
* @param aContentType The content type
* @param aName name to give the attachment
*/
public BinaryArrayDataSource(byte[] aArray,String aContentType,String aName)
{
type = aContentType;
name = aName;
data = aArray;
}
/** The content type
* @return content type
*/
public String getContentType()
{
return type;
}
/** Returns an InputStream of this attachment
* @throws IOException on error
* @return java.io.InputStream
*/
public InputStream getInputStream() throws IOException
{
if(out!=null)
{
// If we have an open OutputStream, then get it's current content
data = out.toByteArray();
}
in = new ByteArrayInputStream(data);
return in;
}
/** The name (if any) of this attachment
* @return name of attachment
*/
public String getName()
{
return name;
}
/** Returns an OutputStream for this attachment.
* @throws IOException on error
* @return OutputStream
*/
public OutputStream getOutputStream() throws IOException
{
out = new ByteArrayOutputStream();
return out;
}
/** Close any InputStream or OutputStream connected to this attachment
* @throws IOException on error
*/
public void close() throws IOException
{
try
{
if(out!=null)
{
out.close();
}
}
finally
{
out=null;
try
{
if(in!=null)
{
in.close();
}
}
finally
{
in=null;
}
}
}
}
/*
* $Log: BinaryArrayDataSource.java,v $
* Revision 1.3 2007/01/06 16:56:25 peter
* Fixed copyright to account for 2007
*
* Revision 1.2 2006/09/10 22:26:44 peter
* Code cleanup and some changes to new package layout
*
* Revision 1.1 2003/06/26 09:50:17 petermount
* Initial import. Moved from site2 to src modules as its now required by xml/rpc package
*
* Revision 1.1.1.1 2003/06/14 12:11:32 peter
* Initial import
*
* Revision 1.1 2003/04/20 18:15:59 peter
* Initial import
*
*/