Provides utility methods to Base64 encode data : Base64 « Development Class « Java






Provides utility methods to Base64 encode data

      
/* 
 *
 * 
 * Copyright 2005 Vincent Massol.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * 
 */

import java.io.ByteArrayOutputStream;

/**
 * Provides utility methods to Base64 encode data. This class uses the Base64 encoding as specified
 * in RFC 2045, 6.8. Base64 Content-Transfer-Encoding.
 * 
 * @version $Id: Base64.java 1705 2008-09-02 13:14:55Z adriana $
 */
public final class Base64
{
    // constants --------------------------------------------------------------

    /**
     * The Base64 character set look-up table. This consists of the following ordered alphanumerics:
     * A-Z, a-z, 0-9, + and /.
     */
    private static final char[] ENCODE = new char[64];

    /**
     * The character to pad the output with if not a multiple of 24-bits.
     */
    private static final char PAD_CHAR = '=';

    // static -----------------------------------------------------------------

    static
    {
        // create Base64 character look-up table
        for (int i = 0; i < 26; i++)
        {
            ENCODE[i] = (char) ('A' + i);
            ENCODE[i + 26] = (char) ('a' + i);
        }
        for (int i = 0; i < 10; i++)
        {
            ENCODE[i + 52] = (char) ('0' + i);
        }
        ENCODE[62] = '+';
        ENCODE[63] = '/';
    }

    // constructors -----------------------------------------------------------

    /**
     * Private to prevent unnecessary instantation.
     */
    private Base64()
    {
        // Private to prevent unnecessary instantation
    }

    // public methods ---------------------------------------------------------

    /**
     * Base64 encodes the specified bytes. This method is provided for signature compatibility with
     * commons-codec.
     * 
     * @param bytes the bytes to encode
     * @return the encoded bytes
     */
    public static byte[] encodeBase64(byte[] bytes)
    {
        return encode(bytes);
    }

    /**
     * Base64 encodes the specified string using the platform's default encoding.
     * 
     * @param string the string to encode
     * @return the encoded string
     */
    public static String encode(String string)
    {
        return new String(encode(string.getBytes()));
    }

    /**
     * Base64 encodes the specified bytes.
     * 
     * @param bytes the bytes to encode
     * @return the encoded bytes
     */
    public static byte[] encode(byte[] bytes)
    {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int count = 0;
        int carry = 0;

        for (int i = 0; i < bytes.length; i++)
        {
            byte b = bytes[i];

            switch (count++ % 3)
            {
                // first byte of 24-bits: write 6-bits and carry 2-bits
                case 0:
                    out.write(ENCODE[b >> 2]);
                    carry = b & 0x03;
                    break;

                // second byte of 24-bits: write carry + 4-bits, carry 4-bits
                case 1:
                    out.write(ENCODE[(carry << 4) + (b >> 4)]);
                    carry = b & 0x0F;
                    break;

                // third byte of 24-bits: write carry + 2-bits, write 6-bits
                case 2:
                    out.write(ENCODE[(carry << 2) + (b >> 6)]);
                    out.write(ENCODE[b & 0x3F]);
                    break;

                default:
                    throw new InternalError();
            }
        }

        switch (count % 3)
        {
            // third byte of 24-bits: 24-bit aligned
            case 0:
                break;

            // first byte of 24-bits: write 4-bit carry and pad 16-bits
            case 1:
                out.write(ENCODE[carry << 4]);
                out.write(PAD_CHAR);
                out.write(PAD_CHAR);
                break;

            // second byte of 24-bits: write 2-bit carry and pad 8-bits
            case 2:
                out.write(ENCODE[carry << 2]);
                out.write(PAD_CHAR);
                break;

            default:
                throw new InternalError();
        }

        return out.toByteArray();
    }
}

   
    
    
    
    
    
  








Related examples in the same category

1.Base64 encoding/decoding.
2.Decodes Base64 data into octects
3.Implementation of MIME's Base64 encoding and decoding conversions.
4.Encode/decode for RFC 2045 Base64 as defined by RFC 2045
5.Encode/decode for RFC 2045 Base64 as defined by RFC 2045, N. Freed and N. Borenstein.
6.Encodes and decodes to and from Base64 notation.
7.Encodes hex octects into Base64
8.Helper class to provide Base64 encoding routines.
9.Represents a collection of 64 boolean (on/off) flags.
10.byte to be tested if it is Base64 alphabet
11.to Base64
12.One of the fastest implementation of the Base64 encoding. Jakarta and others are slower
13.array of byte to encode
14.Codes number up to radix 62
15.A Base64 Encoder/Decoder
16.A fast and memory efficient class to encode and decode to and from BASE64 in full accordance with RFC 2045
17.BASE64 encoder implementation
18.Base-64 Encoder - translates from base-64 text into binary
19.Base64 Character encoder as specified in RFC1113
20.Base64 Utils
21.Base64 encoder/decoder
22.Base64 from by Funambol, Inc.
23.Convert to hex from byte arrays and back
24.Converting hexadecimal strings
25.Encode and decode data in Base64 format as described in RFC 1521
26.Encode and decode integers, times, and internationalized strings to and from popular binary formats
27.Encoding of raw bytes to base64-encoded characters, and decoding of base64 characters to raw bytes
28.Performs Base64 encoding and/or decoding
29.Provides Base64 encoding and decoding as defined by RFC 2045
30.Provides Base64 encoding and decoding with URL and filename safe alphabet as defined by RFC 3548, section 4.
31.QP Decoder Stream
32.QP Encoder Stream
33.A class to decode Base64 streams and strings.
34.A class to encode Base64 streams and strings.
35.Encodes binary data to plain text as Base64
36.A very fast and memory efficient class to encode and decode to and from BASE64 in full accordance with RFC 2045.
37.Decodes InputStreams which contain binary data in base64 form
38.Base 64 Converter
39.Base64 from org.cspoker.common.util
40.Base64 converted from code at http://iharder.sourceforge.net/base64/
41.Encodes and decodes to and from Base64 notation.
42.Simple Base64 string decoding function
43.Class to represent unsigned 64-bit numbers.
44.A Base64 encoder/decoder.
45.The Base64 utility class implements Base-64 and Base-85 encoding and decoding algorithms.
46.Provides Base64 encoding and decoding
47.Code to read and write Base64-encoded text.
48.Base32 encoding/decoding class.