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