1 22 23 24 package org.infozone.tools; 25 26 27 import java.io.ByteArrayOutputStream ; 28 29 30 38 public final class MimeBase64Decoder 39 { 40 41 42 private ByteArrayOutputStream out = new ByteArrayOutputStream (); 43 44 private byte token[] = new byte[4]; private byte bytes[] = new byte[3]; private int token_length = 0; 48 static private final byte NUL = 127; static private final byte EOF = 126; 51 static private final byte map[] = { 52 NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, 62, NUL, NUL, NUL, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, NUL, NUL, NUL, EOF, NUL, NUL, 61 NUL, 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, NUL, NUL, NUL, NUL, NUL, NUL, 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, NUL, NUL, NUL, NUL, NUL, 70 NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, 79 NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, }; 88 89 90 private void decode_token() 93 { 94 int num = ((token[0] << 18) | 95 (token[1] << 12) | 96 (token[2] << 6) | 97 (token[3])); 98 99 bytes[0] = (byte) (0xFF & (num >> 16)); 100 bytes[1] = (byte) (0xFF & (num >> 8)); 101 bytes[2] = (byte) (0xFF & num); 102 103 out.write(bytes,0,3); 104 } 105 106 107 private final void decode_final_token() 111 { 112 113 byte b0 = token[0]; 114 byte b1 = token[1]; 115 byte b2 = token[2]; 116 byte b3 = token[3]; 117 118 int eq_count = 0; 119 120 if (b0 == EOF) { b0 = 0; eq_count++; } 121 if (b1 == EOF) { b1 = 0; eq_count++; } 122 if (b2 == EOF) { b2 = 0; eq_count++; } 123 if (b3 == EOF) { b3 = 0; eq_count++; } 124 125 int num = ((b0 << 18) | (b1 << 12) | (b2 << 6) | (b3)); 126 127 135 out.write((byte) (num >> 16)); if (eq_count <= 1) { 137 out.write((byte) ((num >> 8) & 0xFF)); if (eq_count == 0) { 139 out.write((byte) (num & 0xFF)); } 141 } 142 } 143 144 145 public final void translate(char[] ch) 146 { 147 translate (ch, 0, ch.length); 148 } 149 150 151 public final void translate(char[] ch, int offset, int length) 152 { 153 154 if (token == null) return; 156 157 for (int i = offset; i < offset + length; i++) { 158 byte t = map[(ch[i]&0xff)]; 159 160 if ( t == EOF ) { 161 eof(); 162 } else if (t != NUL) { 163 token[token_length++] = t; 164 } 165 if (token_length == 4) { 166 decode_token(); 167 token_length = 0; 168 } 169 } 170 } 171 172 173 private void eof() 174 { 175 if (token != null && token_length != 0) { 176 while (token_length < 4) 177 token[token_length++] = EOF; 178 decode_final_token(); 179 } 180 token_length = 0; 181 token = new byte[4]; 182 bytes = new byte[3]; 183 } 184 185 186 public byte[] getByteArray() 187 { 188 eof(); 189 return out.toByteArray(); 190 } 191 } 192 | Popular Tags |