1 38 39 40 package com.sun.xml.fastinfoset.algorithm; 41 42 import java.io.EOFException ; 43 import java.io.IOException ; 44 import java.io.InputStream ; 45 import java.io.OutputStream ; 46 import java.nio.CharBuffer ; 47 import java.util.ArrayList ; 48 import java.util.List ; 49 import org.jvnet.fastinfoset.EncodingAlgorithmException; 50 import com.sun.xml.fastinfoset.CommonResourceBundle; 51 52 public class BASE64EncodingAlgorithm extends BuiltInEncodingAlgorithm { 53 54 protected static final char encodeBase64[] = { 55 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 56 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 57 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 58 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 59 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' 60 }; 61 62 protected static final int decodeBase64[] = { 63 62, 64 -1, -1, -1, 65 63, 66 52, 67 53, 68 54, 69 55, 70 56, 71 57, 72 58, 73 59, 74 60, 75 61, 76 -1, -1, -1, -1, -1, -1, -1, 77 0, 78 1, 79 2, 80 3, 81 4, 82 5, 83 6, 84 7, 85 8, 86 9, 87 10, 88 11, 89 12, 90 13, 91 14, 92 15, 93 16, 94 17, 95 18, 96 19, 97 20, 98 21, 99 22, 100 23, 101 24, 102 25, 103 -1, -1, -1, -1, -1, -1, 104 26, 105 27, 106 28, 107 29, 108 30, 109 31, 110 32, 111 33, 112 34, 113 35, 114 36, 115 37, 116 38, 117 39, 118 40, 119 41, 120 42, 121 43, 122 44, 123 45, 124 46, 125 47, 126 48, 127 49, 128 50, 129 51 130 }; 131 132 public final Object decodeFromBytes(byte[] b, int start, int length) throws EncodingAlgorithmException { 133 final byte[] data = new byte[length]; 134 System.arraycopy(b, start, data, 0, length); 135 return data; 136 } 137 138 public final Object decodeFromInputStream(InputStream s) throws IOException { 139 throw new UnsupportedOperationException (CommonResourceBundle.getInstance().getString("message.notImplemented")); 140 } 141 142 143 public void encodeToOutputStream(Object data, OutputStream s) throws IOException { 144 if (!(data instanceof byte[])) { 145 throw new IllegalArgumentException (CommonResourceBundle.getInstance().getString("message.dataNotByteArray")); 146 } 147 148 s.write((byte[])data); 149 } 150 151 public final Object convertFromCharacters(char[] ch, int start, int length) { 152 if (length == 0) { 153 return new byte[0]; 154 } 155 156 StringBuffer encodedValue = removeWhitespace(ch, start, length); 157 int encodedLength = encodedValue.length(); 158 if (encodedLength == 0) { 159 return new byte[0]; 160 } 161 162 int blockCount = encodedLength / 4; 163 int partialBlockLength = 3; 164 165 if (encodedValue.charAt(encodedLength - 1) == '=') { 166 --partialBlockLength; 167 if (encodedValue.charAt(encodedLength - 2) == '=') { 168 --partialBlockLength; 169 } 170 } 171 172 int valueLength = (blockCount - 1) * 3 + partialBlockLength; 173 byte[] value = new byte[valueLength]; 174 175 int idx = 0; 176 int encodedIdx = 0; 177 for (int i = 0; i < blockCount; ++i) { 178 int x1 = decodeBase64[encodedValue.charAt(encodedIdx++) - '+']; 179 int x2 = decodeBase64[encodedValue.charAt(encodedIdx++) - '+']; 180 int x3 = decodeBase64[encodedValue.charAt(encodedIdx++) - '+']; 181 int x4 = decodeBase64[encodedValue.charAt(encodedIdx++) - '+']; 182 183 value[idx++] = (byte) ((x1 << 2) | (x2 >> 4)); 184 if (idx < valueLength) { 185 value[idx++] = (byte) (((x2 & 0x0f) << 4) | (x3 >> 2)); 186 } 187 if (idx < valueLength) { 188 value[idx++] = (byte) (((x3 & 0x03) << 6) | x4); 189 } 190 } 191 192 return value; 193 } 194 195 public final void convertToCharacters(Object data, StringBuffer s) { 196 if (data == null) { 197 return; 198 } 199 final byte[] value = (byte[]) data; 200 if (value.length == 0) { 201 return; 202 } 203 204 final int partialBlockLength = value.length % 3; 205 final int blockCount = (partialBlockLength != 0) ? 206 value.length / 3 + 1 : 207 value.length / 3; 208 209 final int encodedLength = blockCount * 4; 210 s.ensureCapacity(encodedLength); 211 212 int idx = 0; 213 char encodedChar; 214 for (int i = 0; i < blockCount; ++i) { 215 int b1 = value[idx++] & 0xFF; 216 int b2 = (idx < value.length) ? value[idx++] & 0xFF : 0; 217 int b3 = (idx < value.length) ? value[idx++] & 0xFF : 0; 218 219 s.append(encodeBase64[b1 >> 2]); 220 221 s.append(encodeBase64[((b1 & 0x03) << 4) | (b2 >> 4)]); 222 223 s.append(encodeBase64[((b2 & 0x0f) << 2) | (b3 >> 6)]); 224 225 s.append(encodeBase64[b3 & 0x3f]); 226 } 227 228 switch (partialBlockLength) { 229 case 1 : 230 s.setCharAt(encodedLength - 1, '='); 231 s.setCharAt(encodedLength - 2, '='); 232 break; 233 case 2 : 234 s.setCharAt(encodedLength - 1, '='); 235 break; 236 } 237 } 238 239 240 241 public final int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException { 242 return octetLength; 243 } 244 245 public int getOctetLengthFromPrimitiveLength(int primitiveLength) { 246 return primitiveLength; 247 } 248 249 public final void encodeToBytes(Object array, int astart, int alength, byte[] b, int start) { 250 System.arraycopy((byte[])array, astart, b, start, alength); 251 } 252 } 253 | Popular Tags |