1 36 package org.columba.ristretto.coder; 37 38 import java.nio.ByteBuffer ; 39 import java.nio.charset.Charset ; 40 41 53 public class Base64 { 54 private static final char[] etable = { 55 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 56 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 57 85, 86, 87, 88, 89, 90, 97, 98, 99,100, 58 101,102,103,104,105,106,107,108,109,110, 59 111,112,113,114,115,116,117,118,119,120, 60 121,122, 48, 49, 50, 51, 52, 53, 54, 55, 61 56, 57, 43, 47 }; 62 63 64 66 private static byte[] dtable = 67 { 68 000, 69 000, 70 000, 71 000, 72 000, 73 000, 74 000, 75 000, 76 000, 77 000, 78 000, 79 000, 80 000, 81 000, 82 000, 83 000, 84 000, 85 000, 86 000, 87 000, 88 000, 89 000, 90 000, 91 000, 92 000, 93 000, 94 000, 95 000, 96 000, 97 000, 98 000, 99 000, 100 000, 101 000, 102 000, 103 000, 104 000, 105 000, 106 000, 107 000, 108 000, 109 000, 110 000, 111 62, 112 000, 113 000, 114 000, 115 63, 116 52, 117 53, 118 54, 55, 56, 57, 58, 59, 60, 61, 000, 000, 000, 0, 000, 000, 000, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 000, 000, 000, 000, 000, 000, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 000, 000, 000, 000, 000 }; 128 135 public static ByteBuffer decode(CharSequence input) { 136 int size = (int) (input.length() * .75); 138 139 byte[] outBytes = new byte[size]; 140 byte[] pack = new byte[4]; 141 int outPos = 0; 142 int packSize = 0; 143 int pads = 0; 144 char current; 145 146 for (int i = 0; i < input.length(); i++) { 148 current = input.charAt(i); 149 if (current != '\r' && current != '\n') { 150 pack[packSize++] = (byte) current; 151 if (current == '=') { 152 pads++; 153 } 154 } 155 if (packSize == 4) { 156 outBytes[outPos++] = 157 (byte) ((dtable[pack[0]] << 2) | (dtable[pack[1]] >> 4)); 158 if (pads < 2) 159 outBytes[outPos++] = 160 (byte) ((dtable[pack[1]] << 4) | (dtable[pack[2]] >> 2)); 161 if (pads < 1) 162 outBytes[outPos++] = 163 (byte) ((dtable[pack[2]] << 6) | (dtable[pack[3]])); 164 packSize = 0; 165 if( pads != 0) break; 166 } 167 } 168 169 return ByteBuffer.wrap(outBytes,0,outPos); 171 } 172 173 179 public static byte[] decodeToArray(CharSequence input) { 180 ByteBuffer buffer = Base64.decode(input); 181 if( buffer.limit() == buffer.capacity()) return buffer.array(); 182 183 byte[] result = new byte[buffer.limit()]; 185 System.arraycopy(buffer.array(),0,result,0,buffer.limit()); 186 return result; 187 } 188 189 198 public static StringBuffer encode( ByteBuffer input, boolean wrap ) { 199 int lastPackSize = input.limit() % 3; 200 int estimatedEncodedSize = ((int) (input.limit() * 1.333 + .5)) +2; 201 StringBuffer result = new StringBuffer (estimatedEncodedSize + (estimatedEncodedSize / 76) * 2); 202 int packsPerLine = 0; 203 int i; 204 205 for( i=0; i<(input.limit() - lastPackSize); i+=3) { 207 result.append( etable[(byte)(0x03F & (input.get(i)>>2))] ); 208 result.append( etable[(byte)((0x03F & (input.get(i)<<4)) | (0x00F & (input.get(i+1)>>4)))]); 209 result.append( etable[(byte)((0x03F & (input.get(i+1)<<2)) | (0x003 & (input.get(i+2)>>6)))]); 210 result.append( etable[(byte)(0x03F & input.get(i+2))]); 211 212 packsPerLine++; 214 if( packsPerLine == 25 && wrap) { 215 result.append("\r\n"); 216 packsPerLine = 0; 217 } 218 } 219 220 if( lastPackSize == 2 ) { 222 result.append( etable[(byte)(0x03F & (input.get(i)>>2))] ); 223 result.append( etable[(byte)((0x03F & (input.get(i)<<4)) | (0x00F & (input.get(i+1)>>4)))]); 224 result.append( etable[(byte)(0x03F & (input.get(i+1)<<2))] ); 225 result.append( '='); 226 } 227 228 if( lastPackSize == 1 ) { 229 result.append( etable[(byte)(0x03F & (input.get(i)>>2))] ); 230 result.append( etable[(byte)(0x03F & (input.get(i)<<4))]); 231 result.append( '=' ); 232 result.append( '='); 233 } 234 235 return result; 236 } 237 238 247 public static StringBuffer encode( String input ) { 248 Charset charset = Charset.forName("US-ASCII"); 249 ByteBuffer bytes = charset.encode(input); 250 return encode( bytes); 251 } 252 253 262 public static StringBuffer encode( ByteBuffer buffer ) { 263 return encode( buffer, true ); 264 } 265 266 } 267 | Popular Tags |