/** * 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_FOO)); final Base32InputStream b32stream = new Base32InputStream(ins); // due to CODEC-130, skip now skips correctly decoded characters rather than encoded assertEquals(3, b32stream.skip(10)); // End of stream reached assertEquals(-1, b32stream.read()); assertEquals(-1, b32stream.read()); b32stream.close(); }
/** * Tests markSupported. * * @throws Exception */ @Test public void testMarkSupported() throws Exception { final byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE); final ByteArrayInputStream bin = new ByteArrayInputStream(decoded); final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 }); // Always returns false for now. assertFalse("Base32InputStream.markSupported() is false", in.markSupported()); in.close(); }
/** * 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_FOO)); final Base32InputStream b32stream = new Base32InputStream(ins); b32stream.skip(-10); b32stream.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 Base32InputStream in = new Base32InputStream(new Codec105ErrorInputStream(), true, 0, null); try { for (int i = 0; i < 5; i++) { in.read(); } } finally { in.close(); } }
@Override public void process(InputStream in, OutputStream out) throws IOException { try (Base32InputStream bis = new Base32InputStream(new ValidatingBase32InputStream(in))) { StreamUtils.copy(bis, out); } } }
/** * Tests the problem reported in CODEC-130. Missing / wrong implementation of skip. */ @Test public void testCodec130() throws IOException { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final Base32OutputStream base32os = new Base32OutputStream(bos); base32os.write(StringUtils.getBytesUtf8(STRING_FIXTURE)); base32os.close(); final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); final Base32InputStream ins = new Base32InputStream(bis); // we skip the first character read from the reader ins.skip(1); final byte[] decodedBytes = Base32TestData.streamToBytes(ins, new byte[64]); final String str = StringUtils.newStringUtf8(decodedBytes); assertEquals(STRING_FIXTURE.substring(1), str); }
/** * Tests read returning 0 * * @throws Exception */ @Test public void testRead0() throws Exception { final byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE); final byte[] buf = new byte[1024]; int bytesRead = 0; final ByteArrayInputStream bin = new ByteArrayInputStream(decoded); final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 }); bytesRead = in.read(buf, 0, 0); assertEquals("Base32InputStream.read(buf, 0, 0) returns 0", 0, bytesRead); in.close(); }
in = new Base32InputStream(new ByteArrayInputStream(decoded), true, chunkSize, separator); byte[] output = new byte[encoded.length]; for (int i = 0; i < output.length; i++) { in = new Base32InputStream(new ByteArrayInputStream(encoded)); output = new byte[decoded.length]; for (int i = 0; i < output.length; i++) { in = new Base32InputStream(in, true, chunkSize, separator); in = new Base32InputStream(in, false);
/** * 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_FOO)); final Base32InputStream b32stream = new Base32InputStream(ins); // due to CODEC-130, skip now skips correctly decoded characters rather than encoded assertEquals(3, b32stream.skip(3)); // End of stream reached assertEquals(-1, b32stream.read()); assertEquals(-1, b32stream.read()); b32stream.close(); }
/** * Tests read with null. * * @throws Exception * for some failure scenarios. */ @Test public void testReadNull() throws Exception { final byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE); final ByteArrayInputStream bin = new ByteArrayInputStream(decoded); final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 }); try { in.read(null, 0, 0); fail("Base32InputStream.read(null, 0, 0) to throw a NullPointerException"); } catch (final NullPointerException e) { // Expected } in.close(); }
in = new Base32InputStream(new ByteArrayInputStream(decoded), true, chunkSize, separator); byte[] output = Base32TestData.streamToBytes(in); in = new Base32InputStream(new ByteArrayInputStream(encoded)); output = Base32TestData.streamToBytes(in); in = new Base32InputStream(in, true, chunkSize, separator); in = new Base32InputStream(in, false);
/** * Tests skipping as a noop * * @throws Throwable */ @Test public void testSkipNone() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO)); final Base32InputStream b32stream = new Base32InputStream(ins); final byte[] actualBytes = new byte[6]; assertEquals(0, b32stream.skip(0)); b32stream.read(actualBytes, 0, actualBytes.length); assertArrayEquals(actualBytes, new byte[] { 102, 111, 111, 0, 0, 0 }); // End of stream reached assertEquals(-1, b32stream.read()); b32stream.close(); }
final byte[] buf = new byte[1024]; final ByteArrayInputStream bin = new ByteArrayInputStream(decoded); final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 }); in.read(buf, -1, 0); fail("Expected Base32InputStream.read(buf, -1, 0) to throw IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException e) { in.read(buf, 0, -1); fail("Expected Base32InputStream.read(buf, 0, -1) to throw IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException e) { in.read(buf, buf.length + 1, 0); fail("Base32InputStream.read(buf, buf.length + 1, 0) throws IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException e) { in.read(buf, buf.length - 1, 2); fail("Base32InputStream.read(buf, buf.length - 1, 2) throws IndexOutOfBoundsException"); } catch (final IndexOutOfBoundsException e) { in.close();
@Override public void process(InputStream in, OutputStream out) throws IOException { try (Base32InputStream bis = new Base32InputStream(new ValidatingBase32InputStream(in))) { StreamUtils.copy(bis, out); } } }
/** * 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_FOO)); final Base32InputStream b32stream = new Base32InputStream(ins); assertEquals(3, b32stream.skip(1024)); // End of stream reached assertEquals(-1, b32stream.read()); assertEquals(-1, b32stream.read()); b32stream.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_FOO)); final Base32InputStream b32stream = new Base32InputStream(ins); assertEquals(1, b32stream.available()); assertEquals(3, b32stream.skip(10)); // End of stream reached assertEquals(0, b32stream.available()); assertEquals(-1, b32stream.read()); assertEquals(-1, b32stream.read()); assertEquals(0, b32stream.available()); b32stream.close(); }