View Javadoc
1 /*** 2 * Copyright (c) 2002, CodeStreet LLC. All rights reserved.<p> 3 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following 4 * conditions are met:<p> 5 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 6 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer 7 * in the documentation and/or other materials provided with the distribution. Neither the name of CodeStreet LLC. nor the 8 * names of its contributors may be used to endorse or promote products derived from this software without specific prior written 9 * permission.<p> 10 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 11 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 12 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 13 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 14 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 15 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<p> 16 */ 17 18 package com.codestreet.messageforge; 19 20 import java.io.IOException; 21 import java.io.OutputStream; 22 import java.io.Writer; 23 24 /*** 25 * Base64 encoder/decoder. 26 * @author Jawaid Hakim. 27 */ 28 public abstract class RBase64 29 { 30 /*** 31 * Decode a Base64 encoded <tt>String</tt>. 32 * @param string Base64 encoded <tt>String</tt>. 33 * @return Byte array. 34 */ 35 public static byte[] decode(String string) 36 { 37 char[] cs = new char[4]; 38 int i = 0; 39 byte[] is = new byte[string.length() / 4 * 3 + 3]; 40 int i_0_ = 0; 41 for (int i_1_ = 0; i_1_ < string.length(); i_1_++) 42 { 43 char c = string.charAt(i_1_); 44 if (c == 61 || c < S_DECODETABLE.length && S_DECODETABLE[c] != 127) 45 { 46 cs[i++] = c; 47 if (i == cs.length) 48 { 49 i = 0; 50 i_0_ += decode0(cs, is, i_0_); 51 } 52 } 53 } 54 if (i_0_ == is.length) 55 return is; 56 57 byte[] is_2_ = new byte[i_0_]; 58 System.arraycopy(is, 0, is_2_, 0, i_0_); 59 return is_2_; 60 } 61 62 /*** 63 * Decode a Base64 encoded <tt>String</tt> and write to <tt>OutputStream</tt>. 64 * @param string Base64 encoded <tt>String</tt>. 65 * @param os Output stream. 66 */ 67 public static void decode(String string, OutputStream os) throws IOException 68 { 69 char[] cs = new char[4]; 70 int i = 0; 71 byte[] is = new byte[3]; 72 for (int i_3_ = 0; i_3_ < string.length(); i_3_++) 73 { 74 char c = string.charAt(i_3_); 75 if (c == 61 || c < S_DECODETABLE.length && S_DECODETABLE[c] != 127) 76 { 77 cs[i++] = c; 78 if (i == cs.length) 79 { 80 i = 0; 81 int i_4_ = decode0(cs, is, 0); 82 os.write(is, 0, i_4_); 83 } 84 } 85 } 86 } 87 88 /*** 89 * Decode a Base64 encoded <tt>char[]</tt>. 90 * @param cs Base64 encoded chatacter array. 91 * @param offset Offset. 92 * @param len Length. 93 * @return Byte array. 94 */ 95 public static byte[] decode(char[] cs, int offset, int len) 96 { 97 char[] cs_6_ = new char[4]; 98 int i_7_ = 0; 99 byte[] is = new byte[len / 4 * 3 + 3]; 100 int i_8_ = 0; 101 for (int i_9_ = offset; i_9_ < offset + len; i_9_++) 102 { 103 char c = cs[i_9_]; 104 if (c == 61 || c < S_DECODETABLE.length && S_DECODETABLE[c] != 127) 105 { 106 cs_6_[i_7_++] = c; 107 if (i_7_ == cs_6_.length) 108 { 109 i_7_ = 0; 110 i_8_ += decode0(cs_6_, is, i_8_); 111 } 112 } 113 } 114 if (i_8_ == is.length) 115 return is; 116 byte[] is_10_ = new byte[i_8_]; 117 System.arraycopy(is, 0, is_10_, 0, i_8_); 118 return is_10_; 119 } 120 121 public static void decode(char[] cs, int offset, int len, OutputStream os) throws IOException 122 { 123 char[] cs_12_ = new char[4]; 124 int i_13_ = 0; 125 byte[] is = new byte[3]; 126 for (int i_14_ = offset; i_14_ < offset + len; i_14_++) 127 { 128 char c = cs[i_14_]; 129 if (c == 61 || c < S_DECODETABLE.length && S_DECODETABLE[c] != 127) 130 { 131 cs_12_[i_13_++] = c; 132 if (i_13_ == cs_12_.length) 133 { 134 i_13_ = 0; 135 int i_15_ = decode0(cs_12_, is, 0); 136 os.write(is, 0, i_15_); 137 } 138 } 139 } 140 } 141 142 private static int decode0(char[] cs, byte[] is, int i) 143 { 144 int i_16_ = 3; 145 if (cs[3] == '=') 146 i_16_ = 2; 147 if (cs[2] == '=') 148 i_16_ = 1; 149 int i_17_ = S_DECODETABLE[cs[0]]; 150 int i_18_ = S_DECODETABLE[cs[1]]; 151 int i_19_ = S_DECODETABLE[cs[2]]; 152 int i_20_ = S_DECODETABLE[cs[3]]; 153 switch (i_16_) { 154 case 1: 155 is[i] = (byte) (i_17_ << 2 & 0xfc | i_18_ >> 4 & 0x3); 156 return 1; 157 case 2: 158 is[i++] = (byte) (i_17_ << 2 & 0xfc | i_18_ >> 4 & 0x3); 159 is[i] = (byte) (i_18_ << 4 & 0xf0 | i_19_ >> 2 & 0xf); 160 return 2; 161 case 3: 162 is[i++] = (byte) (i_17_ << 2 & 0xfc | i_18_ >> 4 & 0x3); 163 is[i++] = (byte) (i_18_ << 4 & 0xf0 | i_19_ >> 2 & 0xf); 164 is[i] = (byte) (i_19_ << 6 & 0xc0 | i_20_ & 0x3f); 165 return 3; 166 default: 167 throw new RuntimeException("Internal Errror"); 168 } 169 } 170 171 /*** 172 * Encode a byte array as a Base64 encoded <tt>String</tt>. 173 * @param data Byte array. 174 * @return Base64 encoded <tt>String</tt>. 175 */ 176 public static String encode(byte[] data) 177 { 178 return encode(data, 0, data.length); 179 } 180 181 /*** 182 * Encode a byte array as a Base64 encoded <tt>String</tt>. 183 * @param data Byte array. 184 * @param offset Offset. 185 * @param len Length. 186 * @return Base64 encoded <tt>String</tt>. 187 */ 188 public static String encode(byte[] data, int offset, int len) 189 { 190 if (len <= 0) 191 return ""; 192 char[] cs = new char[len / 3 * 4 + 4]; 193 int i_22_ = offset; 194 int i_23_ = 0; 195 int i_24_; 196 for (i_24_ = len - offset; i_24_ >= 3; i_24_ -= 3) 197 { 198 int i_25_ 199 = (((data[i_22_] & 0xff) << 16) + ((data[i_22_ + 1] & 0xff) << 8) 200 + (data[i_22_ + 2] & 0xff)); 201 cs[i_23_++] = S_BASE64CHAR[i_25_ >> 18]; 202 cs[i_23_++] = S_BASE64CHAR[i_25_ >> 12 & 0x3f]; 203 cs[i_23_++] = S_BASE64CHAR[i_25_ >> 6 & 0x3f]; 204 cs[i_23_++] = S_BASE64CHAR[i_25_ & 0x3f]; 205 i_22_ += 3; 206 } 207 if (i_24_ == 1) 208 { 209 int i_26_ = data[i_22_] & 0xff; 210 cs[i_23_++] = S_BASE64CHAR[i_26_ >> 2]; 211 cs[i_23_++] = S_BASE64CHAR[i_26_ << 4 & 0x3f]; 212 cs[i_23_++] = '='; 213 cs[i_23_++] = '='; 214 } 215 else if (i_24_ == 2) 216 { 217 int i_27_ = ((data[i_22_] & 0xff) << 8) + (data[i_22_ + 1] & 0xff); 218 cs[i_23_++] = S_BASE64CHAR[i_27_ >> 10]; 219 cs[i_23_++] = S_BASE64CHAR[i_27_ >> 4 & 0x3f]; 220 cs[i_23_++] = S_BASE64CHAR[i_27_ << 2 & 0x3f]; 221 cs[i_23_++] = '='; 222 } 223 return new String(cs, 0, i_23_); 224 } 225 226 /*** 227 * Encode a byte array as to Base64 to write to an <tt>OutputStream</tt>. 228 * @param data Byte array. 229 * @param offset Offset. 230 * @param len Length. 231 * @param os Output stream. 232 */ 233 public static void encode(byte[] data, int offset, int len, OutputStream os) throws IOException 234 { 235 if (len > 0) 236 { 237 byte[] is_29_ = new byte[4]; 238 int i_30_ = offset; 239 int i_31_; 240 for (i_31_ = len - offset; i_31_ >= 3; i_31_ -= 3) 241 { 242 int i_32_ = (((data[i_30_] & 0xff) << 16) 243 + ((data[i_30_ + 1] & 0xff) << 8) 244 + (data[i_30_ + 2] & 0xff)); 245 is_29_[0] = (byte) S_BASE64CHAR[i_32_ >> 18]; 246 is_29_[1] = (byte) S_BASE64CHAR[i_32_ >> 12 & 0x3f]; 247 is_29_[2] = (byte) S_BASE64CHAR[i_32_ >> 6 & 0x3f]; 248 is_29_[3] = (byte) S_BASE64CHAR[i_32_ & 0x3f]; 249 os.write(is_29_, 0, 4); 250 i_30_ += 3; 251 } 252 if (i_31_ == 1) 253 { 254 int i_33_ = data[i_30_] & 0xff; 255 is_29_[0] = (byte) S_BASE64CHAR[i_33_ >> 2]; 256 is_29_[1] = (byte) S_BASE64CHAR[i_33_ << 4 & 0x3f]; 257 is_29_[2] = (byte) 61; 258 is_29_[3] = (byte) 61; 259 os.write(is_29_, 0, 4); 260 } 261 else if (i_31_ == 2) 262 { 263 int i_34_ = ((data[i_30_] & 0xff) << 8) + (data[i_30_ + 1] & 0xff); 264 is_29_[0] = (byte) S_BASE64CHAR[i_34_ >> 10]; 265 is_29_[1] = (byte) S_BASE64CHAR[i_34_ >> 4 & 0x3f]; 266 is_29_[2] = (byte) S_BASE64CHAR[i_34_ << 2 & 0x3f]; 267 is_29_[3] = (byte) 61; 268 os.write(is_29_, 0, 4); 269 } 270 } 271 } 272 273 /*** 274 * Encode a byte array as to Base64 to write to an <tt>Writer</tt>. 275 * @param data Byte array. 276 * @param offset Offset. 277 * @param len Length. 278 * @param writer Writer. 279 */ 280 public static void encode(byte[] data, int offset, int len, Writer writer) throws IOException 281 { 282 if (len > 0) 283 { 284 char[] cs = new char[4]; 285 int i_36_ = offset; 286 int i_37_ = len - offset; 287 int i_38_ = 0; 288 while (i_37_ >= 3) 289 { 290 int i_39_ = (((data[i_36_] & 0xff) << 16) 291 + ((data[i_36_ + 1] & 0xff) << 8) 292 + (data[i_36_ + 2] & 0xff)); 293 cs[0] = S_BASE64CHAR[i_39_ >> 18]; 294 cs[1] = S_BASE64CHAR[i_39_ >> 12 & 0x3f]; 295 cs[2] = S_BASE64CHAR[i_39_ >> 6 & 0x3f]; 296 cs[3] = S_BASE64CHAR[i_39_ & 0x3f]; 297 writer.write(cs, 0, 4); 298 i_36_ += 3; 299 i_37_ -= 3; 300 i_38_ += 4; 301 if (i_38_ % 76 == 0) 302 writer.write("\n"); 303 } 304 if (i_37_ == 1) 305 { 306 int i_40_ = data[i_36_] & 0xff; 307 cs[0] = S_BASE64CHAR[i_40_ >> 2]; 308 cs[1] = S_BASE64CHAR[i_40_ << 4 & 0x3f]; 309 cs[2] = '='; 310 cs[3] = '='; 311 writer.write(cs, 0, 4); 312 } 313 else if (i_37_ == 2) 314 { 315 int i_41_ = ((data[i_36_] & 0xff) << 8) + (data[i_36_ + 1] & 0xff); 316 cs[0] = S_BASE64CHAR[i_41_ >> 10]; 317 cs[1] = S_BASE64CHAR[i_41_ >> 4 & 0x3f]; 318 cs[2] = S_BASE64CHAR[i_41_ << 2 & 0x3f]; 319 cs[3] = '='; 320 writer.write(cs, 0, 4); 321 } 322 } 323 } 324 325 private static final char[] S_BASE64CHAR 326 = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 327 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 328 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 329 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 330 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' }; 331 //private static final char S_BASE64PAD = '='; 332 private static final byte[] S_DECODETABLE = new byte[128]; 333 334 static 335 { 336 for (int i = 0; i < S_DECODETABLE.length; i++) 337 S_DECODETABLE[i] = (byte) 127; 338 for (int i = 0; i < S_BASE64CHAR.length; i++) 339 S_DECODETABLE[S_BASE64CHAR[i]] = (byte) i; 340 } 341 }

This page was automatically generated by Maven