KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > erpCommon > businessUtility > ByteArrayDataSource


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

17 package org.openbravo.erpCommon.businessUtility;
18
19 import java.io.*;
20 import javax.activation.*;
21
22 public class ByteArrayDataSource implements DataSource {
23   private byte[] g_data = null;
24   private String JavaDoc g_type = "text/plain";
25   private String JavaDoc g_name = null;
26
27   public ByteArrayDataSource (InputStream is, String JavaDoc type) {
28     g_type = type;
29     try {
30       ByteArrayOutputStream os = new ByteArrayOutputStream ();
31       int ch;
32
33       while ((ch = is.read ()) != -1) {
34         os.write (ch);
35       }
36       g_data = os.toByteArray ();
37     } catch (IOException ioex) {
38       System.err.println("ByteArrayDataSource - " + ioex);
39     }
40   }
41
42   public ByteArrayDataSource (byte[] data, String JavaDoc type) {
43     g_data = data;
44     g_type = type;
45   }
46
47   public ByteArrayDataSource (String JavaDoc asciiData, String JavaDoc type) {
48     try {
49       g_data = asciiData.getBytes ("UTF-8");
50     } catch (UnsupportedEncodingException uex) {
51       System.err.println("ByteArrayDataSource - " + uex);
52     }
53     g_type = type;
54   }
55
56   public InputStream getInputStream () throws IOException {
57     if (g_data == null) throw new IOException ("no data");
58     return new ByteArrayInputStream (g_data);
59   }
60
61   public OutputStream getOutputStream () throws IOException {
62     throw new IOException ("cannot do this");
63   }
64
65   public String JavaDoc getContentType () {
66     return g_type;
67   }
68
69   public ByteArrayDataSource setName(String JavaDoc name) {
70     g_name = name;
71     return this;
72   }
73
74   public String JavaDoc getName () {
75     if (g_name != null) return g_name;
76     return "ByteArrayDataStream " + g_type;
77   }
78 }
79
Popular Tags