KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > util > ByteArrayDataSource


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Smart Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.util;
15
16 import java.io.*;
17 import javax.activation.*;
18
19 /**
20  * A DataSource based on the Java Mail Example.
21  * This class implements a DataSource from:
22  * an InputStream
23  * a byte array
24  * a String
25  * @author John Mani
26  * @author Bill Shannon
27  * @author Max Spivak
28  */

29 public class ByteArrayDataSource
30     implements DataSource
31 {
32     /**
33      * Create a DataSource from an input stream
34      * @param is stream
35      * @param type MIME type e.g. text/html
36      */

37     public ByteArrayDataSource (InputStream is, String JavaDoc type)
38     {
39         m_type = type;
40         try
41         {
42             ByteArrayOutputStream os = new ByteArrayOutputStream ();
43             int ch;
44
45             while ((ch = is.read ()) != -1)
46             {
47                 // XXX - must be made more efficient by
48
// doing buffered reads, rather than one byte reads
49
os.write (ch);
50             }
51             m_data = os.toByteArray ();
52         }
53         catch (IOException ioex)
54         {
55             System.err.println("ByteArrayDataSource - " + ioex);
56         }
57     } // ByteArrayDataSource
58

59     /**
60      * Create a DataSource from a byte array
61      * @param data data
62      * @param type type e.g. text/html
63      */

64     public ByteArrayDataSource (byte[] data, String JavaDoc type)
65     {
66         m_data = data;
67         m_type = type;
68     } // ByteArrayDataSource
69

70     /**
71      * Create a DataSource from a String
72      * @param asciiData data
73      * Assumption that the string contains only ASCII characters!
74      * Otherwise just pass a charset into this constructor and use it in getBytes()
75      * @param type MIME type e.g. text/html
76      */

77     public ByteArrayDataSource (String JavaDoc asciiData, String JavaDoc type)
78     {
79         try
80         {
81             m_data = asciiData.getBytes ("iso-8859-1");
82         }
83         catch (UnsupportedEncodingException uex)
84         {
85             System.err.println("ByteArrayDataSource - " + uex);
86         }
87         m_type = type;
88     } // ByteArrayDataSource
89

90     /** Data **/
91     private byte[] m_data = null;
92     /** Content Type **/
93     private String JavaDoc m_type = "text/plain";
94     /** Name **/
95     private String JavaDoc m_name = null;
96
97     /**
98      * Return an InputStream for the data.
99      * @returns inputstream
100      * @throws IOException
101      */

102     public InputStream getInputStream ()
103         throws IOException
104     {
105         if (m_data == null)
106             throw new IOException ("no data");
107         // a new stream must be returned each time.
108
return new ByteArrayInputStream (m_data);
109     } // getInputStream
110

111     /**
112      * Throws exception
113      * @return null
114      * @throws IOException
115      */

116     public OutputStream getOutputStream ()
117         throws IOException
118     {
119         throw new IOException ("cannot do this");
120     } // getOutputStream
121

122     /**
123      * Get Content Type
124      * @return MIME type e.g. text/html
125      */

126     public String JavaDoc getContentType ()
127     {
128         return m_type;
129     } // getContentType
130

131     /**
132      * Set Name
133      * @param name name
134      * @return this
135      */

136     public ByteArrayDataSource setName(String JavaDoc name)
137     {
138         m_name = name;
139         return this;
140     } // setName
141

142     /**
143      * Return Name or Class Name & Content Type
144      * @return dummy
145      */

146     public String JavaDoc getName ()
147     {
148         if (m_name != null)
149             return m_name;
150         return "ByteArrayDataStream " + m_type;
151     } // getName
152

153 } // ByteArrayDataStream
154
Popular Tags