FlattrConnection implements Connection { protected String base64(String str) { try { byte[] data = str.getBytes(ENCODING); StringBuilder sb = new StringBuilder(); for (int ix = 0; ix < data.length; ix += 3) { int triplet = (data[ix] & 0xFF) << 16; if (ix + 1 < data.length) { triplet |= (data[ix+1] & 0xFF) << 8; } if (ix + 2 < data.length) { triplet |= (data[ix+2] & 0xFF); } for (int iy = 0; iy < 4; iy++) { if (ix + iy <= data.length) { int ch = (triplet & 0xFC0000) >> 18; sb.append(BASE64.charAt(ch)); triplet <<= 6; } else { sb.append('='); } } } return sb.toString(); } catch (UnsupportedEncodingException ex) { throw new IllegalArgumentException(ENCODING, ex); } } FlattrConnection(RequestType type); @Override Connection url(String url); @Override Connection call(String call); @Override Connection token(AccessToken token); @Override Connection key(ConsumerKey key); @Override Connection parameter(String name, String value); @Override Connection parameterArray(String name, String[] value); @Override Connection query(String name, String value); @Override Connection data(FlattrObject data); @Override Connection form(String name, String value); @Override Connection rateLimit(RateLimit limit); @Override Collection<FlattrObject> result(); @Override FlattrObject singleResult();  }
@Test public void base64Test() { FlattrConnection conn = new FlattrConnection(RequestType.GET); assertThat(conn.base64(""), is("")); assertThat(conn.base64("1"), is("MQ==")); assertThat(conn.base64("12"), is("MTI=")); assertThat(conn.base64("123"), is("MTIz")); assertThat(conn.base64("foo:bar"), is("Zm9vOmJhcg==")); assertThat(conn.base64("barFooX2"), is("YmFyRm9vWDI=")); assertThat(conn.base64("TeStFoüX4"), is("VGVTdEZvw7xYNA==")); assertThat(conn.base64("AlmostDone"), is("QWxtb3N0RG9uZQ==")); assertThat(conn.base64("Just4Test#i"), is("SnVzdDRUZXN0I2k=")); assertThat(conn.base64("FinAlTeSt567"), is("RmluQWxUZVN0NTY3")); }