001: package com.quadcap.io;
002:
003: /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.ByteArrayOutputStream;
042: import java.io.IOException;
043: import java.io.OutputStream;
044:
045: /**
046: * A filter output stream usually, converting binary octets to their
047: * base64 representation. If no filter outputstream is specified, we
048: * collect the output into a buffer and can return it via 'toString()'
049: *
050: * @author Stan Bailes
051: */
052: public class Base64OutputStream extends OutputStream {
053: OutputStream out;
054: int accum;
055: int pos = 0;
056: public boolean doLineBreaks = true;
057:
058: public static byte[] base64 = { (byte) 'A', (byte) 'B', (byte) 'C',
059: (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H',
060: (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L', (byte) 'M',
061: (byte) 'N', (byte) 'O', (byte) 'P', (byte) 'Q', (byte) 'R',
062: (byte) 'S', (byte) 'T', (byte) 'U', (byte) 'V', (byte) 'W',
063: (byte) 'X', (byte) 'Y', (byte) 'Z', (byte) 'a', (byte) 'b',
064: (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f', (byte) 'g',
065: (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k', (byte) 'l',
066: (byte) 'm', (byte) 'n', (byte) 'o', (byte) 'p', (byte) 'q',
067: (byte) 'r', (byte) 's', (byte) 't', (byte) 'u', (byte) 'v',
068: (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z', (byte) '0',
069: (byte) '1', (byte) '2', (byte) '3', (byte) '4', (byte) '5',
070: (byte) '6', (byte) '7', (byte) '8', (byte) '9', (byte) '+',
071: (byte) '/' };
072:
073: /**
074: * Default constructor for catpure as string
075: */
076: public Base64OutputStream() {
077: this .out = new ByteArrayOutputStream();
078: }
079:
080: /**
081: * Constructor for output stream chaining mode.
082: */
083: public Base64OutputStream(OutputStream out) {
084: this .out = out;
085: }
086:
087: /**
088: * Write a byte.
089: */
090: public void write(int c) throws IOException {
091: accum <<= 8;
092: accum |= (c & 0xff);
093: if ((++pos % 3) == 0) {
094: out.write(base64[(accum >> 18) & 0x3f]);
095: out.write(base64[(accum >> 12) & 0x3f]);
096: out.write(base64[(accum >> 6) & 0x3f]);
097: out.write(base64[(accum >> 0) & 0x3f]);
098: accum = 0;
099: }
100: if (doLineBreaks && (pos % 54) == 0) {
101: out.write('\r');
102: out.write('\n');
103: }
104: }
105:
106: /**
107: * Finish the base64 encoding operation...
108: */
109: public void finish() throws IOException {
110: int p = pos % 3;
111: if (p == 1) {
112: accum <<= 16;
113: out.write(base64[(accum >> 18) & 0x3f]);
114: out.write(base64[(accum >> 12) & 0x3f]);
115: out.write('=');
116: out.write('=');
117: } else if (p == 2) {
118: accum <<= 8;
119: out.write(base64[(accum >> 18) & 0x3f]);
120: out.write(base64[(accum >> 12) & 0x3f]);
121: out.write(base64[(accum >> 6) & 0x3f]);
122: out.write('=');
123: }
124: }
125:
126: /**
127: * Return a string representation, if we can
128: */
129: public String toString() {
130: return out.toString();
131: }
132: }
|