Here you can find the source of base64Encode(byte[] in)
Parameter | Description |
---|---|
in | The input byte array to convert. |
public static String base64Encode(byte[] in)
//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); } }