ByteArrayDataSource.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:4k
源码类别:
Java编程
开发平台:
Java
- /*
- * @(#)ByteArrayDataSource.java 1.4 01/05/23
- *
- * Copyright 1998-2000 Sun Microsystems, Inc. All Rights Reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistribution in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * Neither the name of Sun Microsystems, Inc. or the names of contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * This software is provided "AS IS," without a warranty of any kind. ALL
- * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
- * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
- * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
- * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
- * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
- * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
- * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
- * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
- * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
- * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
- *
- * You acknowledge that Software is not designed, licensed or intended
- * for use in the design, construction, operation or maintenance of any
- * nuclear facility.
- */
- import java.io.*;
- import javax.activation.*;
- /**
- * A simple DataSource for demonstration purposes.
- * This class implements a DataSource from:
- * an InputStream
- * a byte array
- * a String
- *
- * @author John Mani
- * @author Bill Shannon
- * @author Max Spivak
- */
- public class ByteArrayDataSource implements DataSource {
- private byte[] data; // data
- private String type; // content-type
- /* Create a DataSource from an input stream */
- public ByteArrayDataSource(InputStream is, String type) {
- this.type = type;
- try {
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- int ch;
- while ((ch = is.read()) != -1)
- // XXX - must be made more efficient by
- // doing buffered reads, rather than one byte reads
- os.write(ch);
- data = os.toByteArray();
- } catch (IOException ioex) { }
- }
- /* Create a DataSource from a byte array */
- public ByteArrayDataSource(byte[] data, String type) {
- this.data = data;
- this.type = type;
- }
- /* Create a DataSource from a String */
- public ByteArrayDataSource(String data, String type) {
- try {
- // Assumption that the string contains only ASCII
- // characters! Otherwise just pass a charset into this
- // constructor and use it in getBytes()
- this.data = data.getBytes("iso-8859-1");
- } catch (UnsupportedEncodingException uex) { }
- this.type = type;
- }
- /**
- * Return an InputStream for the data.
- * Note - a new stream must be returned each time.
- */
- public InputStream getInputStream() throws IOException {
- if (data == null)
- throw new IOException("no data");
- return new ByteArrayInputStream(data);
- }
- public OutputStream getOutputStream() throws IOException {
- throw new IOException("cannot do this");
- }
- public String getContentType() {
- return type;
- }
- public String getName() {
- return "dummy";
- }
- }