aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.org.jvnet.staxex; aoqi@0: aoqi@0: /** aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: class Base64Encoder { aoqi@0: private static final char[] encodeMap = initEncodeMap(); aoqi@0: aoqi@0: private static char[] initEncodeMap() { aoqi@0: char[] map = new char[64]; aoqi@0: int i; aoqi@0: for( i= 0; i<26; i++ ) map[i] = (char)('A'+i); aoqi@0: for( i=26; i<52; i++ ) map[i] = (char)('a'+(i-26)); aoqi@0: for( i=52; i<62; i++ ) map[i] = (char)('0'+(i-52)); aoqi@0: map[62] = '+'; aoqi@0: map[63] = '/'; aoqi@0: aoqi@0: return map; aoqi@0: } aoqi@0: aoqi@0: public static char encode( int i ) { aoqi@0: return encodeMap[i&0x3F]; aoqi@0: } aoqi@0: aoqi@0: public static byte encodeByte( int i ) { aoqi@0: return (byte)encodeMap[i&0x3F]; aoqi@0: } aoqi@0: aoqi@0: public static String print(byte[] input, int offset, int len) { aoqi@0: char[] buf = new char[((len+2)/3)*4]; aoqi@0: int ptr = print(input,offset,len,buf,0); aoqi@0: assert ptr==buf.length; aoqi@0: return new String(buf); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Encodes a byte array into a char array by doing base64 encoding. aoqi@0: * aoqi@0: * The caller must supply a big enough buffer. aoqi@0: * aoqi@0: * @return aoqi@0: * the value of {@code ptr+((len+2)/3)*4}, which is the new offset aoqi@0: * in the output buffer where the further bytes should be placed. aoqi@0: */ aoqi@0: public static int print(byte[] input, int offset, int len, char[] buf, int ptr) { aoqi@0: for( int i=offset; i>2); aoqi@0: buf[ptr++] = encode(((input[i])&0x3)<<4); aoqi@0: buf[ptr++] = '='; aoqi@0: buf[ptr++] = '='; aoqi@0: break; aoqi@0: case 2: aoqi@0: buf[ptr++] = encode(input[i]>>2); aoqi@0: buf[ptr++] = encode( aoqi@0: ((input[i]&0x3)<<4) | aoqi@0: ((input[i+1]>>4)&0xF)); aoqi@0: buf[ptr++] = encode((input[i+1]&0xF)<<2); aoqi@0: buf[ptr++] = '='; aoqi@0: break; aoqi@0: default: aoqi@0: buf[ptr++] = encode(input[i]>>2); aoqi@0: buf[ptr++] = encode( aoqi@0: ((input[i]&0x3)<<4) | aoqi@0: ((input[i+1]>>4)&0xF)); aoqi@0: buf[ptr++] = encode( aoqi@0: ((input[i+1]&0xF)<<2)| aoqi@0: ((input[i+2]>>6)&0x3)); aoqi@0: buf[ptr++] = encode(input[i+2]&0x3F); aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: return ptr; aoqi@0: } aoqi@0: }