public void testSlice() throws IOException { // Test preconditions try { source.slice(-1, 10); fail(); } catch (IllegalArgumentException expected) { } try { source.slice(0, -1); fail(); } catch (IllegalArgumentException expected) { } assertCorrectSlice(0, 0, 0, 0); assertCorrectSlice(0, 0, 1, 0); assertCorrectSlice(100, 0, 10, 10); assertCorrectSlice(100, 0, 100, 100); assertCorrectSlice(100, 5, 10, 10); assertCorrectSlice(100, 5, 100, 95); assertCorrectSlice(100, 100, 0, 0); assertCorrectSlice(100, 100, 10, 0); assertCorrectSlice(100, 101, 10, 0); }
try { for (ByteSource in : BROKEN_SOURCES) { runFailureTest(in, newNormalByteSink()); assertTrue(logHandler.getStoredLogRecords().isEmpty()); runFailureTest(in, BROKEN_CLOSE_SINK); assertEquals((in == BROKEN_OPEN_SOURCE) ? 0 : 1, getAndResetRecords(logHandler)); runFailureTest(newNormalByteSource(), out); assertTrue(logHandler.getStoredLogRecords().isEmpty()); runFailureTest(BROKEN_CLOSE_SOURCE, out); assertEquals(1, getAndResetRecords(logHandler)); runFailureTest(in, out); assertTrue(getAndResetRecords(logHandler) <= 1); int suppressed = runSuppressionFailureTest(in, newNormalByteSink()); assertEquals(0, suppressed); suppressed = runSuppressionFailureTest(in, BROKEN_CLOSE_SINK); assertEquals((in == BROKEN_OPEN_SOURCE) ? 0 : 1, suppressed); int suppressed = runSuppressionFailureTest(newNormalByteSource(), out); assertEquals(0, suppressed); suppressed = runSuppressionFailureTest(BROKEN_CLOSE_SOURCE, out); assertEquals(1, suppressed);
public void testCopyTo_byteSink() throws IOException { TestByteSink sink = new TestByteSink(); assertFalse(sink.wasStreamOpened() || sink.wasStreamClosed()); assertEquals(bytes.length, source.copyTo(sink)); assertTrue(source.wasStreamOpened() && source.wasStreamClosed()); assertTrue(sink.wasStreamOpened() && sink.wasStreamClosed()); assertArrayEquals(bytes, sink.getBytes()); }
public void testOpenBufferedStream() throws IOException { InputStream in = source.openBufferedStream(); assertTrue(source.wasStreamOpened()); assertFalse(source.wasStreamClosed()); ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteStreams.copy(in, out); in.close(); out.close(); assertTrue(source.wasStreamClosed()); assertArrayEquals(bytes, out.toByteArray()); }
public void testClosesOnErrors_whenReadThrows() { TestByteSource failSource = new TestByteSource(bytes, READ_THROWS); try { failSource.copyTo(new TestByteSink()); fail(); } catch (IOException expected) { } assertTrue(failSource.wasStreamClosed()); }
public void testContentEquals() throws IOException { assertTrue(source.contentEquals(source)); assertTrue(source.wasStreamOpened() && source.wasStreamClosed()); ByteSource equalSource = new TestByteSource(bytes); assertTrue(source.contentEquals(equalSource)); assertTrue(new TestByteSource(bytes).contentEquals(source)); ByteSource fewerBytes = new TestByteSource(newPreFilledByteArray(bytes.length / 2)); assertFalse(source.contentEquals(fewerBytes)); byte[] copy = bytes.clone(); copy[9876] = 1; ByteSource oneByteOff = new TestByteSource(copy); assertFalse(source.contentEquals(oneByteOff)); }
public void testCopyTo_outputStream() throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); assertEquals(bytes.length, source.copyTo(out)); assertTrue(source.wasStreamOpened() && source.wasStreamClosed()); assertArrayEquals(bytes, out.toByteArray()); }
/** * Tests that the default slice() behavior is correct when the source is sliced starting at an * offset that is greater than the current length of the source, a stream is then opened to that * source, and finally additional bytes are appended to the source before the stream is read. * * <p>Without special handling, it's possible to have reads of the open stream start <i>before</i> * the offset at which the slice is supposed to start. */ // TODO(cgdecker): Maybe add a test for this to ByteSourceTester public void testSlice_appendingAfterSlicing() throws IOException { // Source of length 5 AppendableByteSource source = new AppendableByteSource(newPreFilledByteArray(5)); // Slice it starting at offset 10. ByteSource slice = source.slice(10, 5); // Open a stream to the slice. InputStream in = slice.openStream(); // Append 10 more bytes to the source. source.append(newPreFilledByteArray(5, 10)); // The stream reports no bytes... importantly, it doesn't read the byte at index 5 when it // should be reading the byte at index 10. // We could use a custom InputStream instead to make the read start at index 10, but since this // is a racy situation anyway, this behavior seems reasonable. assertEquals(-1, in.read()); }
public void testRead_withProcessor_stopsOnFalse() throws IOException { ByteProcessor<Void> processor = new ByteProcessor<Void>() { boolean firstCall = true; @Override public boolean processBytes(byte[] buf, int off, int len) throws IOException { assertTrue("consume() called twice", firstCall); firstCall = false; return false; } @Override public Void getResult() { return null; } }; source.read(processor); assertTrue(source.wasStreamOpened() && source.wasStreamClosed()); }
private static void runFailureTest(ByteSource in, ByteSink out) { try { in.copyTo(out); fail(); } catch (IOException expected) { } }
public void testCopyToStream_doesNotCloseThatStream() throws IOException { TestOutputStream out = new TestOutputStream(ByteStreams.nullOutputStream()); assertFalse(out.closed()); source.copyTo(out); assertFalse(out.closed()); }
public void testHash() throws IOException { ByteSource byteSource = new TestByteSource("hamburger\n".getBytes(Charsets.US_ASCII)); // Pasted this expected string from `echo hamburger | md5sum` assertEquals("cfa0c5002275c90508338a5cdb2a9781", byteSource.hash(Hashing.md5()).toString()); }
/** * @param input the size of the input source * @param offset the first argument to {@link ByteSource#slice} * @param length the second argument to {@link ByteSource#slice} * @param expectRead the number of bytes we expect to read */ private static void assertCorrectSlice(int input, int offset, long length, int expectRead) throws IOException { checkArgument(expectRead == (int) Math.max(0, Math.min(input, offset + length) - offset)); byte[] expected = newPreFilledByteArray(offset, expectRead); ByteSource source = new TestByteSource(newPreFilledByteArray(input)); ByteSource slice = source.slice(offset, length); assertArrayEquals(expected, slice.read()); }
public void testClosesOnErrors_copyingToOutputStreamThatThrows() { TestByteSource okSource = new TestByteSource(bytes); try { OutputStream out = new TestOutputStream(ByteStreams.nullOutputStream(), WRITE_THROWS); okSource.copyTo(out); fail(); } catch (IOException expected) { } assertTrue(okSource.wasStreamClosed()); }
public void testContentEquals() throws IOException { assertTrue(source.contentEquals(source)); assertTrue(source.wasStreamOpened() && source.wasStreamClosed()); ByteSource equalSource = new TestByteSource(bytes); assertTrue(source.contentEquals(equalSource)); assertTrue(new TestByteSource(bytes).contentEquals(source)); ByteSource fewerBytes = new TestByteSource(newPreFilledByteArray(bytes.length / 2)); assertFalse(source.contentEquals(fewerBytes)); byte[] copy = bytes.clone(); copy[9876] = 1; ByteSource oneByteOff = new TestByteSource(copy); assertFalse(source.contentEquals(oneByteOff)); }
public void testSize() throws IOException { assertEquals(bytes.length, source.size()); assertTrue(source.wasStreamOpened() && source.wasStreamClosed()); // test that we can get the size even if skip() isn't supported assertEquals(bytes.length, new TestByteSource(bytes, SKIP_THROWS).size()); // test that we can get the size even if available() always returns zero assertEquals(bytes.length, new TestByteSource(bytes, AVAILABLE_ALWAYS_ZERO).size()); }
public void testOpenBufferedStream() throws IOException { InputStream in = source.openBufferedStream(); assertTrue(source.wasStreamOpened()); assertFalse(source.wasStreamClosed()); ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteStreams.copy(in, out); in.close(); out.close(); assertTrue(source.wasStreamClosed()); assertArrayEquals(bytes, out.toByteArray()); }
/** * Tests that the default slice() behavior is correct when the source is sliced starting at an * offset that is greater than the current length of the source, a stream is then opened to that * source, and finally additional bytes are appended to the source before the stream is read. * * <p>Without special handling, it's possible to have reads of the open stream start <i>before</i> * the offset at which the slice is supposed to start. */ // TODO(cgdecker): Maybe add a test for this to ByteSourceTester public void testSlice_appendingAfterSlicing() throws IOException { // Source of length 5 AppendableByteSource source = new AppendableByteSource(newPreFilledByteArray(5)); // Slice it starting at offset 10. ByteSource slice = source.slice(10, 5); // Open a stream to the slice. InputStream in = slice.openStream(); // Append 10 more bytes to the source. source.append(newPreFilledByteArray(5, 10)); // The stream reports no bytes... importantly, it doesn't read the byte at index 5 when it // should be reading the byte at index 10. // We could use a custom InputStream instead to make the read start at index 10, but since this // is a racy situation anyway, this behavior seems reasonable. assertEquals(-1, in.read()); }
public void testRead_toArray() throws IOException { assertArrayEquals(bytes, source.read()); assertTrue(source.wasStreamOpened() && source.wasStreamClosed()); }
/** @return the number of exceptions that were suppressed on the expected thrown exception */ private static int runSuppressionFailureTest(ByteSource in, ByteSink out) { try { in.copyTo(out); fail(); } catch (IOException expected) { return CloserTest.getSuppressed(expected).length; } throw new AssertionError(); // can't happen }