Re: Problems implementing DataHandler for AttachmentPart

From: Vishal Mahajan <Vishal.Mahajan_at_Sun.COM>
Date: Mon, 18 Oct 2004 19:46:48 +0530

Hi Oliver,

Please see inline ...

Oliver Jaun wrote:

>I want to implement my own DataHandler because I need
>strict base64 encoding for my SOAP Attachments. The
>BASE64EncoderStream makes newlines after 76
>characters. This does not conform to strict base64
>encoding (newline is not part of base64 alphabet).
RFC 2045 (Format of Internet Message Bodies) talks about Base64
Content-Transfer-Encoding. Here's the relevant portion of this RFC:

   The encoded output stream must be represented in lines of no more
   than 76 characters each. All line breaks or other characters not
   found in Table 1 must be ignored by decoding software. In base64
   data, characters other than those in Table 1, line breaks, and other
   white space probably indicate a transmission error, about which a
   warning message or even a message rejection might be appropriate
   under some circumstances.

This indicates that the attachment APIs are doing the right thing by
restricting the size of the lines to 76 characters.

>If you create a AttachmentPart and you use
>setMimeHeader("Content-Type-Encoding", "base64") then
>the default BASE64EncoderStream is used.
Yes that's true. When you set the value of Content-Transfer-Encoding
MIME header to "base64", the javax.mail.internet.MimeBodyPart.writeTo()
method writes the data to a Base64 encoding output stream. The
attachment APIs implementation is built on top of javax.mail.internet.*
classes, so there's no work-around to this situation.



>So I implemeted my own DataHandler and added it to the
>AttachmentPart. In the DataHandler I implemented the
>writeTo(OutputStream os) method. Now something funny
>(well, not really ;-) happens: The OutputStream passed
>is already a BASE64EncoderStream. My idea was to do
>something like this: MyBase64Encoder(os) and write te
>data from the DataSource to this stream.
>I either got something completly wrong or this is a
>bug... Here's my code
>public getUpdate(String updateId, String
>terminalId, BigInteger sliceNo) {
>try {
>MessageContext mc =
>FileInputStream fis = new
>InputStreamDataSource ids = new InputStreamDataSource(
>fis, "application/octet-stream", "distributedFile");
>DataHandler dh = new MyDataHandler(ids);
>AttachmentPart att =
>System.out.println("attachmentpart: " +
>ArrayList list = new ArrayList();
> retVal = new
>return retVal;
>} catch(Exception e) {
>return null;
>The Data Handler:
>class MyDataHandler extends DataHandler {
>public MyDataHandler(DataSource ds) {
>System.out.println("init mydatahandler");
>// os is passed as BASE64EncoderStream.... why???? I
>want to encode it myself!
>public void writeTo(OutputStream os) throws
> {
>System.out.println("writeToooooooooooooooooooooo: " +
>super.writeTo(new Base64.OutputStream(os,
>I hope anyone can help... Thanks a lot in advance
>Oliver Jaun
>Do you Yahoo!?
>Declare Yourself - Register online to vote today!
>To unsubscribe, e-mail:
>For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail: