/** * 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 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); }
/** * 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 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(); }