Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.lib.base64; |
| 2 | |
| 3 | import java.io.*; |
| 4 | |
| 5 | /* |
| 6 | * Base 64 converter. |
| 7 | * |
| 8 | * TODO Implement string to byte[] |
| 9 | */ |
| 10 | public class Base64 { |
| 11 | byte[] data; |
| 12 | |
| 13 | static final String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 14 | static byte[] values = new byte[128]; |
| 15 | |
| 16 | static { |
| 17 | for (int i = 0; i < values.length; i++) { |
| 18 | values[i] = -1; |
| 19 | } |
| 20 | // Create reverse index |
| 21 | for (int i = 0; i < alphabet.length(); i++) { |
| 22 | char c = alphabet.charAt(i); |
| 23 | values[c] = (byte) i; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | public Base64(byte data[]) { |
| 28 | this.data = data; |
| 29 | } |
| 30 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 31 | public final static byte[] decodeBase64(String string) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 32 | ByteArrayOutputStream bout = new ByteArrayOutputStream(string.length() * 2 / 3); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 33 | StringReader rdr = new StringReader(string.trim()); |
| 34 | try { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 35 | decode(rdr, bout); |
| 36 | } |
| 37 | catch (Exception e) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 38 | // cannot happen |
| 39 | } |
| 40 | return bout.toByteArray(); |
| 41 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 42 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 43 | public final static void decode(Reader rdr, OutputStream out) throws Exception { |
| 44 | int register = 0; |
| 45 | int i = 0; |
| 46 | int pads = 0; |
| 47 | |
| 48 | byte test[] = new byte[3]; |
| 49 | int c; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 50 | while ((c = rdr.read()) >= 0) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 51 | |
| 52 | if (c > 0x7F) |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 53 | throw new IllegalArgumentException("Invalid base64 character in " + rdr + ", character value > 128 "); |
| 54 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 55 | int v = 0; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 56 | if (c == '=') { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 57 | pads++; |
| 58 | } else { |
| 59 | v = values[c]; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 60 | if (v < 0) |
| 61 | throw new IllegalArgumentException("Invalid base64 character in " + rdr + ", " + c); |
| 62 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 63 | register <<= 6; |
| 64 | register |= v; |
| 65 | test[2] = (byte) (register & 0xFF); |
| 66 | test[1] = (byte) ((register >> 8) & 0xFF); |
| 67 | test[0] = (byte) ((register >> 16) & 0xFF); |
| 68 | |
| 69 | i++; |
| 70 | |
| 71 | if ((i % 4) == 0) { |
| 72 | flush(out, register, pads); |
| 73 | register = 0; |
| 74 | pads = 0; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | static private void flush(OutputStream out, int register, int pads) throws IOException { |
| 80 | switch (pads) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 81 | case 0 : |
| 82 | out.write(0xFF & (register >> 16)); |
| 83 | out.write(0xFF & (register >> 8)); |
| 84 | out.write(0xFF & (register >> 0)); |
| 85 | break; |
| 86 | |
| 87 | case 1 : |
| 88 | out.write(0xFF & (register >> 16)); |
| 89 | out.write(0xFF & (register >> 8)); |
| 90 | break; |
| 91 | |
| 92 | case 2 : |
| 93 | out.write(0xFF & (register >> 16)); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
| 97 | public Base64(String s) { |
| 98 | data = decodeBase64(s); |
| 99 | } |
| 100 | |
Stuart McCulloch | 55d4dfe | 2012-08-07 10:57:21 +0000 | [diff] [blame^] | 101 | @Override |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 102 | public String toString() { |
| 103 | return encodeBase64(data); |
| 104 | } |
| 105 | |
| 106 | public static String encodeBase64(byte data[]) { |
| 107 | StringWriter sw = new StringWriter(); |
| 108 | ByteArrayInputStream bin = new ByteArrayInputStream(data); |
| 109 | try { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 110 | encode(bin, sw); |
| 111 | } |
| 112 | catch (IOException e) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 113 | // can't happen |
| 114 | } |
| 115 | return sw.toString(); |
| 116 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 117 | |
| 118 | public Object toData() { |
| 119 | return data; |
| 120 | } |
| 121 | |
| 122 | public static void encode(InputStream in, Appendable sb) throws IOException { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 123 | // StringBuilder sb = new StringBuilder(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 124 | int buf = 0; |
| 125 | int bits = 0; |
| 126 | int out = 0; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 127 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 128 | while (true) { |
| 129 | if (bits >= 6) { |
| 130 | bits -= 6; |
| 131 | int v = 0x3F & (buf >> bits); |
| 132 | sb.append(alphabet.charAt(v)); |
| 133 | out++; |
| 134 | } else { |
| 135 | int c = in.read(); |
| 136 | if (c < 0) |
| 137 | break; |
| 138 | |
| 139 | buf <<= 8; |
| 140 | buf |= 0xFF & c; |
| 141 | bits += 8; |
| 142 | } |
| 143 | } |
| 144 | if (bits != 0) {// must be less than 7 |
| 145 | sb.append(alphabet.charAt(0x3F & (buf << (6 - bits)))); |
| 146 | out++; |
| 147 | } |
| 148 | int mod = 4 - (out % 4); |
| 149 | if (mod != 4) { |
| 150 | for (int i = 0; i < mod; i++) |
| 151 | sb.append('='); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | } |