/************************************************************************** * Copyright (c) 2006 by Chris Gray, /k/ Embedded Java Solutions. * * All rights reserved. * * * * Redistribution and use in source and binary forms, with or without * * modification, are permitted provided that the following conditions * * are met: * * 1. Redistributions of source code must retain the above copyright * * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * * notice, this list of conditions and the following disclaimer in the * * documentation and/or other materials provided with the distribution. * * 3. Neither the name of /k/ Embedded Java Solutions nor the names of * * other contributors may be used to endorse or promote products * * derived from this software without specific prior written permission.* * * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED * * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * * IN NO EVENT SHALL /K/ EMBEDDED JAVA SOLUTIONS OR OTHER CONTRIBUTORS BE * * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR * * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE * * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN * * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * **************************************************************************/ package wonka.encoder; import java.io.*; /** * A class to encode Base64 streams and strings. * See RFC 1521 section 5.2 for details of the Base64 algorithm. * * This class can be used for encoding strings: * * String unencoded = "webmaster:try2gueSS"; * String encoded = Base64Encoder.encode(unencoded); * * or for encoding streams: * * OutputStream out = new Base64Encoder(System.out); * * * @author Jason Hunter, Copyright � 2000 * @version 1.0, 2000/06/11 */ public class Base64Encoder extends FilterOutputStream { private static final char[] chars = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' }; private int charCount; private int carryOver; /** * Constructs a new Base64 encoder that writes output to the given * OutputStream. * * @param out the output stream */ public Base64Encoder(OutputStream out) { super(out); } /** * Writes the given byte to the output stream in an encoded form. * * @exception IOException if an I/O error occurs */ public void write(int b) throws IOException { // Take 24-bits from three octets, translate into four encoded chars // Break lines at 76 chars // If necessary, pad with 0 bits on the right at the end // Use = signs as padding at the end to ensure encodedLength % 4 == 0 b &= 0xff; // First byte use first six bits, save last two bits if (charCount % 3 == 0) { int lookup = b >> 2; carryOver = b & 3; // last two bits out.write(chars[lookup]); } // Second byte use previous two bits and first four new bits, // save last four bits else if (charCount % 3 == 1) { int lookup = ((carryOver << 4) + (b >> 4)) & 63; carryOver = b & 15; // last four bits out.write(chars[lookup]); } // Third byte use previous four bits and first two new bits, // then use last six new bits else if (charCount % 3 == 2) { int lookup = ((carryOver << 2) + (b >> 6)) & 63; out.write(chars[lookup]); lookup = b & 63; // last six bits out.write(chars[lookup]); carryOver = 0; } charCount++; // Add newline every 76 output chars (that's 57 input chars) if (charCount % 57 == 0) { out.write('\n'); } } /** * Writes the given byte array to the output stream in an * encoded form. * * @param b the data to be written * @param off the start offset of the data * @param len the length of the data * @exception IOException if an I/O error occurs */ public void write(byte[] b, int off, int len) throws IOException { // This could of course be optimized for (int i = 0; i < len; i++) { write(b[off + i]); } } /** * Closes the stream, this MUST be called to ensure proper padding is * written to the end of the output stream. * * @exception IOException if an I/O error occurs */ public void close() throws IOException { // Handle leftover bytes if (charCount % 3 == 1) { // one leftover int lookup = (carryOver << 4) & 63; out.write(chars[lookup]); out.write('='); out.write('='); } else if (charCount % 3 == 2) { // two leftovers int lookup = (carryOver << 2) & 63; out.write(chars[lookup]); out.write('='); } else { out.write('='); } super.close(); } /** * Returns the encoded form of the given unencoded string. * * @param unencoded the string to encode * @return the encoded form of the unencoded string */ public static String encode(String unencoded) { ByteArrayOutputStream out = new ByteArrayOutputStream((int) (unencoded.length() * 1.37)); Base64Encoder encodedOut = new Base64Encoder(out); byte[] bytes = null; try { bytes = unencoded.getBytes("8859_1"); } catch (UnsupportedEncodingException ignored) { } try { encodedOut.write(bytes); encodedOut.close(); return out.toString("8859_1"); } catch (IOException ignored) { return null; } } public static void main(String[] args) throws Exception { if (args.length != 1) { System.err.println("Usage: java Base64Encoder fileToEncode"); } Base64Encoder encoder = null; BufferedInputStream in = null; try { encoder = new Base64Encoder(System.out); in = new BufferedInputStream(new FileInputStream(args[0])); byte[] buf = new byte[4 * 1024]; // 4K buffer int bytesRead; while ((bytesRead = in.read(buf)) != -1) { encoder.write(buf, 0, bytesRead); } } finally { if (in != null) in.close(); if (encoder != null) encoder.close(); } } }