/** * Creates a Base64InputStream such that all data read is either Base64-encoded or Base64-decoded from the original * provided InputStream. * * @param in * InputStream to wrap. * @param doEncode * true if we should encode all data read from us, false if we should decode. */ public Base64InputStream(final InputStream in, final boolean doEncode) { super(in, new Base64(false), doEncode); }
/** * Creates a Base64OutputStream such that all data written is either Base64-encoded or Base64-decoded to the * original provided OutputStream. * * @param out * OutputStream to wrap. * @param doEncode * true if we should encode all data written to us, false if we should decode. */ public Base64OutputStream(final OutputStream out, final boolean doEncode) { super(out,new Base64(false), doEncode); }
/** * Decodes Base64 data into octets. * <p> * <b>Note:</b> this method seamlessly handles data encoded in URL-safe or normal mode. * </p> * * @param base64Data * Byte array containing Base64 data * @return Array containing decoded data. */ public static byte[] decodeBase64(final byte[] base64Data) { return new Base64().decode(base64Data); }
/** * Decodes a Base64 String into octets. * <p> * <b>Note:</b> this method seamlessly handles data encoded in URL-safe or normal mode. * </p> * * @param base64String * String containing Base64 data * @return Array containing decoded data. * @since 1.4 */ public static byte[] decodeBase64(final String base64String) { return new Base64().decode(base64String); }
public HttpKerberosClientAction(String serverPrincipal, String serverHttpUrl) { this.serverPrincipal = serverPrincipal; this.serverHttpUrl = serverHttpUrl; base64codec = new Base64(0); httpContext = new BasicHttpContext(); httpContext.setAttribute(SERVER_HTTP_URL, serverHttpUrl); }
@Test public void testObjectDecodeWithValidParameter() throws Exception { final String original = "Hello World!"; final Object o = Base64.encodeBase64(original.getBytes(CHARSET_UTF8)); final Base64 b64 = new Base64(); final Object oDecoded = b64.decode(o); final byte[] baDecoded = (byte[]) oDecoded; final String dest = new String(baDecoded); assertEquals("dest string does not equal original", original, dest); }
@Test public void testObjectEncodeWithValidParameter() throws Exception { final String original = "Hello World!"; final Object origObj = original.getBytes(CHARSET_UTF8); final Base64 b64 = new Base64(); final Object oEncoded = b64.encode(origObj); final byte[] bArray = Base64.decodeBase64((byte[]) oEncoded); final String dest = new String(bArray); assertEquals("dest string does not equal original", original, dest); }
@Test public void testObjectEncode() throws Exception { final Base64 b64 = new Base64(); assertEquals("SGVsbG8gV29ybGQ=", new String(b64.encode("Hello World".getBytes(CHARSET_UTF8)))); }
@Test public void testConstructor_Int_ByteArray_Boolean_UrlSafe() { // url-safe variation final Base64 base64 = new Base64(64, new byte[] { '\t' }, true); final byte[] encoded = base64.encode(Base64TestData.DECODED); String expectedResult = Base64TestData.ENCODED_64_CHARS_PER_LINE; expectedResult = expectedResult.replaceAll("=", ""); // url-safe has no // == padding. expectedResult = expectedResult.replace('\n', '\t'); expectedResult = expectedResult.replace('+', '-'); expectedResult = expectedResult.replace('/', '_'); final String result = StringUtils.newStringUtf8(encoded); assertEquals("new Base64(64, \\t, true)", result, expectedResult); }
@Test public void testObjectDecodeWithInvalidParameter() throws Exception { final Base64 b64 = new Base64(); try { b64.decode(Integer.valueOf(5)); fail("decode(Object) didn't throw an exception when passed an Integer object"); } catch (final DecoderException e) { // ignored } }
private void testBase64InBuffer(final int startPasSize, final int endPadSize) { final String content = "Hello World"; String encodedContent; final byte[] bytesUtf8 = StringUtils.getBytesUtf8(content); byte[] buffer = ArrayUtils.addAll(bytesUtf8, new byte[endPadSize]); buffer = ArrayUtils.addAll(new byte[startPasSize], buffer); final byte[] encodedBytes = new Base64().encode(buffer, startPasSize, bytesUtf8.length); encodedContent = StringUtils.newStringUtf8(encodedBytes); assertEquals("encoding hello world", "SGVsbG8gV29ybGQ=", encodedContent); }
@Test public void testConstructor_Int_ByteArray_Boolean() { final Base64 base64 = new Base64(65, new byte[] { '\t' }, false); final byte[] encoded = base64.encode(Base64TestData.DECODED); String expectedResult = Base64TestData.ENCODED_64_CHARS_PER_LINE; expectedResult = expectedResult.replace('\n', '\t'); final String result = StringUtils.newStringUtf8(encoded); assertEquals("new Base64(65, \\t, false)", expectedResult, result); }
@Test public void testObjectEncodeWithInvalidParameter() throws Exception { final Base64 b64 = new Base64(); try { b64.encode("Yadayadayada"); fail("encode(Object) didn't throw an exception when passed a String object"); } catch (final EncoderException e) { // Expected } }
/** * Tests isUrlSafe. */ @Test public void testIsUrlSafe() { final Base64 base64Standard = new Base64(false); final Base64 base64URLSafe = new Base64(true); assertFalse("Base64.isUrlSafe=false", base64Standard.isUrlSafe()); assertTrue("Base64.isUrlSafe=true", base64URLSafe.isUrlSafe()); final byte[] whiteSpace = { ' ', '\n', '\r', '\t' }; assertTrue("Base64.isBase64(whiteSpace)=true", Base64.isBase64(whiteSpace)); }
/** * Tests to make sure Base64's implementation of the org.apache.commons.codec.BinaryEncoder * interface is behaving identical to commons-codec-1.3.jar. * * @throws EncoderException problem */ @Test public void testBinaryEncoder() throws EncoderException { final BinaryEncoder enc = new Base64(); for (int i = 0; i < STRINGS.length; i++) { if (STRINGS[i] != null) { final byte[] base64 = utf8(STRINGS[i]); final byte[] binary = BYTES[i]; final boolean b = Arrays.equals(base64, enc.encode(binary)); assertTrue("BinaryEncoder test-" + i, b); } } }
/** * Tests to make sure Base64's implementation of the org.apache.commons.codec.BinaryDecoder * interface is behaving identical to commons-codec-1.3.jar. * * @throws DecoderException problem */ @Test public void testBinaryDecoder() throws DecoderException { final BinaryDecoder dec = new Base64(); for (int i = 0; i < STRINGS.length; i++) { if (STRINGS[i] != null) { final byte[] base64 = utf8(STRINGS[i]); final byte[] binary = BYTES[i]; final boolean b = Arrays.equals(binary, dec.decode(base64)); assertTrue("BinaryDecoder test-" + i, b); } } }
/** * Tests to make sure Base64's implementation of the org.apache.commons.codec.Encoder * interface is behaving identical to commons-codec-1.3.jar. * * @throws EncoderException problem */ @Test public void testEncoder() throws EncoderException { final Encoder enc = new Base64(); for (int i = 0; i < STRINGS.length; i++) { if (STRINGS[i] != null) { final byte[] base64 = utf8(STRINGS[i]); final byte[] binary = BYTES[i]; final boolean b = Arrays.equals(base64, (byte[]) enc.encode(binary)); assertTrue("Encoder test-" + i, b); } } }
/** * Tests to make sure Base64's implementation of the org.apache.commons.codec.Decoder * interface is behaving identical to commons-codec-1.3.jar. * * @throws DecoderException problem */ @Test public void testDecoder() throws DecoderException { final Decoder dec = new Base64(); for (int i = 0; i < STRINGS.length; i++) { if (STRINGS[i] != null) { final byte[] base64 = utf8(STRINGS[i]); final byte[] binary = BYTES[i]; final boolean b = Arrays.equals(binary, (byte[]) dec.decode(base64)); assertTrue("Decoder test-" + i, b); } } }
/** * Tests a lineSeparator much bigger than DEFAULT_BUFFER_SIZE. * * @see "<a href='http://mail-archives.apache.org/mod_mbox/commons-dev/201202.mbox/%3C4F3C85D7.5060706@snafu.de%3E'>dev@commons.apache.org</a>" */ @Test @Ignore public void testHugeLineSeparator() { final int BaseNCodec_DEFAULT_BUFFER_SIZE = 8192; final int Base64_BYTES_PER_ENCODED_BLOCK = 4; final byte[] baLineSeparator = new byte[BaseNCodec_DEFAULT_BUFFER_SIZE * 4 - 3]; final Base64 b64 = new Base64(Base64_BYTES_PER_ENCODED_BLOCK, baLineSeparator); final String strOriginal = "Hello World"; final String strDecoded = new String(b64.decode(b64.encode(StringUtils.getBytesUtf8(strOriginal)))); assertEquals("testDEFAULT_BUFFER_SIZE", strOriginal, strDecoded); }
@Test public void testNonBase64Test() throws Exception { final byte[] bArray = { '%' }; assertFalse("Invalid Base64 array was incorrectly validated as " + "an array of Base64 encoded data", Base64.isBase64(bArray)); try { final Base64 b64 = new Base64(); final byte[] result = b64.decode(bArray); assertEquals("The result should be empty as the test encoded content did " + "not contain any valid base 64 characters", 0, result.length); } catch (final Exception e) { fail("Exception was thrown when trying to decode " + "invalid base64 encoded data - RFC 2045 requires that all " + "non base64 character be discarded, an exception should not" + " have been thrown"); } }