001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. 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 */ 017package org.apache.commons.mail; 018 019import java.io.BufferedInputStream; 020import java.io.BufferedOutputStream; 021import java.io.ByteArrayInputStream; 022import java.io.ByteArrayOutputStream; 023import java.io.IOException; 024import java.io.InputStream; 025import java.io.OutputStream; 026import java.io.UnsupportedEncodingException; 027 028import javax.activation.DataSource; 029 030/** 031 * This class implements a typed DataSource from:<br> 032 * 033 * - an InputStream<br> 034 * - a byte array<br> 035 * - a String<br> 036 * 037 * <p> 038 * From version 1.3.1, it is possible to set a name for this DataSource, 039 * and it is recommended to do so. 040 * 041 * @since 1.0 042 * @deprecated since 1.4, use {@link javax.mail.util.ByteArrayDataSource} instead 043 */ 044@Deprecated 045public class ByteArrayDataSource implements DataSource 046{ 047 /** Define the buffer size. */ 048 public static final int BUFFER_SIZE = 512; 049 050 /** Stream containing the Data. */ 051 private ByteArrayOutputStream baos; 052 053 /** The Content-type. */ 054 private final String type; // = "application/octet-stream"; 055 056 /** 057 * The name associated with this data source. 058 * By default, the name is an empty string, similar to javax.mail.util.ByteArrayDataSource. 059 * @since 1.3.1 060 */ 061 private String name = ""; 062 063 /** 064 * Create a datasource from a byte array. 065 * 066 * @param data A byte[]. 067 * @param aType A String. 068 * @throws IOException IOException 069 * @since 1.0 070 */ 071 public ByteArrayDataSource(final byte[] data, final String aType) throws IOException 072 { 073 this.type = aType; 074 ByteArrayInputStream bis = null; 075 076 try 077 { 078 bis = new ByteArrayInputStream(data); 079 this.byteArrayDataSource(bis); 080 } 081 finally 082 { 083 if (bis != null) 084 { 085 bis.close(); 086 } 087 } 088 } 089 090 /** 091 * Create a datasource from an input stream. 092 * 093 * @param aIs An InputStream. 094 * @param aType A String. 095 * @throws IOException IOException 096 * @since 1.0 097 */ 098 public ByteArrayDataSource(final InputStream aIs, final String aType) throws IOException 099 { 100 this.type = aType; 101 this.byteArrayDataSource(aIs); 102 } 103 104 /** 105 * Create a datasource from a String. 106 * N.B. assumes the data string can be converted using the charset iso-8859-1. 107 * 108 * @param data A String. 109 * @param aType A String. 110 * @throws IOException IOException 111 * @since 1.0 112 */ 113 public ByteArrayDataSource(final String data, final String aType) throws IOException 114 { 115 this.type = aType; 116 117 try 118 { 119 baos = new ByteArrayOutputStream(); 120 121 // Assumption that the string contains only ASCII characters! 122 // Else just pass in a charset into this constructor and use it in getBytes(). 123 baos.write(data.getBytes("iso-8859-1")); 124 baos.flush(); 125 baos.close(); 126 } 127 catch (final UnsupportedEncodingException uex) 128 { 129 throw new IOException("The Character Encoding is not supported."); 130 } 131 finally 132 { 133 if (baos != null) 134 { 135 baos.close(); 136 } 137 } 138 } 139 140 /** 141 * Create a datasource from an input stream. 142 * 143 * @param aIs An InputStream. 144 * @throws IOException IOException 145 */ 146 private void byteArrayDataSource(final InputStream aIs) 147 throws IOException 148 { 149 BufferedInputStream bis = null; 150 BufferedOutputStream osWriter = null; 151 152 try 153 { 154 int length = 0; 155 final byte[] buffer = new byte[ByteArrayDataSource.BUFFER_SIZE]; 156 157 bis = new BufferedInputStream(aIs); 158 baos = new ByteArrayOutputStream(); 159 osWriter = new BufferedOutputStream(baos); 160 161 // Write the InputData to OutputStream 162 while ((length = bis.read(buffer)) != -1) 163 { 164 osWriter.write(buffer, 0, length); 165 } 166 osWriter.flush(); 167 osWriter.close(); 168 169 } 170 finally 171 { 172 if (bis != null) 173 { 174 bis.close(); 175 } 176 if (baos != null) 177 { 178 baos.close(); 179 } 180 if (osWriter != null) 181 { 182 osWriter.close(); 183 } 184 } 185 } 186 187 /** 188 * Get the content type. 189 * 190 * @return A String. 191 * @since 1.0 192 */ 193 @Override 194 public String getContentType() 195 { 196 return type == null ? "application/octet-stream" : type; 197 } 198 199 /** 200 * Get the input stream. 201 * 202 * @return An InputStream. 203 * @throws IOException IOException 204 * @since 1.0 205 */ 206 @Override 207 public InputStream getInputStream() throws IOException 208 { 209 if (baos == null) 210 { 211 throw new IOException("no data"); 212 } 213 return new ByteArrayInputStream(baos.toByteArray()); 214 } 215 216 /** 217 * Sets the name for this DataSource. 218 * 219 * @param name The name. 220 * @since 1.3.1 221 */ 222 public void setName(final String name) 223 { 224 this.name = name; 225 } 226 227 /** 228 * Get the name. 229 * 230 * @return A String. 231 * @since 1.0 232 */ 233 @Override 234 public String getName() 235 { 236 return name; 237 } 238 239 /** 240 * Get the OutputStream to write to. 241 * 242 * @return An OutputStream 243 * @since 1.0 244 */ 245 @Override 246 public OutputStream getOutputStream() 247 { 248 baos = new ByteArrayOutputStream(); 249 return baos; 250 } 251}