Java Base64 Encode base64Encode(byte[] in)

Here you can find the source of base64Encode(byte[] in)

Description

BASE64-encodes the specified byte array.

License

Apache License

Parameter

Parameter Description
in The input byte array to convert.

Return

The byte array converted to a BASE-64 encoded string.

Declaration

public static String base64Encode(byte[] in) 

Method Source Code

//package com.java2s;
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *

public class Main {
    private static final char[] base64m1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
            .toCharArray();/*from   w  ww.j ava2  s .  c o m*/

    /**
     * BASE64-encodes the specified byte array.
     *
     * @param in The input byte array to convert.
     * @return The byte array converted to a BASE-64 encoded string.
     */
    public static String base64Encode(byte[] in) {
        int outLength = (in.length * 4 + 2) / 3; // Output length without padding
        char[] out = new char[((in.length + 2) / 3) * 4]; // Length includes padding.
        int iIn = 0;
        int iOut = 0;
        while (iIn < in.length) {
            int i0 = in[iIn++] & 0xff;
            int i1 = iIn < in.length ? in[iIn++] & 0xff : 0;
            int i2 = iIn < in.length ? in[iIn++] & 0xff : 0;
            int o0 = i0 >>> 2;
            int o1 = ((i0 & 3) << 4) | (i1 >>> 4);
            int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);
            int o3 = i2 & 0x3F;
            out[iOut++] = base64m1[o0];
            out[iOut++] = base64m1[o1];
            out[iOut] = iOut < outLength ? base64m1[o2] : '=';
            iOut++;
            out[iOut] = iOut < outLength ? base64m1[o3] : '=';
            iOut++;
        }
        return new String(out);
    }
}

Related

  1. base64Encode(byte[] bytes)
  2. base64encode(byte[] bytes)
  3. base64encode(byte[] data)
  4. base64Encode(byte[] data)
  5. base64Encode(byte[] data)
  6. base64Encode(byte[] in)
  7. base64Encode(byte[] in)
  8. Base64Encode(byte[] input, boolean addLineBreaks)
  9. base64Encode(byte[] param)