Java Base64 Decode base64Decode(String s)

Here you can find the source of base64Decode(String s)

Description

Traduce un String en Base64 a un Arreglo de bytes

License

Apache License

Parameter

Parameter Description
s el String en Base64 (not null)

Return

el arreglo de bytes (not null)

Declaration

public static byte[] base64Decode(String s) 

Method Source Code

//package com.java2s;
/*//from  w  w w. j  a  v a 2 s  .c o  m
 * Copyright 2012 GBSYS.
 *
 * 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.
 */

public class Main {
    private static int[] toInt = new int[128];

    /**
     * Traduce un String en Base64 a un Arreglo de bytes
     *
     * @param s el String en Base64 (not null)
     * @return el arreglo de bytes (not null)
     * @author Herman Barrantes
     * @since 2012-05-10
     */
    public static byte[] base64Decode(String s) {
        int delta = s.endsWith("==") ? 2 : s.endsWith("=") ? 1 : 0;
        byte[] buffer = new byte[s.length() * 3 / 4 - delta];
        int mask = 0xFF;
        int index = 0;
        for (int i = 0; i < s.length(); i += 4) {
            int c0 = toInt[s.charAt(i)];
            int c1 = toInt[s.charAt(i + 1)];
            buffer[index++] = (byte) (((c0 << 2) | (c1 >> 4)) & mask);
            if (index >= buffer.length) {
                return buffer;
            }
            int c2 = toInt[s.charAt(i + 2)];
            buffer[index++] = (byte) (((c1 << 4) | (c2 >> 2)) & mask);
            if (index >= buffer.length) {
                return buffer;
            }
            int c3 = toInt[s.charAt(i + 3)];
            buffer[index++] = (byte) (((c2 << 6) | c3) & mask);
        }
        return buffer;
    }
}

Related

  1. base64Decode(String _s, String _enc)
  2. base64Decode(String b64)
  3. base64Decode(String base64Str)
  4. base64Decode(String data)
  5. base64Decode(String input)
  6. base64decode(String s)
  7. base64Decode(String s)
  8. base64Decode(String s)
  9. Base64Decode(String str)