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