1:/* 2: * @(#)ByteArrayDataSource.java 1.2 00/05/24 3: * 4: * Copyright 1998-2000 Sun Microsystems, Inc. All Rights Reserved. 5: * 6: * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, 7: * modify and redistribute this software in source and binary code form, 8: * provided that i) this copyright notice and license appear on all copies of 9: * the software; and ii) Licensee does not utilize the software in a manner 10: * which is disparaging to Sun. 11: * 12: * This software is provided "AS IS," without a warranty of any kind. ALL 13: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY 14: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR 15: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE 16: * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING 17: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS 18: * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, 19: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER 20: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF 21: * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE 22: * POSSIBILITY OF SUCH DAMAGES. 23: * 24: * This software is not designed or intended for use in on-line control of 25: * aircraft, air traffic, aircraft navigation or aircraft communications; or in 26: * the design, construction, operation or maintenance of any nuclear 27: * facility. Licensee represents and warrants that it will not use or 28: * redistribute the Software for such purposes. 29: */ 30: 31:import java.io.*; 32:import javax.activation.*; 33: 34:/** 35: * A simple DataSource for demonstration purposes. 36: * This class implements a DataSource from: 37: * an InputStream 38: * a byte array 39: * a String 40: * 41: * @author John Mani 42: * @author Bill Shannon 43: * @author Max Spivak 44: */ 45:public class ByteArrayDataSource implements DataSource { 46: private byte[] data; // data 47: private String type; // content-type 48: 49: /* Create a DataSource from an input stream */ 50: public ByteArrayDataSource(InputStream is, String type) { 51: this.type = type; 52: try { 53: ByteArrayOutputStream os = new ByteArrayOutputStream(); 54: int ch; 55: 56: while ((ch = is.read()) != -1) 57: // XXX - must be made more efficient by 58: // doing buffered reads, rather than one byte reads 59: os.write(ch); 60: data = os.toByteArray(); 61: 62: } catch (IOException ioex) { } 63: } 64: 65: /* Create a DataSource from a byte array */ 66: public ByteArrayDataSource(byte[] data, String type) { 67: this.data = data; 68: this.type = type; 69: } 70: 71: /* Create a DataSource from a String */ 72: public ByteArrayDataSource(String data, String type) { 73: try { 74: // Assumption that the string contains only ASCII 75: // characters! Otherwise just pass a charset into this 76: // constructor and use it in getBytes() 77: this.data = data.getBytes("iso-8859-1"); 78: } catch (UnsupportedEncodingException uex) { } 79: this.type = type; 80: } 81: 82: /** 83: * Return an InputStream for the data. 84: * Note - a new stream must be returned each time. 85: */ 86: public InputStream getInputStream() throws IOException { 87: if (data == null) 88: throw new IOException("no data"); 89: return new ByteArrayInputStream(data); 90: } 91: 92: public OutputStream getOutputStream() throws IOException { 93: throw new IOException("cannot do this"); 94: } 95: 96: public String getContentType() { 97: return type; 98: } 99: 100: public String getName() { 101: return "dummy"; 102: } 103:}