]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/util/BASE64OutputStream.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / util / BASE64OutputStream.java
1 /*
2  * Copyright (c) 2001-2007 Sun Microsystems, Inc.  All rights reserved.
3  *  
4  *  The Sun Project JXTA(TM) Software License
5  *  
6  *  Redistribution and use in source and binary forms, with or without 
7  *  modification, are permitted provided that the following conditions are met:
8  *  
9  *  1. Redistributions of source code must retain the above copyright notice,
10  *     this list of conditions and the following disclaimer.
11  *  
12  *  2. Redistributions in binary form must reproduce the above copyright notice, 
13  *     this list of conditions and the following disclaimer in the documentation 
14  *     and/or other materials provided with the distribution.
15  *  
16  *  3. The end-user documentation included with the redistribution, if any, must 
17  *     include the following acknowledgment: "This product includes software 
18  *     developed by Sun Microsystems, Inc. for JXTA(TM) technology." 
19  *     Alternately, this acknowledgment may appear in the software itself, if 
20  *     and wherever such third-party acknowledgments normally appear.
21  *  
22  *  4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must 
23  *     not be used to endorse or promote products derived from this software 
24  *     without prior written permission. For written permission, please contact 
25  *     Project JXTA at http://www.jxta.org.
26  *  
27  *  5. Products derived from this software may not be called "JXTA", nor may 
28  *     "JXTA" appear in their name, without prior written permission of Sun.
29  *  
30  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
31  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
32  *  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SUN 
33  *  MICROSYSTEMS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
34  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
35  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
36  *  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
37  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
38  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
39  *  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  *  
41  *  JXTA is a registered trademark of Sun Microsystems, Inc. in the United 
42  *  States and other countries.
43  *  
44  *  Please see the license information page at :
45  *  <http://www.jxta.org/project/www/license.html> for instructions on use of 
46  *  the license in source files.
47  *  
48  *  ====================================================================
49  *  
50  *  This software consists of voluntary contributions made by many individuals 
51  *  on behalf of Project JXTA. For more information on Project JXTA, please see 
52  *  http://www.jxta.org.
53  *  
54  *  This license is based on the BSD license adopted by the Apache Foundation. 
55  */
56
57 package net.jxta.impl.util;
58
59
60 import java.io.OutputStream;
61 import java.io.Writer;
62
63 import java.io.IOException;
64 import java.io.StringReader;
65 import java.io.ByteArrayOutputStream;
66 import java.io.StringWriter;
67
68
69 /**
70  * An OutputStream implementation which encodes the written bytes into BASE64
71  * encoded character data and writes the output to an associated text Writer.
72  *
73  * <p/>This implementation is not thread safe.
74  *
75  * @see net.jxta.impl.util.BASE64InputStream
76  * @see <a href="http://www.ietf.org/rfc/rfc2045.txt" target="_blank">IETF RFC 2045 <i>MIME : Format of Internet Message Bodies</i></a>
77  **/
78 public class BASE64OutputStream extends OutputStream {
79     
80     /**
81      *  If <code>true</code> then the output stream has been closed.
82      **/
83     private boolean closed = false;
84     
85     /**
86      *  The output writer.
87      **/
88     private Writer sendTo = null;
89     
90     /**
91      *  Column width to breakup out.
92      **/
93     private final int columnWidth;
94     
95     /**
96      *  Current output column.
97      **/
98     private int column = 0;
99     
100     /**
101      *  Buffer for incomplete characters.
102      **/
103     private byte[] buffer = new byte[] { 0, 0, 0 };
104     
105     /**
106      *  The number of characters currently in the buffer.
107      **/
108     private byte inBuffer = 0;
109     
110     /**
111      * Construct a BASE64 Output Stream.
112      *
113      * @param sendTo The text Writer to which the BASE64 output will be
114      * written.
115      **/
116     public BASE64OutputStream(Writer sendTo) {
117         this(sendTo, -1);
118     }
119     
120     /**
121      * Construct a BASE64 Output Stream. The output will be broken into lines
122      * <code>columnWidth</code> long.
123      *
124      * @param sendTo The text Writer to which the BASE64 output will be
125      * written.
126      * @param columnWidth The width of lines to break output into.
127      **/
128     public BASE64OutputStream(Writer sendTo, int columnWidth) {
129         this.sendTo = sendTo;
130         this.columnWidth = columnWidth;
131     }
132     
133     /**
134      *  {@inheritDoc}
135      **/
136     @Override
137     public void write(int b) throws IOException {
138         if (closed) {
139             throw new IOException("OutputStream closed.");
140         }
141         
142         buffer[ inBuffer++ ] = (byte) b;
143         
144         if (buffer.length == inBuffer) {
145             writeBuffer();
146         }
147     }
148     
149     /**
150      *  {@inheritDoc}
151      *
152      *  <p/>The output writer is <b>NOT</b> closed.
153      **/
154     @Override
155     public void close() throws IOException {
156         flush();
157         
158         closed = true;
159         sendTo = null;
160     }
161     
162     /**
163      *  {@inheritDoc}
164      **/
165     @Override
166     public void flush() throws IOException {
167         writeBuffer();
168     }
169     
170     /**
171      *  Write a full or partial buffer to the output writer.
172      **/
173     private void writeBuffer() throws IOException {
174         if (0 == inBuffer) {
175             return;
176         }
177         
178         int val = ((buffer[0] & 0x00FF) << 16) + ((buffer[1] & 0x00FF) << 8) + (buffer[2] & 0x00FF);
179         
180         int c0 = (val >> 18) & 0x003F;
181         int c1 = (val >> 12) & 0x003F;
182         int c2 = (val >> 6) & 0x003F;
183         int c3 = val & 0x003F;
184         
185         if ((columnWidth > 0) && (column > columnWidth)) {
186             sendTo.write('\n');
187             column = 0;
188         }
189         
190         sendTo.write(encodeSixBits(c0));
191         sendTo.write(encodeSixBits(c1));
192         
193         if (inBuffer > 1) {
194             sendTo.write(encodeSixBits(c2));
195         } else {
196             sendTo.write('=');
197         }
198         
199         if (inBuffer > 2) {
200             sendTo.write(encodeSixBits(c3));
201         } else {
202             sendTo.write('=');
203         }
204         
205         buffer[0] = 0;
206         buffer[1] = 0;
207         buffer[2] = 0;
208         
209         inBuffer = 0;
210         column += 4;
211     }
212     
213     /**
214      *  BASE64 Encoding Table
215      **/
216     static final char encode[] = {
217         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X'
218                 ,
219         'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v'
220                 ,
221         'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
222     };
223     
224     /**
225      *  Encode six bits into a character using the standard BASE64 table.
226      *
227      *  @param b the bits to encode. b must be >=0 and <= 63
228      *  @return the appropriate character for the input value.
229      **/
230     private static char encodeSixBits(int b) {
231         char c;
232         
233         if ((b < 0) || (b > 63)) {
234             throw new IllegalArgumentException("bad encode value");
235         } else {
236             c = encode[b];
237         }
238         
239         return c;
240     }
241 }