Clover coverage report - Maven Clover report
Coverage timestamp: Sun Aug 20 2006 04:01:44 PDT
file stats: LOC: 171   Methods: 8
NCLOC: 60   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ByteArrayDataSource.java 25% 95.7% 100% 88.6%
coverage coverage
 1    /**
 2    *
 3    * Copyright 2006 The Apache Software Foundation
 4    *
 5    * Licensed under the Apache License, Version 2.0 (the "License");
 6    * you may not use this file except in compliance with the License.
 7    * You may obtain a copy of the License at
 8    *
 9    * http://www.apache.org/licenses/LICENSE-2.0
 10    *
 11    * Unless required by applicable law or agreed to in writing, software
 12    * distributed under the License is distributed on an "AS IS" BASIS,
 13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14    * See the License for the specific language governing permissions and
 15    * limitations under the License.
 16    */
 17   
 18    package javax.mail.util;
 19   
 20    import java.io.ByteArrayOutputStream;
 21    import java.io.ByteArrayInputStream;
 22    import java.io.IOException;
 23    import java.io.InputStream;
 24    import java.io.OutputStream;
 25   
 26    import javax.activation.DataSource;
 27    import javax.mail.internet.ContentType;
 28    import javax.mail.internet.ParseException;
 29    import javax.mail.internet.MimeUtility;
 30   
 31   
 32    /**
 33    * An activation DataSource object that sources the data from
 34    * a byte[] array.
 35    * @version $Rev: 421852 $ $Date: 2006-07-14 03:02:19 -0700 (Fri, 14 Jul 2006) $
 36    */
 37    public class ByteArrayDataSource implements DataSource {
 38    // the data source
 39    private byte[] source;
 40    // the content MIME type
 41    private String contentType;
 42    // the name information (defaults to a null string)
 43    private String name = "";
 44   
 45   
 46    /**
 47    * Create a ByteArrayDataSource from an input stream.
 48    *
 49    * @param in The source input stream.
 50    * @param type The MIME-type of the data.
 51    *
 52    * @exception IOException
 53    */
 54  1 public ByteArrayDataSource(InputStream in, String type) throws IOException {
 55  1 ByteArrayOutputStream sink = new ByteArrayOutputStream();
 56   
 57    // ok, how I wish you could just pipe an input stream into an output stream :-)
 58  1 byte[] buffer = new byte[8192];
 59  1 int bytesRead;
 60   
 61  ? while ((bytesRead = in.read(buffer)) > 0) {
 62  1 sink.write(buffer, 0, bytesRead);
 63    }
 64   
 65  1 source = sink.toByteArray();
 66  1 contentType = type;
 67    }
 68   
 69   
 70    /**
 71    * Create a ByteArrayDataSource directly from a byte array.
 72    *
 73    * @param data The source byte array (not copied).
 74    * @param type The content MIME-type.
 75    */
 76  7 public ByteArrayDataSource(byte[] data, String type) {
 77  7 source = data;
 78  7 contentType = type;
 79    }
 80   
 81    /**
 82    * Create a ByteArrayDataSource from a string value. If the
 83    * type information includes a charset parameter, that charset
 84    * is used to extract the bytes. Otherwise, the default Java
 85    * char set is used.
 86    *
 87    * @param data The source data string.
 88    * @param type The MIME type information.
 89    *
 90    * @exception IOException
 91    */
 92  3 public ByteArrayDataSource(String data, String type) throws IOException {
 93  3 String charset = null;
 94  3 try {
 95    // the charset can be encoded in the content type, which we parse using
 96    // the ContentType class.
 97  3 ContentType content = new ContentType(type);
 98  3 charset = content.getParameter("charset");
 99    } catch (ParseException e) {
 100    // ignored...just use the default if this fails
 101    }
 102  3 if (charset == null) {
 103  3 charset = MimeUtility.getDefaultJavaCharset();
 104    }
 105    else {
 106    // the type information encodes a MIME charset, which may need mapping to a Java one.
 107  0 charset = MimeUtility.javaCharset(charset);
 108    }
 109   
 110    // get the source using the specified charset
 111  3 source = data.getBytes(charset);
 112  3 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  9 public InputStream getInputStream() throws IOException {
 124  9 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  1 public OutputStream getOutputStream() throws IOException {
 137  1 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  9 public String getContentType() {
 147  9 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  2 public String getName() {
 158  2 return name;
 159    }
 160   
 161   
 162    /**
 163    * Set a new DataSource name.
 164    *
 165    * @param name The new name.
 166    */
 167  1 public void setName(String name) {
 168  1 this.name = name;
 169    }
 170    }
 171