Thu, 31 Aug 2017 15:18:52 +0800
merge
1 /*
2 * Copyright (c) 1997, 2012, 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 */
26 package com.sun.xml.internal.org.jvnet.staxex;
28 /**
29 * @author Kohsuke Kawaguchi
30 */
31 class Base64Encoder {
32 private static final char[] encodeMap = initEncodeMap();
34 private static char[] initEncodeMap() {
35 char[] map = new char[64];
36 int i;
37 for( i= 0; i<26; i++ ) map[i] = (char)('A'+i);
38 for( i=26; i<52; i++ ) map[i] = (char)('a'+(i-26));
39 for( i=52; i<62; i++ ) map[i] = (char)('0'+(i-52));
40 map[62] = '+';
41 map[63] = '/';
43 return map;
44 }
46 public static char encode( int i ) {
47 return encodeMap[i&0x3F];
48 }
50 public static byte encodeByte( int i ) {
51 return (byte)encodeMap[i&0x3F];
52 }
54 public static String print(byte[] input, int offset, int len) {
55 char[] buf = new char[((len+2)/3)*4];
56 int ptr = print(input,offset,len,buf,0);
57 assert ptr==buf.length;
58 return new String(buf);
59 }
61 /**
62 * Encodes a byte array into a char array by doing base64 encoding.
63 *
64 * The caller must supply a big enough buffer.
65 *
66 * @return
67 * the value of {@code ptr+((len+2)/3)*4}, which is the new offset
68 * in the output buffer where the further bytes should be placed.
69 */
70 public static int print(byte[] input, int offset, int len, char[] buf, int ptr) {
71 for( int i=offset; i<len; i+=3 ) {
72 switch( len-i ) {
73 case 1:
74 buf[ptr++] = encode(input[i]>>2);
75 buf[ptr++] = encode(((input[i])&0x3)<<4);
76 buf[ptr++] = '=';
77 buf[ptr++] = '=';
78 break;
79 case 2:
80 buf[ptr++] = encode(input[i]>>2);
81 buf[ptr++] = encode(
82 ((input[i]&0x3)<<4) |
83 ((input[i+1]>>4)&0xF));
84 buf[ptr++] = encode((input[i+1]&0xF)<<2);
85 buf[ptr++] = '=';
86 break;
87 default:
88 buf[ptr++] = encode(input[i]>>2);
89 buf[ptr++] = encode(
90 ((input[i]&0x3)<<4) |
91 ((input[i+1]>>4)&0xF));
92 buf[ptr++] = encode(
93 ((input[i+1]&0xF)<<2)|
94 ((input[i+2]>>6)&0x3));
95 buf[ptr++] = encode(input[i+2]&0x3F);
96 break;
97 }
98 }
99 return ptr;
100 }
101 }