Java Base64 Decode decodeBase64(InputStream is, OutputStream os)

Here you can find the source of decodeBase64(InputStream is, OutputStream os)

Description

decode Base

License

Apache License

Declaration

public static final int decodeBase64(InputStream is, OutputStream os) throws IOException 

Method Source Code


//package com.java2s;
/*/* w  w w. j  a v  a  2  s . c om*/
   Copyright 2007-2013 Hendrik Iben, University Bremen
    
   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.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    public static final int decodeBase64(InputStream is, OutputStream os) throws IOException {
        int written = 0;
        byte[] buffer = { '=', '=', '=', '=' };
        int bufferIndex = 0;
        byte[] tmpBuffer = { 0, 0, 0, 0 };
        int r, i, outWrite, value;
        byte[] outBuffer = { 0, 0, 0 };
        while (is.available() > 0) {
            r = is.read(tmpBuffer);
            if (r > 0) {
                for (i = 0; i < r; i++) {
                    if (Character.isISOControl(tmpBuffer[i]))
                        continue;
                    if (!isBase64Char(tmpBuffer[i]))
                        throw new IOException("Invalid data in Base64 stream!");
                    buffer[bufferIndex++] = tmpBuffer[i];

                    if (bufferIndex == 4) {
                        value = base64CharToValue(buffer[0]) << 18;
                        value |= base64CharToValue(buffer[1]) << 12;
                        value |= base64CharToValue(buffer[2]) << 6;
                        value |= base64CharToValue(buffer[3]);

                        outBuffer[0] = (byte) ((value >> 16) & 0xFF);
                        outBuffer[1] = (byte) ((value >> 8) & 0xFF);
                        outBuffer[2] = (byte) (value & 0xFF);

                        outWrite = 3;
                        if (buffer[3] == '=')
                            outWrite--;
                        if (buffer[2] == '=')
                            outWrite--;

                        os.write(outBuffer, 0, outWrite);
                        written += outWrite;

                        bufferIndex = 0;
                    }
                }
            }
        }

        return written;
    }

    public static final boolean isBase64Char(byte c) {
        if (c >= 'A' && c <= 'Z')
            return true;
        if (c >= 'a' && c <= 'z')
            return true;
        if (c >= '0' && c <= '9')
            return true;
        if (c == '+')
            return true;
        if (c == '/')
            return true;
        if (c == '=')
            return true;

        return false;
    }

    public static int base64CharToValue(byte c) {
        if (c >= 'A' && c <= 'Z') {
            return c - 'A';
        }
        if (c >= 'a' && c <= 'z') {
            return 26 + (c - 'a');
        }
        if (c >= '0' && c <= '9') {
            return 52 + (c - '0');
        }
        if (c == '+')
            return 62;
        if (c == '/')
            return 63;
        if (c == '=')
            return 0;

        return -1;
    }
}

Related

  1. base64decodeString(String inputString)
  2. decode(String base64)
  3. decode(String base64Code)
  4. decodeBase64(char[] data)
  5. decodeBASE64(InputStream in, OutputStream out)
  6. decodeBase64(String base64Data)
  7. decodeBase64(String data)
  8. decodeBase64(String data)
  9. decodeBASE64(String pEncoded)