Refine search
/** * Another test for the CODEC-101 bug: In commons-codec-1.4 this test shows InputStreamReader explicitly hating an * InputStream.read(byte[]) return of 0: * * java.io.IOException: Underlying input stream returned zero bytes at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:268) at * sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306) at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158) at * java.io.InputStreamReader.read(InputStreamReader.java:167) at java.io.BufferedReader.fill(BufferedReader.java:136) at * java.io.BufferedReader.readLine(BufferedReader.java:299) at java.io.BufferedReader.readLine(BufferedReader.java:362) at * org.apache.commons.codec.binary.Base64InputStreamTest.testInputStreamReader(Base64InputStreamTest.java:75) * * But in commons-codec-1.5 it's fixed. :-) * * @throws Exception * for some failure scenarios. */ @Test public void testInputStreamReader() 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 InputStreamReader isr = new InputStreamReader(in); final BufferedReader br = new BufferedReader(isr); final String line = br.readLine(); assertNotNull("Codec101: InputStreamReader works!", line); br.close(); }
/** * 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(); }
@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(); } } });
/** * Tests if negative arguments to skip are handled correctly. * * @throws Throwable */ @Test(expected=IllegalArgumentException.class) public void testSkipWrongArgument() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_B64)); final Base64InputStream b64stream = new Base64InputStream(ins); b64stream.skip(-10); b64stream.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 markSupported. * * @throws Exception */ @Test public void testMarkSupported() 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 }); // Always returns false for now. assertFalse("Base64InputStream.markSupported() is false", in.markSupported()); in.close(); }
/** * Tests the problem reported in CODEC-130. Missing / wrong implementation of skip. */ @Test public void testCodec130() throws IOException { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final Base64OutputStream base64os = new Base64OutputStream(bos); base64os.write(StringUtils.getBytesUtf8(STRING_FIXTURE)); base64os.close(); final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); final Base64InputStream ins = new Base64InputStream(bis); // we skip the first character read from the reader ins.skip(1); final byte[] decodedBytes = Base64TestData.streamToBytes(ins, new byte[64]); final String str = StringUtils.newStringUtf8(decodedBytes); assertEquals(STRING_FIXTURE.substring(1), str); }
private InputStream decodeInputStreamWithApache(InputStream origStream) { return new Base64InputStream(origStream); }
private File toBase64(File imageFile) throws Exception { LOG.info(this.getClass().getName() + " toBase64 method is called"); System.out.println("toBase64 is called"); Base64InputStream in = new Base64InputStream(new FileInputStream(imageFile), true); File f = new File("/root/temp/" + imageFile.getName() .replaceFirst("[.][^.]+$", "") + "_base64.txt"); Files.copy(in, f.toPath(), StandardCopyOption.REPLACE_EXISTING); in.close(); return f; }
@Override public void process(InputStream in, OutputStream out) throws IOException { try (Base64InputStream bis = new Base64InputStream(new ValidatingBase64InputStream(in))) { StreamUtils.copy(bis, out); } } }
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]); } } } }
in = new Base64InputStream(new ByteArrayInputStream(decoded), true, chunkSize, separator); byte[] output = new byte[encoded.length]; for (int i = 0; i < output.length; i++) { in = new Base64InputStream(new ByteArrayInputStream(encoded)); output = new byte[decoded.length]; for (int i = 0; i < output.length; i++) { in = new ByteArrayInputStream(decoded); for (int i = 0; i < 10; i++) { in = new Base64InputStream(in, true, chunkSize, separator); in = new Base64InputStream(in, false);
@Override public Object unmarshal(Exchange exchange, InputStream input) throws Exception { return new Base64InputStream(input, false, lineLength, lineSeparator); }
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(); }
in = new Base64InputStream(new ByteArrayInputStream(decoded), true, chunkSize, separator); byte[] output = Base64TestData.streamToBytes(in); in = new Base64InputStream(new ByteArrayInputStream(encoded)); output = Base64TestData.streamToBytes(in); in = new ByteArrayInputStream(decoded); for (int i = 0; i < 10; i++) { in = new Base64InputStream(in, true, chunkSize, separator); in = new Base64InputStream(in, false);
/** * 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(); }
private InputStream decodeInputStreamWithApache(InputStream origStream) { return new Base64InputStream(origStream); }
@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 the Base64InputStream implementation against the special NPE inducing input identified in the CODEC-98 bug. * * @throws Exception * for some failure scenarios. */ @Test public void testCodec98NPE() throws Exception { final byte[] codec98 = StringUtils.getBytesUtf8(Base64TestData.CODEC_98_NPE); final ByteArrayInputStream data = new ByteArrayInputStream(codec98); final Base64InputStream stream = new Base64InputStream(data); // This line causes an NPE in commons-codec-1.4.jar: final byte[] decodedBytes = Base64TestData.streamToBytes(stream, new byte[1024]); final String decoded = StringUtils.newStringUtf8(decodedBytes); assertEquals("codec-98 NPE Base64InputStream", Base64TestData.CODEC_98_NPE_DECODED, decoded); }
/** * 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(); }