Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Issue with jdk 1.4.2, and creating a custom DataSource

843834Apr 15 2008 — edited Apr 16 2008
For various reasons, I need to insert a file based on an InputStream into a MIME message. My initial version of code used a FileDataSourceobject, which, when passed to the DataHandler object, worked just fine - the file was inserted inline into the MIME message. Great.

Second attemp was to take an input stream for the same file, and insert that into the MIME message. I had to create a custom Data Source to wrap the InputStream, as you can see below. However, when I run the code, I get a stack dump indicating that the descriptor is bad. This is really strange, since I can read the input stream...

Anyone have any suggestions?
/*
 * Created on Apr 9, 2008
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package com.example;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.crimson.tree.XmlDocument;
import org.apache.crimson.tree.XmlDocumentBuilder;
import org.w3c.dom.Element;

public class MimeTest {

	public static void main(String[] args) {
		MimeTest test = new MimeTest();
		try {
			test.run();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void run() throws Exception {

		try {

			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			XmlDocumentBuilder builder = new XmlDocumentBuilder();
			XmlDocument doc = builder.createDocument();

			Element soapEnvelope = doc.createElement("SOAP:Envelope");
			soapEnvelope.setAttribute("xmlns:SOAP", "http://schemas.xmlsoap.org/soap/envelope/");
			doc.appendChild(soapEnvelope);

			Element soapHeader = doc.createElement("SOAP:Header");
			soapHeader.setAttribute("xmlns:SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
			soapHeader.setAttribute("xmlns:xsd", "http://www.w3.org/1999/XMLSchema");
			soapHeader.setAttribute("xmlns:xsi", "http://www.w3.org/1999/XMLSchema-instance");
			soapHeader.setAttribute("xmlns:SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/");
			soapEnvelope.appendChild(soapHeader);

			class InputStreamDataSource implements DataSource {
				InputStream is;
				String name;
				String contentType;
				public InputStreamDataSource(InputStream is, String name, String contentType) {
					this.is = is;
					this.name = name;
					this.contentType = contentType;
				}
				public String getContentType() {
					return contentType;
				}
				public String getName() {
					return name;
				}
				public InputStream getInputStream() throws IOException {
					return is;
				}
				public OutputStream getOutputStream() throws IOException {
					throw new IOException("Function not supported");
				}
			}

			Message msg = new MimeMessage((Session) null);

			MimeMultipart multiPart = new MimeMultipart("related");

			MimeBodyPart part = new MimeBodyPart();
			InputStream fis = new FileInputStream( "d:/temp/x.gif" );
			part.setDataHandler( new DataHandler( new InputStreamDataSource( fis, "name", "image/gif")));
			part.setDisposition("attachment;filename=x.gif");
			multiPart.addBodyPart(part);

			msg.setContent(multiPart);
			msg.writeTo(System.out);

		} catch (MessagingException mex) {
			mex.printStackTrace();
			Exception ex = null;
			if ((ex = mex.getNextException()) != null) {
				ex.printStackTrace();
			}
		}

	}
}
Here's the stack dump:

java.io.IOException: Bad file descriptor
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:177)
at javax.activation.DataHandler.writeTo(DataHandler.java:299)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1089)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:635)
at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:233)
at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:68)
at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:849)
at javax.activation.DataHandler.writeTo(DataHandler.java:305)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1089)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1527)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1503)
at ca.huskyenergy.MimeTest.run(MimeTest.java:98)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 14 2008
Added on Apr 15 2008
2 comments
2,468 views