1 package com.ozacc.mail.impl; 2 3 /* 4 * @(#)ByteArrayDataSource.java 1.4 01/05/23 5 * 6 * Copyright 1998-2000 Sun Microsystems, Inc. All Rights Reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * - Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * - Redistribution in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * Neither the name of Sun Microsystems, Inc. or the names of contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * This software is provided "AS IS," without a warranty of any kind. ALL 24 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, 25 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A 26 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND 27 * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES 28 * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION 29 * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL 30 * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR 31 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE 32 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, 33 * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS 34 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 35 * 36 * You acknowledge that Software is not designed, licensed or intended 37 * for use in the design, construction, operation or maintenance of any 38 * nuclear facility. 39 */ 40 41 import java.io.ByteArrayInputStream; 42 import java.io.ByteArrayOutputStream; 43 import java.io.IOException; 44 import java.io.InputStream; 45 import java.io.OutputStream; 46 import java.io.UnsupportedEncodingException; 47 48 import javax.activation.DataSource; 49 50 /*** 51 * InputStream、byte array、StringからDataSourceインスタンスを生成するクラス。<br> 52 * JavaMailパッケージ付属品。 53 * <p> 54 * <strong>注:</strong> このクラスはpublicですが、ozacc-mail library外からは使用しないでください。 55 * 56 * @since 1.1 57 * 58 * @author John Mani 59 * @author Bill Shannon 60 * @author Max Spivak 61 */ 62 public class ByteArrayDataSource implements DataSource { 63 64 private byte[] data; // data 65 66 private String type; // content-type 67 68 /* Create a DataSource from an input stream */ 69 public ByteArrayDataSource(InputStream is, String type) { 70 this.type = type; 71 try { 72 ByteArrayOutputStream os = new ByteArrayOutputStream(); 73 int ch; 74 75 while ((ch = is.read()) != -1) 76 // XXX - must be made more efficient by 77 // doing buffered reads, rather than one byte reads 78 os.write(ch); 79 data = os.toByteArray(); 80 81 } catch (IOException ioex) {} 82 } 83 84 /* Create a DataSource from a byte array */ 85 public ByteArrayDataSource(byte[] data, String type) { 86 this.data = data; 87 this.type = type; 88 } 89 90 /* Create a DataSource from a String */ 91 public ByteArrayDataSource(String data, String type) { 92 try { 93 // Assumption that the string contains only ASCII 94 // characters! Otherwise just pass a charset into this 95 // constructor and use it in getBytes() 96 this.data = data.getBytes("iso-8859-1"); 97 } catch (UnsupportedEncodingException uex) {} 98 this.type = type; 99 } 100 101 /*** 102 * Return an InputStream for the data. 103 * Note - a new stream must be returned each time. 104 */ 105 public InputStream getInputStream() throws IOException { 106 if (data == null) 107 throw new IOException("no data"); 108 return new ByteArrayInputStream(data); 109 } 110 111 public OutputStream getOutputStream() throws IOException { 112 throw new IOException("cannot do this"); 113 } 114 115 public String getContentType() { 116 return type; 117 } 118 119 public String getName() { 120 return "dummy"; 121 } 122 }