You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by dl...@apache.org on 2001/08/03 21:12:03 UTC
cvs commit: jakarta-turbine/src/adapter/org/apache/turbine/util/mail ByteArrayDataSource.java
dlr 01/08/03 12:12:03
Modified: src/adapter/org/apache/turbine/util/mail
ByteArrayDataSource.java
Log:
Checked in Colin Chamlers' patch (with few tweaks of my own):
> I was trying to accomplish two things here:
>
> 1.
> In the original file data[] was used for holding the data.There was a
> getOuputStream method returning a dummy outputstream (because it probably
> couldn't get linked up to the data[]) whereas it should have returned a
> stream to which you can write (to). Furthermore after parsing the
> InputStream the Ouptutstream used was being converted to a byte[]. By
> replacing data[] with an outputstream these issues were solved/simplified.
>
> 2.
> In order to reuse code (I like writing things only once :-) ) I made a
> private method which can be used whether you access the class via a byte[]
> OR an InputStream.
Revision Changes Path
1.3 +130 -41 jakarta-turbine/src/adapter/org/apache/turbine/util/mail/ByteArrayDataSource.java
Index: ByteArrayDataSource.java
===================================================================
RCS file: /home/cvs/jakarta-turbine/src/adapter/org/apache/turbine/util/mail/ByteArrayDataSource.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -u -r1.2 -r1.3
--- ByteArrayDataSource.java 2001/07/25 01:05:29 1.2
+++ ByteArrayDataSource.java 2001/08/03 19:12:03 1.3
@@ -72,32 +72,62 @@
* - a byte array<br>
* - a String<br>
*
+ * @author <a href="mailto:colin.chalmers@maxware.nl">Colin Chalmers</a>
* @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
* @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
- * @version $Id: ByteArrayDataSource.java,v 1.2 2001/07/25 01:05:29 jvanzyl Exp $
+ * @version $Id: ByteArrayDataSource.java,v 1.3 2001/08/03 19:12:03 dlr Exp $
*/
public class ByteArrayDataSource
implements DataSource
{
- /** Data. */
- private byte[] data;
-
+ /** Stream containg the Data */
+ private ByteArrayOutputStream baos = null;
+
/** Content-type. */
- private String type;
+ private String type = "application/octet-stream";
- private ByteArrayOutputStream baos;
+ /**
+ * Default constructor
+ *
+ */
+ public ByteArrayDataSource()
+ {
+ }
/**
* Create a datasource from a byte array.
*
* @param data A byte[].
* @param type A String.
+ * @exception IOException.
*/
- public ByteArrayDataSource(byte[] data,
- String type)
+ public ByteArrayDataSource(byte[] data, String type)
+ throws IOException
{
- this.data = data;
- this.type = type;
+ ByteArrayInputStream Bis = null;
+
+ try
+ {
+ Bis = new ByteArrayInputStream(data);
+ this.ByteArrayDataSource(Bis, type);
+ }
+ catch (IOException ioex)
+ {
+ throw ioex;
+ }
+ finally
+ {
+ try
+ {
+ if (Bis != null)
+ {
+ Bis.close();
+ }
+ }
+ catch (IOException ignored)
+ {
+ }
+ }
}
/**
@@ -105,52 +135,118 @@
*
* @param is An InputStream.
* @param type A String.
+ * @exception IOException.
*/
- public ByteArrayDataSource(InputStream is,
- String type)
+ public ByteArrayDataSource(InputStream aIs, String type)
+ throws IOException
{
+ this.ByteArrayDataSource(aIs, type);
+ }
+
+ /**
+ * Create a datasource from an input stream.
+ *
+ * @param is An InputStream.
+ * @param type A String.
+ * @exception IOException.
+ */
+ private void ByteArrayDataSource(InputStream aIs, String type)
+ throws IOException
+ {
this.type = type;
+
+ BufferedInputStream Bis = null;
+ BufferedOutputStream osWriter = null;
+
try
{
- int ch;
+ int length = 0;
+ byte[] buffer = new byte[512];
+
+ Bis = new BufferedInputStream( aIs );
+ baos = new ByteArrayOutputStream();
+ osWriter = new BufferedOutputStream( baos );
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- BufferedInputStream isReader = new BufferedInputStream( is );
- BufferedOutputStream osWriter = new BufferedOutputStream( os );
-
- while ((ch = isReader.read()) != -1)
+ //Write the InputData to OutputStream
+ while ((length = Bis.read(buffer)) != -1)
{
- osWriter.write(ch);
- }
- data = os.toByteArray();
+ osWriter.write(buffer, 0 , length);
+ }
+ osWriter.flush();
+ osWriter.close();
+
}
catch (IOException ioex)
{
- // Do something!
+ throw ioex;
}
+ finally
+ {
+ try
+ {
+ if (Bis != null)
+ {
+ Bis.close();
+ }
+ if (baos != null)
+ {
+ baos.close();
+ }
+ if (osWriter != null)
+ {
+ osWriter.close();
+ }
+ }
+ catch (IOException ignored)
+ {
+ }
+ }
}
-
+
/**
* Create a datasource from a String.
*
* @param data A String.
* @param type A String.
+ * @exception IOException.
*/
- public ByteArrayDataSource(String data,
- String type)
+ public ByteArrayDataSource(String data, String type)
+ throws IOException
{
this.type = type;
+
try
{
+ baos = new ByteArrayOutputStream();
+
// Assumption that the string contains only ASCII
// characters! Else just pass in a charset into this
// constructor and use it in getBytes().
- this.data = data.getBytes("iso-8859-1");
+ baos.write(data.getBytes("iso-8859-1"));
+ baos.flush();
+ baos.close();
}
catch (UnsupportedEncodingException uex)
{
// Do something!
}
+ catch (IOException ignored)
+ {
+ // Ignore
+ }
+ finally
+ {
+ try
+ {
+ if (baos != null)
+ {
+ baos.close();
+ }
+ }
+ catch (IOException ignored)
+ {
+ }
+ }
}
/**
@@ -160,14 +256,7 @@
*/
public String getContentType()
{
- if ( type == null )
- {
- return "application/octet-stream";
- }
- else
- {
- return type;
- }
+ return (type == null ? "application/octet-stream" : type)
}
/**
@@ -179,11 +268,11 @@
public InputStream getInputStream()
throws IOException
{
- if (data == null)
+ if (baos == null)
{
throw new IOException("no data");
}
- return new ByteArrayInputStream(data);
+ return new ByteArrayInputStream(baos.toByteArray());
}
/**
@@ -195,15 +284,15 @@
{
return "ByteArrayDataSource";
}
-
+
/**
- * Get the output stream.
+ * Get the OutputStream to write to
*
- * @return An OutputStream.
- * @exception IOException.
+ * @return An OutputStream
+ * @exception IOException
*/
public OutputStream getOutputStream()
- throws IOException
+ throws IOException
{
baos = new ByteArrayOutputStream();
return baos;
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org