@Test public void testEncodeB() throws Exception { Assert.assertEquals("", encodeB("")); Assert.assertEquals("YQ==", encodeB("a")); Assert.assertEquals("YWI=", encodeB("ab")); Assert.assertEquals("YWJj", encodeB("abc")); Assert.assertEquals("YWJjZA==", encodeB("abcd")); Assert.assertEquals("YWJjZGU=", encodeB("abcde")); Assert.assertEquals("YWJjZGVm", encodeB("abcdef")); Assert.assertEquals("YWJjZGVmZw==", encodeB("abcdefg")); Assert.assertEquals("YWJjZGVmZ2g=", encodeB("abcdefgh")); Assert.assertEquals("YWJjZGVmZ2hp", encodeB("abcdefghi")); Assert.assertEquals("DQoMCQ==", encodeB("\r\n\f\t")); Assert.assertEquals("LT0/VGhhdCdzIGEgdGVzdD89LQ==", encodeB("-=?That's a test?=-")); }
public static String encodeB(byte[] bytes) { StringBuilder sb = new StringBuilder(); int idx = 0; final int end = bytes.length; for (; idx < end - 2; idx += 3) { int data = (bytes[idx] & 0xff) << 16 | (bytes[idx + 1] & 0xff) << 8 | bytes[idx + 2] & 0xff; sb.append((char) BASE64_TABLE[data >> 18 & 0x3f]); sb.append((char) BASE64_TABLE[data >> 12 & 0x3f]); sb.append((char) BASE64_TABLE[data >> 6 & 0x3f]); sb.append((char) BASE64_TABLE[data & 0x3f]); } if (idx == end - 2) { int data = (bytes[idx] & 0xff) << 16 | (bytes[idx + 1] & 0xff) << 8; sb.append((char) BASE64_TABLE[data >> 18 & 0x3f]); sb.append((char) BASE64_TABLE[data >> 12 & 0x3f]); sb.append((char) BASE64_TABLE[data >> 6 & 0x3f]); sb.append(BASE64_PAD); } else if (idx == end - 1) { int data = (bytes[idx] & 0xff) << 16; sb.append((char) BASE64_TABLE[data >> 18 & 0x3f]); sb.append((char) BASE64_TABLE[data >> 12 & 0x3f]); sb.append(BASE64_PAD); sb.append(BASE64_PAD); } return sb.toString(); }
EncoderUtil { public static String encodeB(byte[] bytes) { StringBuilder sb = new StringBuilder(); int idx = 0; final int end = bytes.length; for (; idx < end - 2; idx += 3) { int data = (bytes[idx] & 0xff) << 16 | (bytes[idx + 1] & 0xff) << 8 | bytes[idx + 2] & 0xff; sb.append((char) BASE64_TABLE[data >> 18 & 0x3f]); sb.append((char) BASE64_TABLE[data >> 12 & 0x3f]); sb.append((char) BASE64_TABLE[data >> 6 & 0x3f]); sb.append((char) BASE64_TABLE[data & 0x3f]); } if (idx == end - 2) { int data = (bytes[idx] & 0xff) << 16 | (bytes[idx + 1] & 0xff) << 8; sb.append((char) BASE64_TABLE[data >> 18 & 0x3f]); sb.append((char) BASE64_TABLE[data >> 12 & 0x3f]); sb.append((char) BASE64_TABLE[data >> 6 & 0x3f]); sb.append(BASE64_PAD); } else if (idx == end - 1) { int data = (bytes[idx] & 0xff) << 16; sb.append((char) BASE64_TABLE[data >> 18 & 0x3f]); sb.append((char) BASE64_TABLE[data >> 12 & 0x3f]); sb.append(BASE64_PAD); sb.append(BASE64_PAD); } return sb.toString(); } }
EncoderUtil { public static String encodeB(byte[] bytes) { StringBuilder sb = new StringBuilder(); int idx = 0; final int end = bytes.length; for (; idx < end - 2; idx += 3) { int data = (bytes[idx] & 0xff) << 16 | (bytes[idx + 1] & 0xff) << 8 | bytes[idx + 2] & 0xff; sb.append((char) BASE64_TABLE[data >> 18 & 0x3f]); sb.append((char) BASE64_TABLE[data >> 12 & 0x3f]); sb.append((char) BASE64_TABLE[data >> 6 & 0x3f]); sb.append((char) BASE64_TABLE[data & 0x3f]); } if (idx == end - 2) { int data = (bytes[idx] & 0xff) << 16 | (bytes[idx + 1] & 0xff) << 8; sb.append((char) BASE64_TABLE[data >> 18 & 0x3f]); sb.append((char) BASE64_TABLE[data >> 12 & 0x3f]); sb.append((char) BASE64_TABLE[data >> 6 & 0x3f]); sb.append(BASE64_PAD); } else if (idx == end - 1) { int data = (bytes[idx] & 0xff) << 16; sb.append((char) BASE64_TABLE[data >> 18 & 0x3f]); sb.append((char) BASE64_TABLE[data >> 12 & 0x3f]); sb.append(BASE64_PAD); sb.append(BASE64_PAD); } return sb.toString(); } private  EncoderUtil(); }
EncoderUtil { public static String encodeB(byte[] bytes) { StringBuilder sb = new StringBuilder(); int idx = 0; final int end = bytes.length; for (; idx < end - 2; idx += 3) { int data = (bytes[idx] & 0xff) << 16 | (bytes[idx + 1] & 0xff) << 8 | bytes[idx + 2] & 0xff; sb.append((char) BASE64_TABLE[data >> 18 & 0x3f]); sb.append((char) BASE64_TABLE[data >> 12 & 0x3f]); sb.append((char) BASE64_TABLE[data >> 6 & 0x3f]); sb.append((char) BASE64_TABLE[data & 0x3f]); } if (idx == end - 2) { int data = (bytes[idx] & 0xff) << 16 | (bytes[idx + 1] & 0xff) << 8; sb.append((char) BASE64_TABLE[data >> 18 & 0x3f]); sb.append((char) BASE64_TABLE[data >> 12 & 0x3f]); sb.append((char) BASE64_TABLE[data >> 6 & 0x3f]); sb.append(BASE64_PAD); } else if (idx == end - 1) { int data = (bytes[idx] & 0xff) << 16; sb.append((char) BASE64_TABLE[data >> 18 & 0x3f]); sb.append((char) BASE64_TABLE[data >> 12 & 0x3f]); sb.append(BASE64_PAD); sb.append(BASE64_PAD); } return sb.toString(); } private  EncoderUtil(); static String encodeAddressDisplayName(String displayName); static String encodeAddressLocalPart(String localPart); static String encodeHeaderParameter(String name, String value); static String encodeIfNecessary(String text, Usage usage,
            int usedCharacters); static boolean hasToBeEncoded(String text, int usedCharacters); static String encodeEncodedWord(String text, Usage usage); static String encodeEncodedWord(String text, Usage usage,
            int usedCharacters); static String encodeEncodedWord(String text, Usage usage,
            int usedCharacters, Charset charset, Encoding encoding); static String encodeB(byte[] bytes); static String encodeQ(byte[] bytes, Usage usage); static void encodeQBinary(final InputStream in, final OutputStream out); static void encodeQ(final InputStream in, final OutputStream out); static void encodeB(final InputStream in, final OutputStream out); static boolean isToken(String str); }
EncoderUtil { public static String encodeB(byte[] bytes) { StringBuilder sb = new StringBuilder(); int idx = 0; final int end = bytes.length; for (; idx < end - 2; idx += 3) { int data = (bytes[idx] & 0xff) << 16 | (bytes[idx + 1] & 0xff) << 8 | bytes[idx + 2] & 0xff; sb.append((char) BASE64_TABLE[data >> 18 & 0x3f]); sb.append((char) BASE64_TABLE[data >> 12 & 0x3f]); sb.append((char) BASE64_TABLE[data >> 6 & 0x3f]); sb.append((char) BASE64_TABLE[data & 0x3f]); } if (idx == end - 2) { int data = (bytes[idx] & 0xff) << 16 | (bytes[idx + 1] & 0xff) << 8; sb.append((char) BASE64_TABLE[data >> 18 & 0x3f]); sb.append((char) BASE64_TABLE[data >> 12 & 0x3f]); sb.append((char) BASE64_TABLE[data >> 6 & 0x3f]); sb.append(BASE64_PAD); } else if (idx == end - 1) { int data = (bytes[idx] & 0xff) << 16; sb.append((char) BASE64_TABLE[data >> 18 & 0x3f]); sb.append((char) BASE64_TABLE[data >> 12 & 0x3f]); sb.append(BASE64_PAD); sb.append(BASE64_PAD); } return sb.toString(); } private  EncoderUtil(); static String encodeAddressDisplayName(String displayName); static String encodeAddressLocalPart(String localPart); static String encodeHeaderParameter(String name, String value); static String encodeIfNecessary(String text, Usage usage,
            int usedCharacters); static boolean hasToBeEncoded(String text, int usedCharacters); static String encodeEncodedWord(String text, Usage usage); static String encodeEncodedWord(String text, Usage usage,
            int usedCharacters); static String encodeEncodedWord(String text, Usage usage,
            int usedCharacters, Charset charset, Encoding encoding); static String encodeB(byte[] bytes); static String encodeQ(byte[] bytes, Usage usage); static void encodeQBinary(final InputStream in, final OutputStream out); static void encodeQ(final InputStream in, final OutputStream out); static void encodeB(final InputStream in, final OutputStream out); static boolean isToken(String str);  }