Quadcap Embeddable Database

com/quadcap/io/Base64OutputStream.java

Go to the documentation of this file.
00001 package com.quadcap.io; 00002 00003 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.ByteArrayOutputStream; 00042 import java.io.IOException; 00043 import java.io.OutputStream; 00044 00045 /** 00046 * A filter output stream usually, converting binary octets to their 00047 * base64 representation. If no filter outputstream is specified, we 00048 * collect the output into a buffer and can return it via 'toString()' 00049 * 00050 * @author Stan Bailes 00051 */ 00052 public class Base64OutputStream extends OutputStream { 00053 OutputStream out; 00054 int accum; 00055 int pos = 0; 00056 public boolean doLineBreaks = true; 00057 00058 public static byte[] base64 = { 00059 (byte)'A', (byte)'B', (byte)'C', (byte)'D', 00060 (byte)'E', (byte)'F', (byte)'G', (byte)'H', 00061 (byte)'I', (byte)'J', (byte)'K', (byte)'L', 00062 (byte)'M', (byte)'N', (byte)'O', (byte)'P', 00063 (byte)'Q', (byte)'R', (byte)'S', (byte)'T', 00064 (byte)'U', (byte)'V', (byte)'W', (byte)'X', 00065 (byte)'Y', (byte)'Z', (byte)'a', (byte)'b', 00066 (byte)'c', (byte)'d', (byte)'e', (byte)'f', 00067 (byte)'g', (byte)'h', (byte)'i', (byte)'j', 00068 (byte)'k', (byte)'l', (byte)'m', (byte)'n', 00069 (byte)'o', (byte)'p', (byte)'q', (byte)'r', 00070 (byte)'s', (byte)'t', (byte)'u', (byte)'v', 00071 (byte)'w', (byte)'x', (byte)'y', (byte)'z', 00072 (byte)'0', (byte)'1', (byte)'2', (byte)'3', 00073 (byte)'4', (byte)'5', (byte)'6', (byte)'7', 00074 (byte)'8', (byte)'9', (byte)'+', (byte)'/' 00075 }; 00076 00077 /** 00078 * Default constructor for catpure as string 00079 */ 00080 public Base64OutputStream() { 00081 this.out = new ByteArrayOutputStream(); 00082 } 00083 00084 /** 00085 * Constructor for output stream chaining mode. 00086 */ 00087 public Base64OutputStream(OutputStream out) { 00088 this.out = out; 00089 } 00090 00091 /** 00092 * Write a byte. 00093 */ 00094 public void write(int c) throws IOException { 00095 accum <<= 8; 00096 accum |= (c & 0xff); 00097 if ((++pos % 3) == 0) { 00098 out.write(base64[(accum >> 18) & 0x3f]); 00099 out.write(base64[(accum >> 12) & 0x3f]); 00100 out.write(base64[(accum >> 6) & 0x3f]); 00101 out.write(base64[(accum >> 0) & 0x3f]); 00102 accum = 0; 00103 } 00104 if (doLineBreaks && (pos % 54) == 0) { 00105 out.write('\r'); 00106 out.write('\n'); 00107 } 00108 } 00109 00110 /** 00111 * Finish the base64 encoding operation... 00112 */ 00113 public void finish() throws IOException { 00114 int p = pos % 3; 00115 if (p == 1) { 00116 accum <<= 16; 00117 out.write(base64[(accum >> 18) & 0x3f]); 00118 out.write(base64[(accum >> 12) & 0x3f]); 00119 out.write('='); 00120 out.write('='); 00121 } else if (p == 2) { 00122 accum <<= 8; 00123 out.write(base64[(accum >> 18) & 0x3f]); 00124 out.write(base64[(accum >> 12) & 0x3f]); 00125 out.write(base64[(accum >> 6) & 0x3f]); 00126 out.write('='); 00127 } 00128 } 00129 00130 /** 00131 * Return a string representation, if we can 00132 */ 00133 public String toString() { 00134 return out.toString(); 00135 } 00136 }