001: package com.knowgate.dfs;
002:
003: /*
004: * @(#)ByteArrayDataSource.java 1.4 01/05/23
005: *
006: * Copyright 1998-2000 Sun Microsystems, Inc. All Rights Reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * - Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * - Redistribution in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in the
017: * documentation and/or other materials provided with the distribution.
018: *
019: * Neither the name of Sun Microsystems, Inc. or the names of contributors
020: * may be used to endorse or promote products derived from this software
021: * without specific prior written permission.
022: *
023: * This software is provided "AS IS," without a warranty of any kind. ALL
024: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
025: * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
026: * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
027: * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
028: * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
029: * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
030: * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
031: * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
032: * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
033: * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
034: * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that Software is not designed, licensed or intended
037: * for use in the design, construction, operation or maintenance of any
038: * nuclear facility.
039: */
040:
041: import java.io.*;
042: import javax.activation.*;
043:
044: /**
045: * A simple DataSource for demonstration purposes.
046: * This class implements a DataSource from:
047: * an InputStream
048: * a byte array
049: * a String
050: *
051: * @author John Mani
052: * @author Bill Shannon
053: * @author Max Spivak
054: */
055: public class ByteArrayDataSource implements DataSource {
056: private byte[] data; // data
057: private String type; // content-type
058:
059: /* Create a DataSource from an input stream */
060: public ByteArrayDataSource(InputStream is, String type) {
061: this .type = type;
062: try {
063: ByteArrayOutputStream os = new ByteArrayOutputStream();
064: int ch;
065:
066: while ((ch = is.read()) != -1)
067: // XXX - must be made more efficient by
068: // doing buffered reads, rather than one byte reads
069: os.write(ch);
070: data = os.toByteArray();
071:
072: } catch (IOException ioex) {
073: }
074: }
075:
076: /* Create a DataSource from a byte array */
077: public ByteArrayDataSource(byte[] data, String type) {
078: this .data = data;
079: this .type = type;
080: }
081:
082: /* Create a DataSource from a String */
083: public ByteArrayDataSource(String data, String type) {
084: try {
085: // Assumption that the string contains only ASCII
086: // characters! Otherwise just pass a charset into this
087: // constructor and use it in getBytes()
088: this .data = data.getBytes("iso-8859-1");
089: } catch (UnsupportedEncodingException uex) {
090: }
091: this .type = type;
092: }
093:
094: /**
095: * Return an InputStream for the data.
096: * Note - a new stream must be returned each time.
097: */
098: public InputStream getInputStream() throws IOException {
099: if (data == null)
100: throw new IOException("no data");
101: return new ByteArrayInputStream(data);
102: }
103:
104: public OutputStream getOutputStream() throws IOException {
105: throw new IOException("cannot do this");
106: }
107:
108: public String getContentType() {
109: return type;
110: }
111:
112: public String getName() {
113: return "dummy";
114: }
115: }
|