@Override public void process(InputStream in, OutputStream out) throws IOException { try (Base64InputStream bis = new Base64InputStream(new ValidatingBase64InputStream(in))) { int len = -1; byte[] buf = new byte[8192]; while ((len = bis.read(buf)) > 0) { out.write(buf, 0, len); } out.flush(); } } });
/** * Test for the CODEC-101 bug: InputStream.read(byte[]) should never return 0 because Java's builtin InputStreamReader hates that. * * @throws Exception * for some failure scenarios. */ @Test public void testCodec101() throws Exception { final byte[] codec101 = StringUtils.getBytesUtf8(Base64TestData.CODEC_101_MULTIPLE_OF_3); final ByteArrayInputStream bais = new ByteArrayInputStream(codec101); final Base64InputStream in = new Base64InputStream(bais); final byte[] result = new byte[8192]; int c = in.read(result); assertTrue("Codec101: First read successful [c=" + c + "]", c > 0); c = in.read(result); assertTrue("Codec101: Second read should report end-of-stream [c=" + c + "]", c < 0); in.close(); }
in.read(buf, -1, 0); fail("Expected Base64InputStream.read(buf, -1, 0) to throw IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException e) { in.read(buf, 0, -1); fail("Expected Base64InputStream.read(buf, 0, -1) to throw IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException e) { in.read(buf, buf.length + 1, 0); fail("Base64InputStream.read(buf, buf.length + 1, 0) throws IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException e) { in.read(buf, buf.length - 1, 2); fail("Base64InputStream.read(buf, buf.length - 1, 2) throws IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException e) {
/** * Tests skipping number of characters larger than the internal buffer. * * @throws Throwable */ @Test public void testSkipBig() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_B64)); final Base64InputStream b64stream = new Base64InputStream(ins); assertEquals(6, b64stream.skip(Integer.MAX_VALUE)); // End of stream reached assertEquals(-1, b64stream.read()); assertEquals(-1, b64stream.read()); b64stream.close(); }
/** * Tests skipping past the end of a stream. * * @throws Throwable */ @Test public void testSkipPastEnd() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_B64)); final Base64InputStream b64stream = new Base64InputStream(ins); // due to CODEC-130, skip now skips correctly decoded characters rather than encoded assertEquals(6, b64stream.skip(10)); // End of stream reached assertEquals(-1, b64stream.read()); assertEquals(-1, b64stream.read()); b64stream.close(); }
/** * Tests skipping to the end of a stream. * * @throws Throwable */ @Test public void testSkipToEnd() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_B64)); final Base64InputStream b64stream = new Base64InputStream(ins); // due to CODEC-130, skip now skips correctly decoded characters rather than encoded assertEquals(6, b64stream.skip(6)); // End of stream reached assertEquals(-1, b64stream.read()); assertEquals(-1, b64stream.read()); b64stream.close(); }
/** * Tests skipping as a noop * * @throws Throwable */ @Test public void testSkipNone() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_B64)); final Base64InputStream b64stream = new Base64InputStream(ins); final byte[] actualBytes = new byte[6]; assertEquals(0, b64stream.skip(0)); b64stream.read(actualBytes, 0, actualBytes.length); assertArrayEquals(actualBytes, new byte[] { 0, 0, 0, (byte) 255, (byte) 255, (byte) 255 }); // End of stream reached assertEquals(-1, b64stream.read()); b64stream.close(); }
/** * Tests read returning 0 * * @throws Exception */ @Test public void testRead0() throws Exception { final byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE); final byte[] buf = new byte[1024]; int bytesRead = 0; final ByteArrayInputStream bin = new ByteArrayInputStream(decoded); final Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[] { 0, 0, 0 }); bytesRead = in.read(buf, 0, 0); assertEquals("Base64InputStream.read(buf, 0, 0) returns 0", 0, bytesRead); in.close(); }
/** * Tests read with null. * * @throws Exception * for some failure scenarios. */ @Test public void testReadNull() throws Exception { final byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE); final ByteArrayInputStream bin = new ByteArrayInputStream(decoded); final Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[] { 0, 0, 0 }); try { in.read(null, 0, 0); fail("Base64InputStream.read(null, 0, 0) to throw a NullPointerException"); } catch (final NullPointerException e) { // Expected } in.close(); }
/** * Tests the bug reported in CODEC-105. Bad interactions with InputStream when reading one byte at a time. */ @Test public void testCodec105() throws IOException { final Base64InputStream in = new Base64InputStream(new Codec105ErrorInputStream(), true, 0, null); try { for (int i = 0; i < 5; i++) { in.read(); } } finally { in.close(); } }
/** * Tests skipping past the end of a stream. * * @throws Throwable */ @Test public void testAvailable() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_B64)); final Base64InputStream b64stream = new Base64InputStream(ins); assertEquals(1, b64stream.available()); assertEquals(6, b64stream.skip(10)); // End of stream reached assertEquals(0, b64stream.available()); assertEquals(-1, b64stream.read()); assertEquals(-1, b64stream.read()); assertEquals(0, b64stream.available()); b64stream.close(); }
import java.io.*; import org.apache.commons.codec.binary.Base64InputStream; class tmp { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream(args[0]); Base64InputStream b64is = new Base64InputStream(fis, true, -1, null); while (true) { byte[] c = new byte[1024]; int n = b64is.read(c); if (n < 0) break; if (n == 0) throw new IOException("returned 0!"); for (int i = 0; i < n; i++) { System.out.print((char)c[i]); } } } }
private byte[] decodeBase64(File privateKeyFile) throws IOException { Base64InputStream base64InputStream; base64InputStream = new Base64InputStream(new FileInputStream(privateKeyFile)); ByteArrayOutputStream decodedBytes = new ByteArrayOutputStream((int) privateKeyFile.length() * 2); byte[] readBuffer = new byte[1024]; int bytesRead; while ((bytesRead = base64InputStream.read(readBuffer)) != -1) { decodedBytes.write(readBuffer, 0, bytesRead); } return decodedBytes.toByteArray(); }
@Override public void process(InputStream in, OutputStream out) throws IOException { try (Base64InputStream bis = new Base64InputStream(new ValidatingBase64InputStream(in))) { int len = -1; byte[] buf = new byte[8192]; while ((len = bis.read(buf)) > 0) { out.write(buf, 0, len); } out.flush(); } } });
while (( read = b64input.read(buf, 0, 1024)) > 0) { os.write(buf, 0, read);
int read = -1; try { while ((read = b64input.read(buf, 0, 1024)) > 0) { os.write(buf, 0, read);
int read = -1; try { while ((read = b64input.read(buf, 0, 1024)) > 0) { os.write(buf, 0, read);