All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jwat.common.Base64 Maven / Gradle / Ivy

/**
 * Java Web Archive Toolkit - Software to read and validate ARC, WARC
 * and GZip files. (http://jwat.org/)
 * Copyright 2011-2012 Netarkivet.dk (http://netarkivet.dk/)
 *
 * 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.
 */
package org.jwat.common;

import java.io.ByteArrayOutputStream;
import java.util.Arrays;

/**
 * Base64 encoder/decoder implementation based on the specifications in
 * rfc2045/rfc3548.
 *
 * @author nicl
 */
public class Base64 {

    /** Ascii table used to encode. */
    private static String encodeTab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    /** Table used to decode. */
    public static byte[] decodeTab = new byte[ 256 ];

    /** Populate decode table. */
    static {
        Arrays.fill( decodeTab, (byte)0xff );
        for ( int i=0; i> 2 ) & 63;
                        out.append( encodeTab.charAt( cout ) );
                        cout = ( cin << 4 ) & 63;
                        break;
                    case 1:
                        cout |= ( cin >> 4 ) & 63;
                        out.append( encodeTab.charAt( cout ) );
                        cout = ( cin << 2 ) & 63;
                        break;
                    case 2:
                        cout |= ( cin >> 6 ) & 63;
                        out.append( encodeTab.charAt( cout ) );
                        cout = cin & 63;
                        out.append( encodeTab.charAt( cout ) );
                        break;
                    }
                    mod = ( mod + 1 ) % 3;
                }
                else {
                    return null;
                }
            }
            else {
                b = false;
            }
        }

        /*
         * Padding.
         */

        switch( mod ) {
            case 0:
                break;
            case 1:
                out.append( encodeTab.charAt( cout ) );
                out.append( "==" );
                break;
            case 2:
                out.append( encodeTab.charAt( cout ) );
                out.append( "=" );
                break;
        }

        return out.toString();
    }

    /**
     * Encodes a byte array.
     * @param in byte array.
     * @return encoded string or null
     */
    public static String encodeArray(byte[] in) {
        if (in == null) {
            return null;
        }
        if (in.length == 0) {
            return "";
        }

        StringBuffer out = new StringBuffer( 256 );

        boolean b = true;
        int idx = 0;
        int cin;
        int cout = 0;
        int mod = 0;

        /*
         * Loop.
         */

        while ( b ) {
            if ( idx < in.length ) {
                cin = in[ idx++ ] & 255;
                switch ( mod ) {
                    case 0:
                        cout = ( cin >> 2 ) & 63;
                        out.append( encodeTab.charAt( cout ) );
                        cout = ( cin << 4 ) & 63;
                        break;
                    case 1:
                        cout |= ( cin >> 4 ) & 63;
                        out.append( encodeTab.charAt( cout ) );
                        cout = ( cin << 2 ) & 63;
                        break;
                    case 2:
                        cout |= ( cin >> 6 ) & 63;
                        out.append( encodeTab.charAt( cout ) );
                        cout = cin & 63;
                        out.append( encodeTab.charAt( cout ) );
                        break;
                }
                mod = ( mod + 1 ) % 3;
            }
            else {
                b = false;
            }
        }

        /*
         * Padding.
         */

        switch( mod ) {
            case 0:
                break;
            case 1:
                out.append( encodeTab.charAt( cout ) );
                out.append( "==" );
                break;
            case 2:
                out.append( encodeTab.charAt( cout ) );
                out.append( "=" );
                break;
        }

        return out.toString();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy