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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 384
8f2986ff0235
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

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

mercurial