001 /** 002 * 003 * Copyright 2006 The Apache Software Foundation 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package javax.mail.util; 019 020 import java.io.ByteArrayOutputStream; 021 import java.io.ByteArrayInputStream; 022 import java.io.IOException; 023 import java.io.InputStream; 024 import java.io.OutputStream; 025 026 import javax.activation.DataSource; 027 import javax.mail.internet.ContentType; 028 import javax.mail.internet.ParseException; 029 import javax.mail.internet.MimeUtility; 030 031 032 /** 033 * An activation DataSource object that sources the data from 034 * a byte[] array. 035 * @version $Rev: 421852 $ $Date: 2006-07-14 03:02:19 -0700 (Fri, 14 Jul 2006) $ 036 */ 037 public class ByteArrayDataSource implements DataSource { 038 // the data source 039 private byte[] source; 040 // the content MIME type 041 private String contentType; 042 // the name information (defaults to a null string) 043 private String name = ""; 044 045 046 /** 047 * Create a ByteArrayDataSource from an input stream. 048 * 049 * @param in The source input stream. 050 * @param type The MIME-type of the data. 051 * 052 * @exception IOException 053 */ 054 public ByteArrayDataSource(InputStream in, String type) throws IOException { 055 ByteArrayOutputStream sink = new ByteArrayOutputStream(); 056 057 // ok, how I wish you could just pipe an input stream into an output stream :-) 058 byte[] buffer = new byte[8192]; 059 int bytesRead; 060 061 while ((bytesRead = in.read(buffer)) > 0) { 062 sink.write(buffer, 0, bytesRead); 063 } 064 065 source = sink.toByteArray(); 066 contentType = type; 067 } 068 069 070 /** 071 * Create a ByteArrayDataSource directly from a byte array. 072 * 073 * @param data The source byte array (not copied). 074 * @param type The content MIME-type. 075 */ 076 public ByteArrayDataSource(byte[] data, String type) { 077 source = data; 078 contentType = type; 079 } 080 081 /** 082 * Create a ByteArrayDataSource from a string value. If the 083 * type information includes a charset parameter, that charset 084 * is used to extract the bytes. Otherwise, the default Java 085 * char set is used. 086 * 087 * @param data The source data string. 088 * @param type The MIME type information. 089 * 090 * @exception IOException 091 */ 092 public ByteArrayDataSource(String data, String type) throws IOException { 093 String charset = null; 094 try { 095 // the charset can be encoded in the content type, which we parse using 096 // the ContentType class. 097 ContentType content = new ContentType(type); 098 charset = content.getParameter("charset"); 099 } catch (ParseException e) { 100 // ignored...just use the default if this fails 101 } 102 if (charset == null) { 103 charset = MimeUtility.getDefaultJavaCharset(); 104 } 105 else { 106 // the type information encodes a MIME charset, which may need mapping to a Java one. 107 charset = MimeUtility.javaCharset(charset); 108 } 109 110 // get the source using the specified charset 111 source = data.getBytes(charset); 112 contentType = type; 113 } 114 115 116 /** 117 * Create an input stream for this data. A new input stream 118 * is created each time. 119 * 120 * @return An InputStream for reading the encapsulated data. 121 * @exception IOException 122 */ 123 public InputStream getInputStream() throws IOException { 124 return new ByteArrayInputStream(source); 125 } 126 127 128 /** 129 * Open an output stream for the DataSource. This is not 130 * supported by this DataSource, so an IOException is always 131 * throws. 132 * 133 * @return Nothing...an IOException is always thrown. 134 * @exception IOException 135 */ 136 public OutputStream getOutputStream() throws IOException { 137 throw new IOException("Writing to a ByteArrayDataSource is not supported"); 138 } 139 140 141 /** 142 * Get the MIME content type information for this DataSource. 143 * 144 * @return The MIME content type string. 145 */ 146 public String getContentType() { 147 return contentType; 148 } 149 150 151 /** 152 * Retrieve the DataSource name. If not explicitly set, this 153 * returns "". 154 * 155 * @return The currently set DataSource name. 156 */ 157 public String getName() { 158 return name; 159 } 160 161 162 /** 163 * Set a new DataSource name. 164 * 165 * @param name The new name. 166 */ 167 public void setName(String name) { 168 this.name = name; 169 } 170 } 171