View Javadoc
1 /* 2 * PROJECT : emailEJB 3 * 4 * COPYRIGHT : Copyright (C) 1999-2003 Tim Stephenson 5 * 6 * contact me at gpl@enableit.org. 7 * 8 * This library is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Lesser General Public 10 * License as published by the Free Software Foundation; either 11 * version 2.1 of the License, or (at your option) any later version. 12 * 13 * This library is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with this library; if not, write to the Free Software 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 */ 22 package org.enableit.tools.email.handlers; 23 24 // Java Imports 25 import java.awt.datatransfer.DataFlavor ; 26 import java.io.BufferedOutputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.OutputStream ; 30 import javax.activation.ActivationDataFlavor ; 31 import javax.activation.DataContentHandler ; 32 import javax.activation.DataSource ; 33 34 // Log4J Imports 35 import org.apache.log4j.Category; 36 37 /*** 38 * Data content handler for binary data types. 39 * 40 * <p>The J2EE documentation says that 'application/octet-stream' should 41 * be specified as the mime type for unknown data types. This DCH, once 42 * registered, while handle that type.</p> 43 * 44 * <p>TODO: document the interesting configuration problems for this class.</p> 45 * 46 * @author Tim Stephenson 47 */ 48 public class OctetStreamDataContentHandler implements DataContentHandler { 49 50 /* 51 * Properties 52 */ 53 /*** 54 * The Log4J <code>Category</code> doing the logging. 55 */ 56 private static Category logger = 57 Category.getInstance(OctetStreamDataContentHandler.class); 58 59 /*** 60 * Internal ref to the DataFlavor I am handler for. 61 */ 62 private static ActivationDataFlavor myDF; 63 64 /* 65 * Constructors 66 */ 67 /*** 68 * Default Constructor. 69 */ 70 public OctetStreamDataContentHandler() { 71 72 } 73 74 /* 75 * Methods 76 */ 77 78 /*** 79 * Return an object representing the data in its most preferred form. 80 * @param ds The datasource to get the content of. 81 * @return The content (as a <code>byte[]</code>) 82 * @throws IOException If there is a problem in reading the input. 83 */ 84 public Object getContent(DataSource ds) 85 throws IOException { 86 logger.info("METHOD_ENTRY: getContent"); 87 88 InputStream is = ds.getInputStream() ; 89 logger.debug("InputStream is of type" + is.getClass().getName()); 90 int j; 91 byte data[] = new byte[1024]; 92 byte dataCopy[]; 93 // read 1Kb at a time into byte array 94 for (int i = 0; (j = is.read(data, i, 1024)) != -1; data = dataCopy) { 95 i += j; 96 dataCopy = new byte[i + 1024]; 97 System.arraycopy(data, 0, dataCopy, 0, i); 98 } 99 100 logger.info("METHOD_EXIT: getContent"); 101 return data; 102 } 103 104 /*** 105 * Returns an object which represents the data to be transferred. 106 * @param df The <code>DataFlavor</code> the datasource represents. 107 * @param ds The datasource to get the content of. 108 * @return The content (as a <code>byte[]</code>) or null if the 109 * DataFlavor does not match my internal one. 110 * @throws IOException If there is a problem in reading the input. 111 */ 112 public Object getTransferData(DataFlavor df, DataSource ds) 113 throws IOException { 114 logger.info("METHOD_ENTRY: getTransferData"); 115 Object obj = null ; 116 if (myDF.equals(df)) { 117 obj = getContent(ds); 118 } else { 119 obj = null; 120 } 121 logger.info("METHOD_EXIT: getTransferData"); 122 return obj ; 123 } 124 125 /*** 126 * Returns an array of DataFlavor objects indicating the flavors the data 127 * can be provided in. 128 * @return The <code>DataFlavors</code> this 129 * <code>DataContentHandler</code> can handle. 130 */ 131 public DataFlavor[] getTransferDataFlavors() { 132 logger.info("METHOD_ENTRY: getTransferDataFlavors"); 133 DataFlavor[] df = new DataFlavor[] { myDF }; 134 logger.info("METHOD_EXIT: getTransferDataFlavors"); 135 return df ; 136 } 137 138 /*** 139 * Convert the object to a byte stream of the specified MIME type and 140 * write it to the output stream. 141 * @param obj The content object (as a <code>byte[]</code>) or null if the 142 * DataFlavor does not match my internal one. 143 * @param mimeType The mime type of the data object. 144 * @param os <code>OutputStream</code> to write the data object to. 145 * @throws IOException If there is a problem in writing the output. 146 */ 147 public void writeTo(Object obj, String mimeType, OutputStream os) 148 throws IOException { 149 logger.info("METHOD_ENTRY: writeTo"); 150 if (!(obj instanceof byte[])) { 151 throw new IOException("\"" + myDF.getMimeType() 152 + "\" DataContentHandler requires byte[] object, " 153 + "was given object of type " 154 + obj.getClass().toString()); 155 } 156 byte[] data = (byte[]) obj ; 157 BufferedOutputStream out = new BufferedOutputStream(os) ; 158 out.write(data) ; 159 out.flush(); 160 logger.info("METHOD_EXIT: writeTo"); 161 } 162 163 }

This page was automatically generated by Maven