src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/algorithm/BASE64EncodingAlgorithm.java

Wed, 12 Jun 2013 14:47:09 +0100

author
mkos
date
Wed, 12 Jun 2013 14:47:09 +0100
changeset 384
8f2986ff0235
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8013021: Rebase 8005432 & 8003542 against the latest jdk8/jaxws
8003542: Improve processing of MTOM attachments
8005432: Update access to JAX-WS
Reviewed-by: mullan

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

mercurial