1 5 package com.knowgate.misc; 6 7 import java.io.*; 8 9 29 public class Base64Encoder extends FilterOutputStream { 30 31 private static final char[] chars = { 32 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 33 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 34 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 35 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 36 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 37 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', 38 '8', '9', '+', '/' 39 }; 40 41 private int charCount; 42 private int carryOver; 43 44 50 public Base64Encoder(OutputStream out) { 51 super(out); 52 } 53 54 59 public void write(int b) throws IOException { 60 65 if (b < 0) { 68 b += 256; 69 } 70 71 if (charCount % 3 == 0) { 73 int lookup = b >> 2; 74 carryOver = b & 3; out.write(chars[lookup]); 76 } 77 else if (charCount % 3 == 1) { 80 int lookup = ((carryOver << 4) + (b >> 4)) & 63; 81 carryOver = b & 15; out.write(chars[lookup]); 83 } 84 else if (charCount % 3 == 2) { 87 int lookup = ((carryOver << 2) + (b >> 6)) & 63; 88 out.write(chars[lookup]); 89 lookup = b & 63; out.write(chars[lookup]); 91 carryOver = 0; 92 } 93 charCount++; 94 95 if (charCount % 57 == 0) { 97 out.write('\n'); 98 } 99 } 100 101 110 public void write(byte[] buf, int off, int len) throws IOException { 111 for (int i = 0; i < len; i++) { 113 write(buf[off + i]); 114 } 115 } 116 117 123 public void close() throws IOException { 124 if (charCount % 3 == 1) { int lookup = (carryOver << 4) & 63; 127 out.write(chars[lookup]); 128 out.write('='); 129 out.write('='); 130 } 131 else if (charCount % 3 == 2) { int lookup = (carryOver << 2) & 63; 133 out.write(chars[lookup]); 134 out.write('='); 135 } 136 super.close(); 137 } 138 139 148 public static String encode(String unencoded) { 149 byte[] bytes = null; 150 try { 151 bytes = unencoded.getBytes("8859_1"); 152 } 153 catch (UnsupportedEncodingException ignored) { } 154 return encode(bytes); 155 } 156 157 163 public static String encode(byte[] bytes) { 164 ByteArrayOutputStream out = 165 new ByteArrayOutputStream((int) (bytes.length * 1.37)); 166 Base64Encoder encodedOut = new Base64Encoder(out); 167 168 try { 169 encodedOut.write(bytes); 170 encodedOut.close(); 171 172 return out.toString("8859_1"); 173 } 174 catch (IOException ignored) { return null; } 175 } 176 177 public static void main(String [] args) throws Exception { 178 if (args.length != 1) { 179 System.err.println( 180 "Usage: java com.oreilly.servlet.Base64Encoder fileToEncode"); 181 return; 182 } 183 184 Base64Encoder encoder = null; 185 BufferedInputStream in = null; 186 try { 187 encoder = new Base64Encoder(System.out); 188 in = new BufferedInputStream(new FileInputStream(args[0])); 189 190 byte[] buf = new byte[4 * 1024]; int bytesRead; 192 while ((bytesRead = in.read(buf)) != -1) { 193 encoder.write(buf, 0, bytesRead); 194 } 195 } 196 finally { 197 if (in != null) in.close(); 198 if (encoder != null) encoder.close(); 199 } 200 } 201 } 202 | Popular Tags |