Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/misc/BASE64Encoder.java
38829 views
/*1* Copyright (c) 1995, 1997, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.misc;2526import java.io.OutputStream;27import java.io.InputStream;28import java.io.PrintStream;29import java.io.IOException;3031/**32* This class implements a BASE64 Character encoder as specified in RFC1521.33* This RFC is part of the MIME specification as published by the Internet34* Engineering Task Force (IETF). Unlike some other encoding schemes there35* is nothing in this encoding that indicates36* where a buffer starts or ends.37*38* This means that the encoded text will simply start with the first line39* of encoded text and end with the last line of encoded text.40*41* @author Chuck McManis42* @see CharacterEncoder43* @see BASE64Decoder44*/4546public class BASE64Encoder extends CharacterEncoder {4748/** this class encodes three bytes per atom. */49protected int bytesPerAtom() {50return (3);51}5253/**54* this class encodes 57 bytes per line. This results in a maximum55* of 57/3 * 4 or 76 characters per output line. Not counting the56* line termination.57*/58protected int bytesPerLine() {59return (57);60}6162/** This array maps the characters to their 6 bit values */63private final static char pem_array[] = {64// 0 1 2 3 4 5 6 765'A','B','C','D','E','F','G','H', // 066'I','J','K','L','M','N','O','P', // 167'Q','R','S','T','U','V','W','X', // 268'Y','Z','a','b','c','d','e','f', // 369'g','h','i','j','k','l','m','n', // 470'o','p','q','r','s','t','u','v', // 571'w','x','y','z','0','1','2','3', // 672'4','5','6','7','8','9','+','/' // 773};7475/**76* encodeAtom - Take three bytes of input and encode it as 477* printable characters. Note that if the length in len is less78* than three is encodes either one or two '=' signs to indicate79* padding characters.80*/81protected void encodeAtom(OutputStream outStream, byte data[], int offset, int len)82throws IOException {83byte a, b, c;8485if (len == 1) {86a = data[offset];87b = 0;88c = 0;89outStream.write(pem_array[(a >>> 2) & 0x3F]);90outStream.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);91outStream.write('=');92outStream.write('=');93} else if (len == 2) {94a = data[offset];95b = data[offset+1];96c = 0;97outStream.write(pem_array[(a >>> 2) & 0x3F]);98outStream.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);99outStream.write(pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]);100outStream.write('=');101} else {102a = data[offset];103b = data[offset+1];104c = data[offset+2];105outStream.write(pem_array[(a >>> 2) & 0x3F]);106outStream.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);107outStream.write(pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]);108outStream.write(pem_array[c & 0x3F]);109}110}111}112113114