Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | * |
aoqi@0 | 25 | * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. |
aoqi@0 | 26 | */ |
aoqi@0 | 27 | |
aoqi@0 | 28 | package com.sun.xml.internal.fastinfoset.algorithm; |
aoqi@0 | 29 | |
aoqi@0 | 30 | import java.io.IOException; |
aoqi@0 | 31 | import java.io.InputStream; |
aoqi@0 | 32 | import java.io.OutputStream; |
aoqi@0 | 33 | import com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithmException; |
aoqi@0 | 34 | import com.sun.xml.internal.fastinfoset.CommonResourceBundle; |
aoqi@0 | 35 | |
aoqi@0 | 36 | public class BASE64EncodingAlgorithm extends BuiltInEncodingAlgorithm { |
aoqi@0 | 37 | |
aoqi@0 | 38 | /* package */ static final char encodeBase64[] = { |
aoqi@0 | 39 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |
aoqi@0 | 40 | 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
aoqi@0 | 41 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', |
aoqi@0 | 42 | 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', |
aoqi@0 | 43 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' |
aoqi@0 | 44 | }; |
aoqi@0 | 45 | |
aoqi@0 | 46 | /* package */ static final int decodeBase64[] = { |
aoqi@0 | 47 | /*'+'*/ 62, |
aoqi@0 | 48 | -1, -1, -1, |
aoqi@0 | 49 | /*'/'*/ 63, |
aoqi@0 | 50 | /*'0'*/ 52, |
aoqi@0 | 51 | /*'1'*/ 53, |
aoqi@0 | 52 | /*'2'*/ 54, |
aoqi@0 | 53 | /*'3'*/ 55, |
aoqi@0 | 54 | /*'4'*/ 56, |
aoqi@0 | 55 | /*'5'*/ 57, |
aoqi@0 | 56 | /*'6'*/ 58, |
aoqi@0 | 57 | /*'7'*/ 59, |
aoqi@0 | 58 | /*'8'*/ 60, |
aoqi@0 | 59 | /*'9'*/ 61, |
aoqi@0 | 60 | -1, -1, -1, -1, -1, -1, -1, |
aoqi@0 | 61 | /*'A'*/ 0, |
aoqi@0 | 62 | /*'B'*/ 1, |
aoqi@0 | 63 | /*'C'*/ 2, |
aoqi@0 | 64 | /*'D'*/ 3, |
aoqi@0 | 65 | /*'E'*/ 4, |
aoqi@0 | 66 | /*'F'*/ 5, |
aoqi@0 | 67 | /*'G'*/ 6, |
aoqi@0 | 68 | /*'H'*/ 7, |
aoqi@0 | 69 | /*'I'*/ 8, |
aoqi@0 | 70 | /*'J'*/ 9, |
aoqi@0 | 71 | /*'K'*/ 10, |
aoqi@0 | 72 | /*'L'*/ 11, |
aoqi@0 | 73 | /*'M'*/ 12, |
aoqi@0 | 74 | /*'N'*/ 13, |
aoqi@0 | 75 | /*'O'*/ 14, |
aoqi@0 | 76 | /*'P'*/ 15, |
aoqi@0 | 77 | /*'Q'*/ 16, |
aoqi@0 | 78 | /*'R'*/ 17, |
aoqi@0 | 79 | /*'S'*/ 18, |
aoqi@0 | 80 | /*'T'*/ 19, |
aoqi@0 | 81 | /*'U'*/ 20, |
aoqi@0 | 82 | /*'V'*/ 21, |
aoqi@0 | 83 | /*'W'*/ 22, |
aoqi@0 | 84 | /*'X'*/ 23, |
aoqi@0 | 85 | /*'Y'*/ 24, |
aoqi@0 | 86 | /*'Z'*/ 25, |
aoqi@0 | 87 | -1, -1, -1, -1, -1, -1, |
aoqi@0 | 88 | /*'a'*/ 26, |
aoqi@0 | 89 | /*'b'*/ 27, |
aoqi@0 | 90 | /*'c'*/ 28, |
aoqi@0 | 91 | /*'d'*/ 29, |
aoqi@0 | 92 | /*'e'*/ 30, |
aoqi@0 | 93 | /*'f'*/ 31, |
aoqi@0 | 94 | /*'g'*/ 32, |
aoqi@0 | 95 | /*'h'*/ 33, |
aoqi@0 | 96 | /*'i'*/ 34, |
aoqi@0 | 97 | /*'j'*/ 35, |
aoqi@0 | 98 | /*'k'*/ 36, |
aoqi@0 | 99 | /*'l'*/ 37, |
aoqi@0 | 100 | /*'m'*/ 38, |
aoqi@0 | 101 | /*'n'*/ 39, |
aoqi@0 | 102 | /*'o'*/ 40, |
aoqi@0 | 103 | /*'p'*/ 41, |
aoqi@0 | 104 | /*'q'*/ 42, |
aoqi@0 | 105 | /*'r'*/ 43, |
aoqi@0 | 106 | /*'s'*/ 44, |
aoqi@0 | 107 | /*'t'*/ 45, |
aoqi@0 | 108 | /*'u'*/ 46, |
aoqi@0 | 109 | /*'v'*/ 47, |
aoqi@0 | 110 | /*'w'*/ 48, |
aoqi@0 | 111 | /*'x'*/ 49, |
aoqi@0 | 112 | /*'y'*/ 50, |
aoqi@0 | 113 | /*'z'*/ 51 |
aoqi@0 | 114 | }; |
aoqi@0 | 115 | |
aoqi@0 | 116 | public final Object decodeFromBytes(byte[] b, int start, int length) throws EncodingAlgorithmException { |
aoqi@0 | 117 | final byte[] data = new byte[length]; |
aoqi@0 | 118 | System.arraycopy(b, start, data, 0, length); |
aoqi@0 | 119 | return data; |
aoqi@0 | 120 | } |
aoqi@0 | 121 | |
aoqi@0 | 122 | public final Object decodeFromInputStream(InputStream s) throws IOException { |
aoqi@0 | 123 | throw new UnsupportedOperationException(CommonResourceBundle.getInstance().getString("message.notImplemented")); |
aoqi@0 | 124 | } |
aoqi@0 | 125 | |
aoqi@0 | 126 | |
aoqi@0 | 127 | public void encodeToOutputStream(Object data, OutputStream s) throws IOException { |
aoqi@0 | 128 | if (!(data instanceof byte[])) { |
aoqi@0 | 129 | throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotByteArray")); |
aoqi@0 | 130 | } |
aoqi@0 | 131 | |
aoqi@0 | 132 | s.write((byte[])data); |
aoqi@0 | 133 | } |
aoqi@0 | 134 | |
aoqi@0 | 135 | public final Object convertFromCharacters(char[] ch, int start, int length) { |
aoqi@0 | 136 | if (length == 0) { |
aoqi@0 | 137 | return new byte[0]; |
aoqi@0 | 138 | } |
aoqi@0 | 139 | |
aoqi@0 | 140 | StringBuilder encodedValue = removeWhitespace(ch, start, length); |
aoqi@0 | 141 | int encodedLength = encodedValue.length(); |
aoqi@0 | 142 | if (encodedLength == 0) { |
aoqi@0 | 143 | return new byte[0]; |
aoqi@0 | 144 | } |
aoqi@0 | 145 | |
aoqi@0 | 146 | int blockCount = encodedLength / 4; |
aoqi@0 | 147 | int partialBlockLength = 3; |
aoqi@0 | 148 | |
aoqi@0 | 149 | if (encodedValue.charAt(encodedLength - 1) == '=') { |
aoqi@0 | 150 | --partialBlockLength; |
aoqi@0 | 151 | if (encodedValue.charAt(encodedLength - 2) == '=') { |
aoqi@0 | 152 | --partialBlockLength; |
aoqi@0 | 153 | } |
aoqi@0 | 154 | } |
aoqi@0 | 155 | |
aoqi@0 | 156 | int valueLength = (blockCount - 1) * 3 + partialBlockLength; |
aoqi@0 | 157 | byte[] value = new byte[valueLength]; |
aoqi@0 | 158 | |
aoqi@0 | 159 | int idx = 0; |
aoqi@0 | 160 | int encodedIdx = 0; |
aoqi@0 | 161 | for (int i = 0; i < blockCount; ++i) { |
aoqi@0 | 162 | int x1 = decodeBase64[encodedValue.charAt(encodedIdx++) - '+']; |
aoqi@0 | 163 | int x2 = decodeBase64[encodedValue.charAt(encodedIdx++) - '+']; |
aoqi@0 | 164 | int x3 = decodeBase64[encodedValue.charAt(encodedIdx++) - '+']; |
aoqi@0 | 165 | int x4 = decodeBase64[encodedValue.charAt(encodedIdx++) - '+']; |
aoqi@0 | 166 | |
aoqi@0 | 167 | value[idx++] = (byte) ((x1 << 2) | (x2 >> 4)); |
aoqi@0 | 168 | if (idx < valueLength) { |
aoqi@0 | 169 | value[idx++] = (byte) (((x2 & 0x0f) << 4) | (x3 >> 2)); |
aoqi@0 | 170 | } |
aoqi@0 | 171 | if (idx < valueLength) { |
aoqi@0 | 172 | value[idx++] = (byte) (((x3 & 0x03) << 6) | x4); |
aoqi@0 | 173 | } |
aoqi@0 | 174 | } |
aoqi@0 | 175 | |
aoqi@0 | 176 | return value; |
aoqi@0 | 177 | } |
aoqi@0 | 178 | |
aoqi@0 | 179 | public final void convertToCharacters(Object data, StringBuffer s) { |
aoqi@0 | 180 | if (data == null) { |
aoqi@0 | 181 | return; |
aoqi@0 | 182 | } |
aoqi@0 | 183 | final byte[] value = (byte[]) data; |
aoqi@0 | 184 | |
aoqi@0 | 185 | convertToCharacters(value, 0, value.length, s); |
aoqi@0 | 186 | } |
aoqi@0 | 187 | |
aoqi@0 | 188 | public final int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException { |
aoqi@0 | 189 | return octetLength; |
aoqi@0 | 190 | } |
aoqi@0 | 191 | |
aoqi@0 | 192 | public int getOctetLengthFromPrimitiveLength(int primitiveLength) { |
aoqi@0 | 193 | return primitiveLength; |
aoqi@0 | 194 | } |
aoqi@0 | 195 | |
aoqi@0 | 196 | public final void encodeToBytes(Object array, int astart, int alength, byte[] b, int start) { |
aoqi@0 | 197 | System.arraycopy((byte[])array, astart, b, start, alength); |
aoqi@0 | 198 | } |
aoqi@0 | 199 | |
aoqi@0 | 200 | public final void convertToCharacters(byte[] data, int offset, int length, StringBuffer s) { |
aoqi@0 | 201 | if (data == null) { |
aoqi@0 | 202 | return; |
aoqi@0 | 203 | } |
aoqi@0 | 204 | final byte[] value = data; |
aoqi@0 | 205 | if (length == 0) { |
aoqi@0 | 206 | return; |
aoqi@0 | 207 | } |
aoqi@0 | 208 | |
aoqi@0 | 209 | final int partialBlockLength = length % 3; |
aoqi@0 | 210 | final int blockCount = (partialBlockLength != 0) ? |
aoqi@0 | 211 | length / 3 + 1 : |
aoqi@0 | 212 | length / 3; |
aoqi@0 | 213 | |
aoqi@0 | 214 | final int encodedLength = blockCount * 4; |
aoqi@0 | 215 | final int originalBufferSize = s.length(); |
aoqi@0 | 216 | s.ensureCapacity(encodedLength + originalBufferSize); |
aoqi@0 | 217 | |
aoqi@0 | 218 | int idx = offset; |
aoqi@0 | 219 | int lastIdx = offset + length; |
aoqi@0 | 220 | for (int i = 0; i < blockCount; ++i) { |
aoqi@0 | 221 | int b1 = value[idx++] & 0xFF; |
aoqi@0 | 222 | int b2 = (idx < lastIdx) ? value[idx++] & 0xFF : 0; |
aoqi@0 | 223 | int b3 = (idx < lastIdx) ? value[idx++] & 0xFF : 0; |
aoqi@0 | 224 | |
aoqi@0 | 225 | s.append(encodeBase64[b1 >> 2]); |
aoqi@0 | 226 | |
aoqi@0 | 227 | s.append(encodeBase64[((b1 & 0x03) << 4) | (b2 >> 4)]); |
aoqi@0 | 228 | |
aoqi@0 | 229 | s.append(encodeBase64[((b2 & 0x0f) << 2) | (b3 >> 6)]); |
aoqi@0 | 230 | |
aoqi@0 | 231 | s.append(encodeBase64[b3 & 0x3f]); |
aoqi@0 | 232 | } |
aoqi@0 | 233 | |
aoqi@0 | 234 | switch (partialBlockLength) { |
aoqi@0 | 235 | case 1 : |
aoqi@0 | 236 | s.setCharAt(originalBufferSize + encodedLength - 1, '='); |
aoqi@0 | 237 | s.setCharAt(originalBufferSize + encodedLength - 2, '='); |
aoqi@0 | 238 | break; |
aoqi@0 | 239 | case 2 : |
aoqi@0 | 240 | s.setCharAt(originalBufferSize + encodedLength - 1, '='); |
aoqi@0 | 241 | break; |
aoqi@0 | 242 | } |
aoqi@0 | 243 | } |
aoqi@0 | 244 | } |