1 5 package com.knowgate.misc; 6 7 import java.io.*; 8 9 28 public class Base64Decoder extends FilterInputStream { 29 30 private static final char[] chars = { 31 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 32 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 33 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 34 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 35 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 36 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', 37 '8', '9', '+', '/' 38 }; 39 40 private static final int[] ints = new int[128]; 42 static { 43 for (int i = 0; i < 64; i++) { 44 ints[chars[i]] = i; 45 } 46 } 47 48 private int charCount; 49 private int carryOver; 50 51 57 public Base64Decoder(InputStream in) { 58 super(in); 59 } 60 61 69 public int read() throws IOException { 70 int x; 72 do { 73 x = in.read(); 74 if (x == -1) { 75 return -1; 76 } 77 } while (Character.isWhitespace((char)x)); 78 charCount++; 79 80 if (x == '=') { 82 return -1; } 84 85 x = ints[x]; 87 88 int mode = (charCount - 1) % 4; 90 91 if (mode == 0) { 93 carryOver = x & 63; 94 return read(); 95 } 96 else if (mode == 1) { 99 int decoded = ((carryOver << 2) + (x >> 4)) & 255; 100 carryOver = x & 15; 101 return decoded; 102 } 103 else if (mode == 2) { 106 int decoded = ((carryOver << 4) + (x >> 2)) & 255; 107 carryOver = x & 3; 108 return decoded; 109 } 110 else if (mode == 3) { 112 int decoded = ((carryOver << 6) + x) & 255; 113 return decoded; 114 } 115 return -1; } 117 118 129 public int read(byte[] buf, int off, int len) throws IOException { 130 if (buf.length < (len + off - 1)) { 131 throw new IOException("The input buffer is too small: " + len + 132 " bytes requested starting at offset " + off + " while the buffer " + 133 " is only " + buf.length + " bytes long."); 134 } 135 136 int i; 138 for (i = 0; i < len; i++) { 139 int x = read(); 140 if (x == -1 && i == 0) { return -1; 142 } 143 else if (x == -1) { break; 145 } 146 buf[off + i] = (byte) x; 147 } 148 return i; 149 } 150 151 160 public static String decode(String encoded) { 161 return new String (decodeToBytes(encoded)); 162 } 163 164 170 public static byte[] decodeToBytes(String encoded) { 171 byte[] bytes = null; 172 try { 173 bytes = encoded.getBytes("8859_1"); 174 } 175 catch (UnsupportedEncodingException ignored) { } 176 177 Base64Decoder in = new Base64Decoder( 178 new ByteArrayInputStream(bytes)); 179 180 ByteArrayOutputStream out = 181 new ByteArrayOutputStream((int) (bytes.length * 0.67)); 182 183 try { 184 byte[] buf = new byte[4 * 1024]; int bytesRead; 186 while ((bytesRead = in.read(buf)) != -1) { 187 out.write(buf, 0, bytesRead); 188 } 189 out.close(); 190 191 return out.toByteArray(); 192 } 193 catch (IOException ignored) { return null; } 194 } 195 196 public static void main(String [] args) throws Exception { 197 if (args.length != 1) { 198 System.err.println("Usage: java Base64Decoder fileToDecode"); 199 return; 200 } 201 202 Base64Decoder decoder = null; 203 try { 204 decoder = new Base64Decoder( 205 new BufferedInputStream( 206 new FileInputStream(args[0]))); 207 byte[] buf = new byte[4 * 1024]; int bytesRead; 209 while ((bytesRead = decoder.read(buf)) != -1) { 210 System.out.write(buf, 0, bytesRead); 211 } 212 } 213 finally { 214 if (decoder != null) decoder.close(); 215 } 216 } 217 } 218 | Popular Tags |