class YBase64 {

    public static byte[] encode(byte[] inBytes) {
        
        if (null == inBytes) {
            throw new NullPointerException("input buffer was null");
        }

        /* Sanity check, should always be padded at the end. */
        if (inBytes.length < 1) {
            return new byte[] {};
        }

        int len = inBytes.length;
        int encodeLen = y64encodeLen(len);
        byte[] out = new byte[encodeLen];
        int j = 0;
        int tlen;

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

            tlen = (len - i);
            if (tlen > 3) {
                tlen = 3;
            }

            byte a;
            byte b;
            byte c;

            if (tlen == 1) {
                a = inBytes[i];
                b = 0;
                // enc1: Y64_array[(a >> 2)];
                out[j++] = encode1(a);
                // enc2: Y64_array[((a << 4) & 0x30) + (b >> 4)];
                out[j++] = encode2(a, b);
                out[j++] = '-';
                out[j++] = '-';
            } else if (tlen == 2) {
                a = inBytes[i];
                b = inBytes[i + 1];
                c = 0;
                // enc1: Y64_array[(a >> 2)];
                out[j++] = encode1(a);
                // enc2: Y64_array[((a << 4) & 0x30) + (b >> 4)];
                out[j++] = encode2(a, b);
                // enc3: Y64_array[((b << 2) & 0x3C) + (c >> 6)];
                out[j++] = encode3(b, c);
                out[j++] = '-';
            } else {
                a = inBytes[i];
                b = inBytes[i + 1];
                c = inBytes[i + 2];
                // enc1: Y64_array[(a >> 2)];
                out[j++] = encode1(a);
                // enc2: Y64_array[((a << 4) & 0x30) + (b >> 4)];
                out[j++] = encode2(a, b);
                // enc3: Y64_array[((b << 2) & 0x3C) + (c >> 6)];
                out[j++] = encode3(b, c);
                // enc4: Y64_array[c & 0x3F];
                out[j++] = encode4(c);
            }
        }

        return Arrays.copyOf(out, j);
    }

}