Encodes a byte array into a base 64 byte array.
<p>

@param dData byte array to encode.

@return encoded byte array.
767382
public static byte[] encode( byte[] dData )
    {
        if( dData == null )
        {
            throw new IllegalArgumentException( "Cannot encode null" );
        }
        byte[] eData = new byte[( ( dData.length + 2 ) / 3 ) * 4];

        int eIndex = 0;
        for( int i = 0; i < dData.length; i += 3 )
        {
            int d1;
            int d2 = 0;
            int d3 = 0;
            int e1;
            int e2;
            int e3;
            int e4;
            int pad = 0;

            d1 = dData[ i ];
            if( ( i + 1 ) < dData.length )
            {
                d2 = dData[ i + 1 ];
                if( ( i + 2 ) < dData.length )
                {
                    d3 = dData[ i + 2 ];
                }
                else
                {
                    pad = 1;
                }
            }
            else
            {
                pad = 2;
            }

            e1 = ALPHASET[ ( d1 & I6O2 ) >> 2 ];
            e2 = ALPHASET[ ( d1 & O6I2 ) << 4 | ( d2 & I4O4 ) >> 4 ];
            e3 = ALPHASET[ ( d2 & O4I4 ) << 2 | ( d3 & I2O6 ) >> 6 ];
            e4 = ALPHASET[ ( d3 & O2I6 ) ];

            eData[ eIndex++ ] = (byte) e1;
            eData[ eIndex++ ] = (byte) e2;
            eData[ eIndex++ ] = ( pad < 2 ) ? (byte) e3 : (byte) '=';
            eData[ eIndex++ ] = ( pad < 1 ) ? (byte) e4 : (byte) '=';

        }
        return eData;
    }
355000
public static String encode(byte[] bytes) {
    if (null == bytes) {
      return null;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream((int) (bytes.length * 1.37));
    Base64Encoder encodedOut = new Base64Encoder(out);

    try {
      encodedOut.write(bytes);
      encodedOut.close();

      return out.toString("UTF-8");
    } catch (IOException ignored) {
      return null;
    }
  }
120629
@FindbugsSuppressWarnings("DM_DEFAULT_ENCODING")
  public static String encodeBase64(String data) {
    final byte[] encodedBytes = Base64.encodeBase64(data.getBytes());

    return new String(encodedBytes);
  }
913061
@Override
    public int encode ( byte[] dst, int dstIndex ) {
        int start = dstIndex;
        SMBUtil.writeInt8(this.sourceOffset, dst, dstIndex);
        dstIndex += 8;
        SMBUtil.writeInt8(this.targetOffset, dst, dstIndex);
        dstIndex += 8;
        SMBUtil.writeInt4(this.length, dst, dstIndex);
        dstIndex += 4;
        dstIndex += 4; // reserved
        return dstIndex - start;
    }
319850
public CachedData encode(Object o) {
    byte[] b = null;
    int flags = 0;
    if (o instanceof String) {
      b = encodeString((String) o);
      if (StringUtils.isJsonObject((String) o)) {
        return new CachedData(flags, b, getMaxSize());
      }
    } else if (o instanceof Long) {
      b = tu.encodeLong((Long) o);
      flags |= SPECIAL_LONG;
    } else if (o instanceof Integer) {
      b = tu.encodeInt((Integer) o);
      flags |= SPECIAL_INT;
    } else if (o instanceof Boolean) {
      b = tu.encodeBoolean((Boolean) o);
      flags |= SPECIAL_BOOLEAN;
    } else if (o instanceof Date) {
      b = tu.encodeLong(((Date) o).getTime());
      flags |= SPECIAL_DATE;
    } else if (o instanceof Byte) {
      b = tu.encodeByte((Byte) o);
      flags |= SPECIAL_BYTE;
    } else if (o instanceof Float) {
      b = tu.encodeInt(Float.floatToRawIntBits((Float) o));
      flags |= SPECIAL_FLOAT;
    } else if (o instanceof Double) {
      b = tu.encodeLong(Double.doubleToRawLongBits((Double) o));
      flags |= SPECIAL_DOUBLE;
    } else if (o instanceof byte[]) {
      b = (byte[]) o;
      flags |= SPECIAL_BYTEARRAY;
    } else {
      b = serialize(o);
      flags |= SERIALIZED;
    }
    assert b != null;
    if (b.length > compressionThreshold) {
      byte[] compressed = compress(b);
      if (compressed.length < b.length) {
        getLogger().debug("Compressed %s from %d to %d",
            o.getClass().getName(), b.length, compressed.length);
        b = compressed;
        flags |= COMPRESSED;
      } else {
        getLogger().info("Compression increased the size of %s from %d to %d",
            o.getClass().getName(), b.length, compressed.length);
      }
    }
    return new CachedData(flags, b, getMaxSize());
  }
436666
public static String base64Encode(byte[] bytes) {
    if (bytes == null) {
      throw new IllegalArgumentException("Input bytes must not be null.");
    }
    if (bytes.length >= BASE64_UPPER_BOUND) {
      throw new IllegalArgumentException(
          "Input bytes length must not exceed " + BASE64_UPPER_BOUND);
    }

    // Every three bytes is encoded into four characters.
    //
    // Example:
    //           input |0 1 0 1 0 0 1 0|0 1 1 0 1 1 1 1|0 1 1 0 0 0 1 0|
    // encode grouping |0 1 0 1 0 0|1 0 0 1 1 0|1 1 1 1 0 1|1 0 0 0 1 0|
    //   encoded ascii |     U     |     m     |     9     |     i     |

    int triples = bytes.length / 3;

    // If the number of input bytes is not a multiple of three, padding characters will be added.
    if (bytes.length % 3 != 0) {
      triples += 1;
    }

    // The encoded string will have four characters for every three bytes.
    char[] encoding = new char[triples << 2];

    for (int in = 0, out = 0; in < bytes.length; in += 3, out += 4) {
      int triple = (bytes[in] & 0xff) << 16;
      if (in + 1 < bytes.length) {
        triple |= ((bytes[in + 1] & 0xff) << 8);
      }
      if (in + 2 < bytes.length) {
        triple |= (bytes[in + 2] & 0xff);
      }
      encoding[out] = BASE64_CHARS.charAt((triple >> 18) & 0x3f);
      encoding[out + 1] = BASE64_CHARS.charAt((triple >> 12) & 0x3f);
      encoding[out + 2] = BASE64_CHARS.charAt((triple >> 6) & 0x3f);
      encoding[out + 3] = BASE64_CHARS.charAt(triple & 0x3f);
    }

    // Add padding characters if needed.
    for (int i = encoding.length - (triples * 3 - bytes.length); i < encoding.length; i++) {
      encoding[i] = '=';
    }

    return String.valueOf(encoding);
  }
707325
private byte[] maybeDecodeBase64(byte[] byteArray) {
    try {
      String pem = new String(byteArray);
      // Remove required begin/end lines.
      pem = pem.substring(BEGIN_CERT_LINE_LENGTH, pem.length() - END_CERT_LINE_LENGTH);
      return Base64.getDecoder().decode(pem);
    } catch (Exception e) {
      // Not a valid PEM encoded certificate, return original array.
      return byteArray;
    }
  }
1066868
public static byte[] encode(InputStream in) {
        Base64OutputStream out = null;
        try {
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            out = new Base64OutputStream(bytes);
            byte[] buf = new byte[4096];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            out.flush();
            return bytes.toByteArray();
        } catch (IOException e) {
            return null;
        } finally {
            try {
                in.close();
                if (out != null) out.close();
            } catch (IOException e) {
                throw new FaultException(e);
            }
        }
    }
847094
private static byte[] encodeEnternal(byte[] d)
    {
        if (d == null) return null;
        byte data[] = new byte[d.length+2];
        System.arraycopy(d, 0, data, 0, d.length);
        byte dest[] = new byte[(data.length/3)*4];

        // 3-byte to 4-byte conversion
        for (int sidx = 0, didx=0; sidx < d.length; sidx += 3, didx += 4)
        {
            dest[didx]   = (byte) ((data[sidx] >>> 2) & 077);
            dest[didx+1] = (byte) ((data[sidx+1] >>> 4) & 017 |
                    (data[sidx] << 4) & 077);
            dest[didx+2] = (byte) ((data[sidx+2] >>> 6) & 003 |
                    (data[sidx+1] << 2) & 077);
            dest[didx+3] = (byte) (data[sidx+2] & 077);
        }

        // 0-63 to ascii printable conversion
        for (int idx = 0; idx <dest.length; idx++)
        {
            if (dest[idx] < 26)     dest[idx] = (byte)(dest[idx] + 'A');
            else if (dest[idx] < 52)  dest[idx] = (byte)(dest[idx] + 'a' - 26);
            else if (dest[idx] < 62)  dest[idx] = (byte)(dest[idx] + '0' - 52);
            else if (dest[idx] < 63)  dest[idx] = (byte)'+';
            else            dest[idx] = (byte)'/';
        }

        // add padding
        for (int idx = dest.length-1; idx > (d.length*4)/3; idx--)
        {
            dest[idx] = (byte)'=';
        }
        return dest;
    }
448851
public static byte[] encodeLZ(String data, String dictionary) {
    byte[] asBytes = new byte[0];
    try {
      asBytes = data.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException(e);
    }
    return encodeLZ(asBytes, dictionary);
  }
67540
@SuppressWarnings("AssignmentToForLoopParameter")
    public static String encode(final byte[] data) {
        final int len = data.length;
        final StringBuilder result = new StringBuilder((len / 3 + 1) * 4);
        int bte;
        int index;

        for (int i = 0; i < len; i++) {

            bte = data[i];

            // First 6 bits
            index = bte >> 2 & MASK_6;
            result.append(CHAR_TABLE.charAt(index));

            // Last 2 bits plus 4 from next byte
            index = bte << 4 & MASK_6;

            if (++i < len) {
                bte = data[i];
                index |= bte >> 4 & MASK_4;
            }
            result.append(CHAR_TABLE.charAt(index));

            // 4 + 2 from next
            if (i < len) {
                index = bte << 2 & MASK_6;
                if (++i < len) {
                    bte = data[i];
                    index |= bte >> 6 & MASK_2;
                }
                result.append(CHAR_TABLE.charAt(index));
            } else {
                i++;
                result.append(CHAR_TABLE.charAt(64));
            }
            if (i < len) {
                index = bte & MASK_6;
                result.append(CHAR_TABLE.charAt(index));
            } else {
                result.append(CHAR_TABLE.charAt(64));
            }
        }

        return result.toString();
    }
535001
@Override
    public int encode ( byte[] dst, int dstIndex ) {
        int start = dstIndex;

        SMBUtil.writeInt2(this.hashAlgos != null ? this.hashAlgos.length : 0, dst, dstIndex);
        SMBUtil.writeInt2(this.salt != null ? this.salt.length : 0, dst, dstIndex + 2);
        dstIndex += 4;

        if ( this.hashAlgos != null ) {
            for ( int hashAlgo : this.hashAlgos ) {
                SMBUtil.writeInt2(hashAlgo, dst, dstIndex);
                dstIndex += 2;
            }
        }

        if ( this.salt != null ) {
            System.arraycopy(this.salt, 0, dst, dstIndex, this.salt.length);
            dstIndex += this.salt.length;
        }

        return dstIndex - start;
    }
860219
@Pure
	@Inline(value = "Base64Coder.encode($1, ($1).length)", imported = {Base64Coder.class})
	public static char[] encode(byte[] in) {
		return encode(in, in.length);
	}
242037
public static byte[] decode(String text) throws Exception
	{
		if (text.length() % 4 != 0)
		{
			throw new Exception("Base64 not a multiple of 4 bytes");
		}

		byte[] result = new byte[text.length() / 4 * 3];
		int p = 0;

		for (int i = 0; i < text.length();)
		{
			if (text.charAt(i) == '\n')
			{
				continue;
			}

			int b1 = b64decode(text.charAt(i++));
			int b2 = b64decode(text.charAt(i++));
			int b3 = b64decode(text.charAt(i++));
			int b4 = b64decode(text.charAt(i++));

			if (b4 >= 0)
			{
				int three = b4 | b3 << 6 | b2 << 12 | b1 << 18;
				result[p++] = (byte) ((three & 0xff0000) >> 16);
				result[p++] = (byte) ((three & 0xff00) >> 8);
				result[p++] = (byte) (three & 0xff);
			} else if (b3 >= 0)
			{
				int two = b3 << 6 | b2 << 12 | b1 << 18;
				result[p++] = (byte) ((two & 0xff0000) >> 16);
				result[p++] = (byte) ((two & 0xff00) >> 8);
			} else
			{
				int one = b2 << 12 | b1 << 18;
				result[p++] = (byte) ((one & 0xff0000) >> 16);
			}
		}

		byte[] output = new byte[p];
		System.arraycopy(result, 0, output, 0, p);

		return output;
	}
1091100
public static String encode(byte[] data) {
        if (data.length == 0) {
            return "";
        }
        final byte[] bytes = copyOfRange(data, 0, data.length);
        // Count leading zeroes.
        int zeroCount = 0;
        while (zeroCount < bytes.length && bytes[zeroCount] == 0) {
            ++zeroCount;
        }
        // The actual encoding.
        byte[] temp = new byte[bytes.length * 2];
        int j = temp.length;

        int startAt = zeroCount;
        while (startAt < bytes.length) {
            byte mod = divmod58(bytes, startAt);
            if (bytes[startAt] == 0) {
                ++startAt;
            }
            temp[--j] = (byte) ALPHABET[mod];
        }

        // Strip extra '1' if there are some after decoding.
        while (j < temp.length && temp[j] == ALPHABET[0]) {
            ++j;
        }
        // Add as many leading '1' as there were leading zeros.
        while (--zeroCount >= 0) {
            temp[--j] = (byte) ALPHABET[0];
        }

        byte[] output = copyOfRange(temp, j, temp.length);
        try {
            return new String(output, "US-ASCII");
        } catch (UnsupportedEncodingException e) {
            throw new ApplicationException(e);  // Cannot happen.
        }
    }
551037
@Override
	public String encodeAsString() throws IOException {
		ByteArray byteArray = encodeAsByteArray();
		if(byteArray.size == 0) return "";
		return new String(Base64Coder.encode(byteArray.array, byteArray.size));
	}
1014559
public String encryptBase64(String data, String charset) {
		return Base64.encode(encrypt(data, charset));
	}