001    /**
002     *
003     * Copyright 2003-2006 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package javax.mail.internet;
019    
020    import java.io.IOException;
021    import java.io.OutputStream;
022    
023    import javax.mail.MessagingException;
024    
025    /**
026     * @version $Rev: 421852 $ $Date: 2006-07-14 03:02:19 -0700 (Fri, 14 Jul 2006) $
027     */
028    
029    
030    public class PreencodedMimeBodyPart extends MimeBodyPart {
031        // the defined transfer encoding
032        private String transferEncoding;
033    
034    
035        /**
036         * Create a new body part with the specified MIME transfer encoding.
037         *
038         * @param encoding The content encoding.
039         */
040        public PreencodedMimeBodyPart(String encoding) {
041            transferEncoding = encoding;
042        }
043    
044    
045        /**
046         * Retieve the defined encoding for this body part.
047         *
048         * @return
049         * @exception MessagingException
050         */
051        public String getEncoding() throws MessagingException {
052            return transferEncoding;
053        }
054    
055        /**
056         * Write the body part content to the stream without applying
057         * and additional encodings.
058         *
059         * @param out    The target output stream.
060         *
061         * @exception IOException
062         * @exception MessagingException
063         */
064        public void writeTo(OutputStream out) throws IOException, MessagingException {
065            headers.writeTo(out, null);
066            // add the separater between the headers and the data portion.
067            out.write('\r');
068            out.write('\n');
069            // write this out without getting an encoding stream
070            getDataHandler().writeTo(out);
071            out.flush();
072        }
073    
074    
075        /**
076         * Override of update headers to ensure the transfer encoding
077         * is forced to the correct type.
078         *
079         * @exception MessagingException
080         */
081        protected void updateHeaders() throws MessagingException {
082            super.updateHeaders();
083            setHeader("Content-Transfer-Encoding", transferEncoding);
084        }
085    }
086