package com.quadcap.io;
/* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
*
* This software is distributed under the Quadcap Free Software License.
* This software may be used or modified for any purpose, personal or
* commercial. Open Source redistributions are permitted. Commercial
* redistribution of larger works derived from, or works which bundle
* this software requires a "Commercial Redistribution License"; see
* http://www.quadcap.com/purchase.
*
* Redistributions qualify as "Open Source" under one of the following terms:
*
* Redistributions are made at no charge beyond the reasonable cost of
* materials and delivery.
*
* Redistributions are accompanied by a copy of the Source Code or by an
* irrevocable offer to provide a copy of the Source Code for up to three
* years at the cost of materials and delivery. Such redistributions
* must allow further use, modification, and redistribution of the Source
* Code under substantially the same terms as this license.
*
* Redistributions of source code must retain the copyright notices as they
* appear in each source code file, these license terms, and the
* disclaimer/limitation of liability set forth as paragraph 6 below.
*
* Redistributions in binary form must reproduce this Copyright Notice,
* these license terms, and the disclaimer/limitation of liability set
* forth as paragraph 6 below, in the documentation and/or other materials
* provided with the distribution.
*
* The Software is provided on an "AS IS" basis. No warranty is
* provided that the Software is free of defects, or fit for a
* particular purpose.
*
* Limitation of Liability. Quadcap Software shall not be liable
* for any damages suffered by the Licensee or any third party resulting
* from use of the Software.
*/
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* A filter output stream usually, converting binary octets to their
* base64 representation. If no filter outputstream is specified, we
* collect the output into a buffer and can return it via 'toString()'
*
* @author Stan Bailes
*/
public class Base64OutputStream extends OutputStream {
OutputStream out;
int accum;
int pos = 0;
public boolean doLineBreaks = true;
public static byte[] base64 = {
(byte)'A', (byte)'B', (byte)'C', (byte)'D',
(byte)'E', (byte)'F', (byte)'G', (byte)'H',
(byte)'I', (byte)'J', (byte)'K', (byte)'L',
(byte)'M', (byte)'N', (byte)'O', (byte)'P',
(byte)'Q', (byte)'R', (byte)'S', (byte)'T',
(byte)'U', (byte)'V', (byte)'W', (byte)'X',
(byte)'Y', (byte)'Z', (byte)'a', (byte)'b',
(byte)'c', (byte)'d', (byte)'e', (byte)'f',
(byte)'g', (byte)'h', (byte)'i', (byte)'j',
(byte)'k', (byte)'l', (byte)'m', (byte)'n',
(byte)'o', (byte)'p', (byte)'q', (byte)'r',
(byte)'s', (byte)'t', (byte)'u', (byte)'v',
(byte)'w', (byte)'x', (byte)'y', (byte)'z',
(byte)'0', (byte)'1', (byte)'2', (byte)'3',
(byte)'4', (byte)'5', (byte)'6', (byte)'7',
(byte)'8', (byte)'9', (byte)'+', (byte)'/'
};
/**
* Default constructor for catpure as string
*/
public Base64OutputStream() {
this.out = new ByteArrayOutputStream();
}
/**
* Constructor for output stream chaining mode.
*/
public Base64OutputStream(OutputStream out) {
this.out = out;
}
/**
* Write a byte.
*/
public void write(int c) throws IOException {
accum <<= 8;
accum |= (c & 0xff);
if ((++pos % 3) == 0) {
out.write(base64[(accum >> 18) & 0x3f]);
out.write(base64[(accum >> 12) & 0x3f]);
out.write(base64[(accum >> 6) & 0x3f]);
out.write(base64[(accum >> 0) & 0x3f]);
accum = 0;
}
if (doLineBreaks && (pos % 54) == 0) {
out.write('\r');
out.write('\n');
}
}
/**
* Finish the base64 encoding operation...
*/
public void finish() throws IOException {
int p = pos % 3;
if (p == 1) {
accum <<= 16;
out.write(base64[(accum >> 18) & 0x3f]);
out.write(base64[(accum >> 12) & 0x3f]);
out.write('=');
out.write('=');
} else if (p == 2) {
accum <<= 8;
out.write(base64[(accum >> 18) & 0x3f]);
out.write(base64[(accum >> 12) & 0x3f]);
out.write(base64[(accum >> 6) & 0x3f]);
out.write('=');
}
}
/**
* Return a string representation, if we can
*/
public String toString() {
return out.toString();
}
}
|