KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > util > io > v1 > MimeOutputStream


1 /*
2  * MimeOutputStream.java - A Filter stream for MIME encoding
3  *
4  * Copyright (C) 2000,,2003 2002 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23
24 package net.sourceforge.groboutils.util.io.v1;
25
26 import java.io.FilterOutputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.awt.TextComponent JavaDoc;
30
31 /**
32  * java.io.FilterOutputStream implementation for Mime base 64. Not incredibly
33  * efficient, but it works and is small.
34  * <P>
35  * All we need to implement are:
36  * <ul>
37  * <li>write(int)</li>
38  * <li>flush()</li>
39  * </ul>
40  *
41  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
42  * @since 0.9.0 Alpha (early 2000)
43  * @version $Date: 2003/02/10 22:52:45 $
44  */

45 public class MimeOutputStream extends FilterOutputStream JavaDoc
46 {
47     private int bits = 0, spare = 0;
48
49     /**
50      * Mime character set translation
51      */

52     private static final int[] charset = {
53        'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R',
54        'S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j',
55        'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1',
56        '2','3','4','5','6','7','8','9','+','/' };
57     private static final int pad = '=';
58
59     /** Constructor! */
60     public MimeOutputStream( OutputStream JavaDoc o )
61     { super(o); }
62
63     /**
64      * Write the specified <code>byte</code>, performing mime encoding.
65      *
66      * <p>Override this method, since all other write methods call it.
67      *
68      * @exception IOException If an I/O error occurs
69      */

70     public void write(int c) throws IOException JavaDoc
71     {
72        int s, t;
73        switch (bits)
74        {
75           case 0: bits++;
76               s = (c >> 2) & 0x3f;
77               spare = c & 0x03;
78               out.write( charset[s] );
79             break;
80           case 1: bits++;
81               s = (spare << 4) | ((c >> 4) & 0x0f);
82               spare = c & 0x0f;
83               out.write( charset[s] );
84             break;
85           case 2: bits = 0;
86               s = (spare << 2) | ((c >> 6) & 0x03);
87               t = c & 0x3f;
88               out.write( charset[s] );
89               out.write( charset[t] );
90             break;
91        }
92     }
93
94
95     /**
96      * Flush the stream. If the stream has saved any characters from the
97      * various write() methods in a buffer, write them immediately to their
98      * intended destination. Then, if that destination is another character or
99      * byte stream, flush it. Thus one flush() invocation will flush all the
100      * buffers in a chain of Writers and OutputStreams.
101      * <P>
102      * This version does not buffer, but Mime encoding may have some trailing
103      * stuff, which needs padding.
104      *
105      * @exception IOException If an I/O error occurs
106      */

107     public void flush() throws IOException JavaDoc
108     {
109        switch (bits)
110        {
111           case 1:
112               out.write( charset[spare << 4] );
113               out.write( pad );
114               out.write( pad );
115             break;
116           case 2:
117               out.write( charset[spare << 2] );
118               out.write( pad );
119             break;
120        }
121        super.flush();
122     }
123 }
124
Popular Tags