Skip to content

Instantly share code, notes, and snippets.

@grommitz
Last active March 28, 2018 16:43
Show Gist options
  • Save grommitz/14abb337a6b44696c0b1675fb8ca1424 to your computer and use it in GitHub Desktop.
Save grommitz/14abb337a6b44696c0b1675fb8ca1424 to your computer and use it in GitHub Desktop.
Test to prove that java.util.Base64 class works identically to sun.misc.BASE64
@Test
public void testBase64() throws IOException {
String s = "the CAT sat on the MAT";
String enc = "dGhlIENBVCBzYXQgb24gdGhlIE1BVA==";
String sunMiscVersion = new String(new sun.misc.BASE64Decoder().decodeBuffer(enc), "utf8");
String commonsVersion = new String(Base64.getDecoder().decode(enc), "utf8");
Assert.assertThat(commonsVersion, is(sunMiscVersion));
Assert.assertThat(commonsVersion, is(s));
String encodedBySunMisc = new sun.misc.BASE64Encoder().encode(s.getBytes());
String encodedByCommons = new String(Base64.getEncoder().encode(s.getBytes()));
assertThat(encodedByCommons, is(encodedBySunMisc));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment