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 org.apache.geronimo.mail.util;
021    
022    import java.io.IOException;
023    import java.io.OutputStream;
024    import java.io.FilterOutputStream;
025    
026    /**
027     * An implementation of a FilterOutputStream that encodes the
028     * stream data in BASE64 encoding format.  This version does the
029     * encoding "on the fly" rather than encoding a single block of
030     * data.  Since this version is intended for use by the MimeUtilty class,
031     * it also handles line breaks in the encoded data.
032     *
033     * @version $Rev: 467553 $ $Date: 2006-10-25 06:01:51 +0200 (Mi, 25. Okt 2006) $
034     */
035    public class Base64EncoderStream extends FilterOutputStream {
036    
037        // our Filtered stream writes everything out as byte data.  This allows the CRLF sequence to
038        // be written with a single call.
039        protected static final byte[] CRLF = { '\r', '\n' };
040    
041        // our hex encoder utility class.
042        protected Base64Encoder encoder = new Base64Encoder();
043    
044        // our default for line breaks
045        protected static final int DEFAULT_LINEBREAK = 76;
046    
047        // Data can only be written out in complete units of 3 bytes encoded as 4.  Therefore, we need to buffer
048        // as many as 2 bytes to fill out an encoding unit.
049    
050        // the buffered byte count
051        protected int bufferedBytes = 0;
052    
053        // we'll encode this part once it is filled up.
054        protected byte[] buffer = new byte[3];
055    
056    
057        // the size we process line breaks at.  If this is Integer.MAX_VALUE, no line breaks are handled.
058        protected int lineBreak;
059    
060        // the number of encoded characters we've written to the stream, which determines where we
061        // insert line breaks.
062        protected int outputCount;
063    
064        /**
065         * Create a Base64 encoder stream that wraps a specifed stream
066         * using the default line break size.
067         *
068         * @param out    The wrapped output stream.
069         */
070        public Base64EncoderStream(OutputStream out) {
071            this(out, DEFAULT_LINEBREAK);
072        }
073    
074    
075        public Base64EncoderStream(OutputStream out, int lineBreak) {
076            super(out);
077            // lines are processed only in multiple of 4, so round this down.
078            this.lineBreak = (lineBreak / 4) * 4 ;
079        }
080    
081        // in order for this to work, we need to override the 3 different signatures for write
082    
083        public void write(int ch) throws IOException {
084            // store this in the buffer.
085            buffer[bufferedBytes++] = (byte)ch;
086            // if the buffer is filled, encode these bytes
087            if (bufferedBytes == 3) {
088                // check for room in the current line for this character
089                checkEOL(4);
090                // write these directly to the stream.
091                encoder.encode(buffer, 0, 3, out);
092                bufferedBytes = 0;
093                // and update the line length checkers
094                updateLineCount(4);
095            }
096        }
097    
098        public void write(byte [] data) throws IOException {
099            write(data, 0, data.length);
100        }
101    
102        public void write(byte [] data, int offset, int length) throws IOException {
103            // if we have something in the buffer, we need to write enough bytes out to flush
104            // those into the output stream AND continue on to finish off a line.  Once we're done there
105            // we can write additional data out in complete blocks.
106            while ((bufferedBytes > 0 || outputCount > 0) && length > 0) {
107                write(data[offset++]);
108                length--;
109            }
110    
111            if (length > 0) {
112                // no linebreaks requested?  YES!!!!!, we can just dispose of the lot with one call.
113                if (lineBreak == Integer.MAX_VALUE) {
114                    encoder.encode(data, offset, length, out);
115                }
116                else {
117                    // calculate the size of a segment we can encode directly as a line.
118                    int segmentSize = (lineBreak / 4) * 3;
119    
120                    // write this out a block at a time, with separators between.
121                    while (length > segmentSize) {
122                        // encode a segment
123                        encoder.encode(data, offset, segmentSize, out);
124                        // write an EOL marker
125                        out.write(CRLF);
126                        offset += segmentSize;
127                        length -= segmentSize;
128                    }
129    
130                    // any remainder we write out a byte at a time to manage the groupings and
131                    // the line count appropriately.
132                    if (length > 0) {
133                        while (length > 0) {
134                            write(data[offset++]);
135                            length--;
136                        }
137                    }
138                }
139            }
140        }
141        
142        public void close() throws IOException {
143            flush();
144            out.close();
145        }
146        
147        public void flush() throws IOException {
148            if (bufferedBytes > 0) {
149                encoder.encode(buffer, 0, bufferedBytes, out);
150                bufferedBytes = 0;
151            }
152        }
153    
154    
155        /**
156         * Check for whether we're about the reach the end of our
157         * line limit for an update that's about to occur.  If we will
158         * overflow, then a line break is inserted.
159         *
160         * @param required The space required for this pending write.
161         *
162         * @exception IOException
163         */
164        private void checkEOL(int required) throws IOException {
165            if (lineBreak != Integer.MAX_VALUE) {
166                // if this write would exceed the line maximum, add a linebreak to the stream.
167                if (outputCount + required > lineBreak) {
168                    out.write(CRLF);
169                    outputCount = 0;
170                }
171            }
172        }
173    
174        /**
175         * Update the counter of characters on the current working line.
176         * This is conditional if we're not working with a line limit.
177         *
178         * @param added  The number of characters just added.
179         */
180        private void updateLineCount(int added) {
181            if (lineBreak != Integer.MAX_VALUE) {
182                outputCount += added;
183            }
184        }
185    }
186