KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > io > Base64OutputStream


1 package com.quadcap.io;
2
3 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.ByteArrayOutputStream JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.OutputStream JavaDoc;
44
45 /**
46  * A filter output stream usually, converting binary octets to their
47  * base64 representation. If no filter outputstream is specified, we
48  * collect the output into a buffer and can return it via 'toString()'
49  *
50  * @author Stan Bailes
51  */

52 public class Base64OutputStream extends OutputStream JavaDoc {
53     OutputStream JavaDoc out;
54     int accum;
55     int pos = 0;
56     public boolean doLineBreaks = true;
57
58     public static byte[] base64 = {
59     (byte)'A', (byte)'B', (byte)'C', (byte)'D',
60     (byte)'E', (byte)'F', (byte)'G', (byte)'H',
61     (byte)'I', (byte)'J', (byte)'K', (byte)'L',
62     (byte)'M', (byte)'N', (byte)'O', (byte)'P',
63     (byte)'Q', (byte)'R', (byte)'S', (byte)'T',
64     (byte)'U', (byte)'V', (byte)'W', (byte)'X',
65     (byte)'Y', (byte)'Z', (byte)'a', (byte)'b',
66     (byte)'c', (byte)'d', (byte)'e', (byte)'f',
67     (byte)'g', (byte)'h', (byte)'i', (byte)'j',
68     (byte)'k', (byte)'l', (byte)'m', (byte)'n',
69     (byte)'o', (byte)'p', (byte)'q', (byte)'r',
70     (byte)'s', (byte)'t', (byte)'u', (byte)'v',
71     (byte)'w', (byte)'x', (byte)'y', (byte)'z',
72     (byte)'0', (byte)'1', (byte)'2', (byte)'3',
73     (byte)'4', (byte)'5', (byte)'6', (byte)'7',
74     (byte)'8', (byte)'9', (byte)'+', (byte)'/'
75     };
76
77     /**
78      * Default constructor for catpure as string
79      */

80     public Base64OutputStream() {
81         this.out = new ByteArrayOutputStream JavaDoc();
82     }
83
84     /**
85      * Constructor for output stream chaining mode.
86      */

87     public Base64OutputStream(OutputStream JavaDoc out) {
88     this.out = out;
89     }
90
91     /**
92      * Write a byte.
93      */

94     public void write(int c) throws IOException JavaDoc {
95     accum <<= 8;
96     accum |= (c & 0xff);
97     if ((++pos % 3) == 0) {
98         out.write(base64[(accum >> 18) & 0x3f]);
99         out.write(base64[(accum >> 12) & 0x3f]);
100         out.write(base64[(accum >> 6) & 0x3f]);
101         out.write(base64[(accum >> 0) & 0x3f]);
102         accum = 0;
103     }
104     if (doLineBreaks && (pos % 54) == 0) {
105         out.write('\r');
106         out.write('\n');
107     }
108     }
109
110     /**
111      * Finish the base64 encoding operation...
112      */

113     public void finish() throws IOException JavaDoc {
114     int p = pos % 3;
115     if (p == 1) {
116         accum <<= 16;
117         out.write(base64[(accum >> 18) & 0x3f]);
118         out.write(base64[(accum >> 12) & 0x3f]);
119         out.write('=');
120         out.write('=');
121     } else if (p == 2) {
122         accum <<= 8;
123         out.write(base64[(accum >> 18) & 0x3f]);
124         out.write(base64[(accum >> 12) & 0x3f]);
125         out.write(base64[(accum >> 6) & 0x3f]);
126         out.write('=');
127     }
128     }
129
130     /**
131      * Return a string representation, if we can
132      */

133     public String JavaDoc toString() {
134         return out.toString();
135     }
136 }
137
Popular Tags