package org.madore.damlengine; import java.io.InputStream; import java.io.IOException; public final class Base64 { public static final char[] base64Alphabet = { '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', '+', '/' }; public static String encode(InputStream str) { byte[] inb = new byte[768]; char[] outb = new char[1024]; StringBuffer strbuf = new StringBuffer(); try { int len; while ( ( len = str.read(inb) ) > 0 ) { for ( int i=0 ; 3*i>>2)&0x3f]; if ( 3*i+1 < len ) outb[4*i+1] = base64Alphabet[(inb[3*i]&3)<<4 | ((inb[3*i+1]>>>4)&0xf)]; else outb[4*i+1] = base64Alphabet[(inb[3*i]&3)<<4]; if ( 3*i+2 < len ) outb[4*i+2] = base64Alphabet[(inb[3*i+1]&0xf)<<2 | ((inb[3*i+2]>>>6)&0x3)]; else if ( 3*i+1 < len ) outb[4*i+2] = base64Alphabet[(inb[3*i+1]&0xf)<<2]; else outb[4*i+2] = '='; if ( 3*i+2 < len ) outb[4*i+3] = base64Alphabet[(inb[3*i+2]&0x3f)]; else outb[4*i+3] = '='; } strbuf.append(outb, 0, ((len+2)/3)*4); } } catch (IOException e) { throw new RuntimeException(e); } return new String(strbuf); } }