Java Base64 Encode base64Encoder(byte[] src, int start, int wrapAt)

Here you can find the source of base64Encoder(byte[] src, int start, int wrapAt)

Description

base Encoder

License

Open Source License

Declaration

public final static String base64Encoder(byte[] src, int start, int wrapAt) 

Method Source Code

//package com.java2s;

public class Main {
    final static String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    final static char pad = '=';

    public final static String base64Encoder(byte[] src, int start, int wrapAt) {
        return base64Encoder(src, start, src.length, wrapAt);
    }//from ww w  .j  av  a2 s. c o m

    public final static String base64Encoder(byte[] src, int start, int length, int wrapAt) {
        if (src == null || src.length == 0)
            return null;
        StringBuffer encodeDst = new StringBuffer();
        int lineCounter = 0;
        length = start + length > src.length ? src.length : start + length;
        while (start < length) {
            int buffer = 0, byteCounter;
            for (byteCounter = 0; byteCounter < 3 && start < length; byteCounter++, start++)
                buffer |= (src[start] & 0xFF) << (16 - (byteCounter << 3));
            if (wrapAt > 0 && lineCounter == wrapAt) {
                encodeDst.append("\r\n");
                lineCounter = 0;
            }
            char b1 = chars.charAt((buffer << 8) >>> 26);
            char b2 = chars.charAt((buffer << 14) >>> 26);
            char b3 = (byteCounter < 2) ? pad : chars.charAt((buffer << 20) >>> 26);
            char b4 = (byteCounter < 3) ? pad : chars.charAt((buffer << 26) >>> 26);
            encodeDst.append(b1).append(b2).append(b3).append(b4);
            lineCounter += 4;
        }
        return encodeDst.toString();
    }
}

Related

  1. base64encode(String str)
  2. base64Encode(String string)
  3. base64Encode(String string)
  4. base64encode(String text)
  5. base64EncodeGeneric(String digits, byte[] data)
  6. base64Encoder(String str)
  7. base64encoding(byte[] bytes)
  8. base64UrlDecode(final String encodedString)
  9. base64UrlEncode(final byte[] v)