1 /* 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 * @(#)MimePartDataSource.java 1.9 02/03/27 28 */ 29 30 31 32 package com.sun.xml.internal.messaging.saaj.packaging.mime.internet; 33 34 import java.io.*; 35 import java.net.UnknownServiceException; 36 37 import javax.activation.DataSource; 38 39 import com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException; 40 41 /** 42 * A utility class that implements a DataSource out of 43 * a MimeBodyPart. This class is primarily meant for service providers. 44 * 45 * @author John Mani 46 */ 47 48 public final class MimePartDataSource implements DataSource { 49 private final MimeBodyPart part; 50 51 /** 52 * Constructor, that constructs a DataSource from a MimeBodyPart. 53 */ 54 public MimePartDataSource(MimeBodyPart part) { 55 this.part = part; 56 } 57 58 /** 59 * Returns an input stream from this MimeBodyPart. <p> 60 * 61 * This method applies the appropriate transfer-decoding, based 62 * on the Content-Transfer-Encoding attribute of this MimeBodyPart. 63 * Thus the returned input stream is a decoded stream of bytes.<p> 64 * 65 * This implementation obtains the raw content from the MimeBodyPart 66 * using the <code>getContentStream()</code> method and decodes 67 * it using the <code>MimeUtility.decode()</code> method. 68 * 69 * @return decoded input stream 70 */ 71 public InputStream getInputStream() throws IOException { 72 73 try { 74 InputStream is = part.getContentStream(); 75 76 String encoding = part.getEncoding(); 77 if (encoding != null) 78 return MimeUtility.decode(is, encoding); 79 else 80 return is; 81 } catch (MessagingException mex) { 82 throw new IOException(mex.getMessage()); 83 } 84 } 85 86 /** 87 * DataSource method to return an output stream. <p> 88 * 89 * This implementation throws the UnknownServiceException. 90 */ 91 public OutputStream getOutputStream() throws IOException { 92 throw new UnknownServiceException(); 93 } 94 95 /** 96 * Returns the content-type of this DataSource. <p> 97 * 98 * This implementation just invokes the <code>getContentType</code> 99 * method on the MimeBodyPart. 100 */ 101 public String getContentType() { 102 return part.getContentType(); 103 } 104 105 /** 106 * DataSource method to return a name. <p> 107 * 108 * This implementation just returns an empty string. 109 */ 110 public String getName() { 111 try { 112 return part.getFileName(); 113 } catch (MessagingException mex) { 114 return ""; 115 } 116 } 117 }