jdk/src/java.base/share/classes/sun/misc/BASE64Encoder.java
changeset 34746 be3fcc9b7a78
parent 34745 995895614a53
parent 34741 6f034bebd711
child 34748 3b2cde99bd99
equal deleted inserted replaced
34745:995895614a53 34746:be3fcc9b7a78
     1 /*
       
     2  * Copyright (c) 1995, 1997, 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 package sun.misc;
       
    26 
       
    27 import java.io.OutputStream;
       
    28 import java.io.InputStream;
       
    29 import java.io.PrintStream;
       
    30 import java.io.IOException;
       
    31 
       
    32 /**
       
    33  * This class implements a BASE64 Character encoder as specified in RFC1521.
       
    34  * This RFC is part of the MIME specification as published by the Internet
       
    35  * Engineering Task Force (IETF). Unlike some other encoding schemes there
       
    36  * is nothing in this encoding that indicates
       
    37  * where a buffer starts or ends.
       
    38  *
       
    39  * This means that the encoded text will simply start with the first line
       
    40  * of encoded text and end with the last line of encoded text.
       
    41  *
       
    42  * @author      Chuck McManis
       
    43  * @see         CharacterEncoder
       
    44  * @see         BASE64Decoder
       
    45  */
       
    46 
       
    47 public class BASE64Encoder extends CharacterEncoder {
       
    48 
       
    49     /** this class encodes three bytes per atom. */
       
    50     protected int bytesPerAtom() {
       
    51         return (3);
       
    52     }
       
    53 
       
    54     /**
       
    55      * this class encodes 57 bytes per line. This results in a maximum
       
    56      * of 57/3 * 4 or 76 characters per output line. Not counting the
       
    57      * line termination.
       
    58      */
       
    59     protected int bytesPerLine() {
       
    60         return (57);
       
    61     }
       
    62 
       
    63     /** This array maps the characters to their 6 bit values */
       
    64     private static final char pem_array[] = {
       
    65         //       0   1   2   3   4   5   6   7
       
    66                 'A','B','C','D','E','F','G','H', // 0
       
    67                 'I','J','K','L','M','N','O','P', // 1
       
    68                 'Q','R','S','T','U','V','W','X', // 2
       
    69                 'Y','Z','a','b','c','d','e','f', // 3
       
    70                 'g','h','i','j','k','l','m','n', // 4
       
    71                 'o','p','q','r','s','t','u','v', // 5
       
    72                 'w','x','y','z','0','1','2','3', // 6
       
    73                 '4','5','6','7','8','9','+','/'  // 7
       
    74         };
       
    75 
       
    76     /**
       
    77      * encodeAtom - Take three bytes of input and encode it as 4
       
    78      * printable characters. Note that if the length in len is less
       
    79      * than three is encodes either one or two '=' signs to indicate
       
    80      * padding characters.
       
    81      */
       
    82     protected void encodeAtom(OutputStream outStream, byte data[], int offset, int len)
       
    83         throws IOException {
       
    84         byte a, b, c;
       
    85 
       
    86         if (len == 1) {
       
    87             a = data[offset];
       
    88             b = 0;
       
    89             c = 0;
       
    90             outStream.write(pem_array[(a >>> 2) & 0x3F]);
       
    91             outStream.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
       
    92             outStream.write('=');
       
    93             outStream.write('=');
       
    94         } else if (len == 2) {
       
    95             a = data[offset];
       
    96             b = data[offset+1];
       
    97             c = 0;
       
    98             outStream.write(pem_array[(a >>> 2) & 0x3F]);
       
    99             outStream.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
       
   100             outStream.write(pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]);
       
   101             outStream.write('=');
       
   102         } else {
       
   103             a = data[offset];
       
   104             b = data[offset+1];
       
   105             c = data[offset+2];
       
   106             outStream.write(pem_array[(a >>> 2) & 0x3F]);
       
   107             outStream.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
       
   108             outStream.write(pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]);
       
   109             outStream.write(pem_array[c & 0x3F]);
       
   110         }
       
   111     }
       
   112 }