YBase64 { public static byte[] encode(byte[] inBytes) { if (null == inBytes) { throw new NullPointerException("input buffer was null"); } 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; out[j++] = encode1(a); out[j++] = encode2(a, b); out[j++] = '-'; out[j++] = '-'; } else if (tlen == 2) { a = inBytes[i]; b = inBytes[i + 1]; c = 0; out[j++] = encode1(a); out[j++] = encode2(a, b); out[j++] = encode3(b, c); out[j++] = '-'; } else { a = inBytes[i]; b = inBytes[i + 1]; c = inBytes[i + 2]; out[j++] = encode1(a); out[j++] = encode2(a, b); out[j++] = encode3(b, c); out[j++] = encode4(c); } } return Arrays.copyOf(out, j); }  static byte[] decode(byte[] inBytes); static byte[] encode(byte[] inBytes); static final byte[] Y64_DECODE_ARRAY; }
@Test public void testEncodeSinglePaddingCharacter() { byte[] data = YBase64.encode(SINGLE_PAD_PUBLIC_KEY.getBytes(StandardCharsets.UTF_8)); assertEquals(new String(data, StandardCharsets.UTF_8), SINGLE_PADDING); }
@Test public void testEncodeDoublePaddingCharacter() { byte[] data = YBase64.encode(DOUBLE_PAD_PUBLIC_KEY.getBytes(StandardCharsets.UTF_8)); assertEquals(new String(data, StandardCharsets.UTF_8), DOUBLE_PADDING); }
@Test public void testEncodeNoPaddingCharacter() { byte[] data = YBase64.encode(NO_PAD_PUBLIC_KEY.getBytes(StandardCharsets.UTF_8)); assertEquals(new String(data, StandardCharsets.UTF_8), NO_PADDING); }
@Test public void testEncodeInvalidData() { try { YBase64.encode(null); fail(); } catch (NullPointerException ignored) { } byte[] data = new byte[0]; assertNotNull(YBase64.encode(data)); }