1/* Copyright (c) 2002,2003, Stefan Haustein, Oberhausen, Rhld., Germany 2 * 3 * Permission is hereby granted, free of charge, to any person obtaining a copy 4 * of this software and associated documentation files (the "Software"), to deal 5 * in the Software without restriction, including without limitation the rights 6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 * sell copies of the Software, and to permit persons to whom the Software is 8 * furnished to do so, subject to the following conditions: 9 * 10 * The above copyright notice and this permission notice shall be included in 11 * all copies or substantial portions of the Software. 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 * IN THE SOFTWARE. */ 20 21package org.ksoap2.kobjects.base64; 22 23import java.io.*; 24 25public class Base64 { 26 27 static final char[] charTab = 28 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" 29 .toCharArray(); 30 31 public static String encode(byte[] data) { 32 return encode(data, 0, data.length, null).toString(); 33 } 34 35 /** Encodes the part of the given byte array denoted by start and 36 len to the Base64 format. The encoded data is appended to the 37 given StringBuffer. If no StringBuffer is given, a new one is 38 created automatically. The StringBuffer is the return value of 39 this method. */ 40 41 public static StringBuffer encode( 42 byte[] data, 43 int start, 44 int len, 45 StringBuffer buf) { 46 47 if (buf == null) 48 buf = new StringBuffer(data.length * 3 / 2); 49 50 int end = len - 3; 51 int i = start; 52 int n = 0; 53 54 while (i <= end) { 55 int d = 56 ((((int) data[i]) & 0x0ff) << 16) 57 | ((((int) data[i + 1]) & 0x0ff) << 8) 58 | (((int) data[i + 2]) & 0x0ff); 59 60 buf.append(charTab[(d >> 18) & 63]); 61 buf.append(charTab[(d >> 12) & 63]); 62 buf.append(charTab[(d >> 6) & 63]); 63 buf.append(charTab[d & 63]); 64 65 i += 3; 66 67 if (n++ >= 14) { 68 n = 0; 69 buf.append("\r\n"); 70 } 71 } 72 73 if (i == start + len - 2) { 74 int d = 75 ((((int) data[i]) & 0x0ff) << 16) 76 | ((((int) data[i + 1]) & 255) << 8); 77 78 buf.append(charTab[(d >> 18) & 63]); 79 buf.append(charTab[(d >> 12) & 63]); 80 buf.append(charTab[(d >> 6) & 63]); 81 buf.append("="); 82 } 83 else if (i == start + len - 1) { 84 int d = (((int) data[i]) & 0x0ff) << 16; 85 86 buf.append(charTab[(d >> 18) & 63]); 87 buf.append(charTab[(d >> 12) & 63]); 88 buf.append("=="); 89 } 90 91 return buf; 92 } 93 94 static int decode(char c) { 95 if (c >= 'A' && c <= 'Z') 96 return ((int) c) - 65; 97 else if (c >= 'a' && c <= 'z') 98 return ((int) c) - 97 + 26; 99 else if (c >= '0' && c <= '9') 100 return ((int) c) - 48 + 26 + 26; 101 else 102 switch (c) { 103 case '+': 104 return 62; 105 case '/': 106 return 63; 107 case '=': 108 return 0; 109 default: 110 throw new RuntimeException( 111 "unexpected code: " + c); 112 } 113 } 114 115 /** Decodes the given Base64 encoded String to a new byte array. 116 The byte array holding the decoded data is returned. */ 117 118 public static byte[] decode(String s) { 119 120 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 121 try { 122 decode(s, bos); 123 } catch (IOException e) { 124 throw new RuntimeException(); 125 } 126 return bos.toByteArray(); 127 } 128 129 public static void decode(String s, OutputStream os) 130 throws IOException { 131 int i = 0; 132 133 int len = s.length(); 134 135 while (true) { 136 while (i < len && s.charAt(i) <= ' ') 137 i++; 138 139 if (i == len) 140 break; 141 142 int tri = 143 (decode(s.charAt(i)) << 18) 144 + (decode(s.charAt(i + 1)) << 12) 145 + (decode(s.charAt(i + 2)) << 6) 146 + (decode(s.charAt(i + 3))); 147 148 os.write((tri >> 16) & 255); 149 if (s.charAt(i + 2) == '=') 150 break; 151 os.write((tri >> 8) & 255); 152 if (s.charAt(i + 3) == '=') 153 break; 154 os.write(tri & 255); 155 156 i += 4; 157 } 158 } 159} 160