0N/A/*
2362N/A * Copyright (c) 1995, 1997, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage sun.misc;
0N/A
0N/Aimport java.io.OutputStream;
0N/Aimport java.io.InputStream;
0N/Aimport java.io.PrintStream;
0N/Aimport java.io.IOException;
0N/A
0N/A/**
0N/A * This class implements a BASE64 Character encoder as specified in RFC1521.
0N/A * This RFC is part of the MIME specification as published by the Internet
0N/A * Engineering Task Force (IETF). Unlike some other encoding schemes there
0N/A * is nothing in this encoding that indicates
0N/A * where a buffer starts or ends.
0N/A *
0N/A * This means that the encoded text will simply start with the first line
0N/A * of encoded text and end with the last line of encoded text.
0N/A *
0N/A * @author Chuck McManis
0N/A * @see CharacterEncoder
0N/A * @see BASE64Decoder
0N/A */
0N/A
0N/Apublic class BASE64Encoder extends CharacterEncoder {
0N/A
0N/A /** this class encodes three bytes per atom. */
0N/A protected int bytesPerAtom() {
0N/A return (3);
0N/A }
0N/A
0N/A /**
0N/A * this class encodes 57 bytes per line. This results in a maximum
0N/A * of 57/3 * 4 or 76 characters per output line. Not counting the
0N/A * line termination.
0N/A */
0N/A protected int bytesPerLine() {
0N/A return (57);
0N/A }
0N/A
0N/A /** This array maps the characters to their 6 bit values */
0N/A private final static char pem_array[] = {
0N/A // 0 1 2 3 4 5 6 7
0N/A 'A','B','C','D','E','F','G','H', // 0
0N/A 'I','J','K','L','M','N','O','P', // 1
0N/A 'Q','R','S','T','U','V','W','X', // 2
0N/A 'Y','Z','a','b','c','d','e','f', // 3
0N/A 'g','h','i','j','k','l','m','n', // 4
0N/A 'o','p','q','r','s','t','u','v', // 5
0N/A 'w','x','y','z','0','1','2','3', // 6
0N/A '4','5','6','7','8','9','+','/' // 7
0N/A };
0N/A
0N/A /**
0N/A * encodeAtom - Take three bytes of input and encode it as 4
0N/A * printable characters. Note that if the length in len is less
0N/A * than three is encodes either one or two '=' signs to indicate
0N/A * padding characters.
0N/A */
0N/A protected void encodeAtom(OutputStream outStream, byte data[], int offset, int len)
0N/A throws IOException {
0N/A byte a, b, c;
0N/A
0N/A if (len == 1) {
0N/A a = data[offset];
0N/A b = 0;
0N/A c = 0;
0N/A outStream.write(pem_array[(a >>> 2) & 0x3F]);
0N/A outStream.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
0N/A outStream.write('=');
0N/A outStream.write('=');
0N/A } else if (len == 2) {
0N/A a = data[offset];
0N/A b = data[offset+1];
0N/A c = 0;
0N/A outStream.write(pem_array[(a >>> 2) & 0x3F]);
0N/A outStream.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
0N/A outStream.write(pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]);
0N/A outStream.write('=');
0N/A } else {
0N/A a = data[offset];
0N/A b = data[offset+1];
0N/A c = data[offset+2];
0N/A outStream.write(pem_array[(a >>> 2) & 0x3F]);
0N/A outStream.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
0N/A outStream.write(pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]);
0N/A outStream.write(pem_array[c & 0x3F]);
0N/A }
0N/A }
0N/A}