@Test public void closeSource() throws IOException { SplitInputStream split; CloseCountInputStream source; InputStream input; int i; source = new CloseCountInputStream(); split = new SplitInputStream(source, 2); input = split.getStream(0); for (i = split.getStreamCount() + 5; --i >= 0; ) { input.close(); } assertEquals(0, source.getCloseCount()); input = split.getStream(1); input.close(); assertEquals(1, source.getCloseCount()); for (i = split.getStreamCount() + 5; --i >= 0; ) { input.close(); } assertEquals(1, source.getCloseCount()); }
@Test public void overflowAvailable() throws IOException { SplitInputStream split; MaxAvailableInputStream source; InputStream input; source = new MaxAvailableInputStream(); split = new SplitInputStream(source, 1); input = split.getStream(0); assertEquals(0, input.read()); assertEquals(Integer.MAX_VALUE, input.available()); }
@After public void after() throws IOException { InputStream input; int i, j; for (i = m_fixture.getStreamCount(); --i > 0; ) { input = m_fixture.getStream(i); for (j = m_expect.length - input.available(); j < m_expect.length; j++) { assertEquals(m_expect[j] & 0x0FF, input.read()); } assertEquals(-1, input.read()); assertEquals(-1, input.read(new byte[1])); } }
@Test public void ignoreClosedStream() throws IOException, NoSuchFieldException, IllegalAccessException { Field bufferField; int i, length; byte expect[], actual[]; length = 2 * SplitInputStream.INITIAL_BUFFER_SIZE; initialize(length, SplitInputStream.INITIAL_BUFFER_SIZE); m_input.close(); bufferField = SplitInputStream.class.getDeclaredField("m_buffer"); bufferField.setAccessible(true); expect = (byte[]) bufferField.get(m_fixture); m_input = m_fixture.getStream(1); for (i = 0; i < length; i++) { assertEquals(m_expect[i] & 0x0FF, m_input.read()); } actual = (byte[]) bufferField.get(m_fixture); assertSame(expect, actual); }
@Test public void readArrayEveryByteValue() throws IOException { ByteArrayInputStream source; int i, delta; byte actual[]; m_expect = new byte[256]; actual = new byte[256]; for (i = 256; --i >= 0; ) { m_expect[i] = (byte) i; } source = new ByteArrayInputStream(m_expect); m_fixture = new SplitInputStream(source, 2); m_input = m_fixture.getStream(0); for (i = 0; i < 256; i += delta) { delta = m_input.read(actual, i, 256 - i); assertTrue(delta >= 0); } assertArrayEquals(m_expect, actual); }
public void testSplitInputStream() throws Exception { Random random = new Random(); byte[] data = new byte[4096 + random.nextInt(1024)]; random.nextBytes(data); SplitInputStream splitInputStream = new SplitInputStream(new ByteArrayInputStream(data)); ByteArrayOutputStream out = new ByteArrayOutputStream(); while (true) { byte[] s = splitInputStream.readNextSplit(); if (s == null) break; out.write(s); } /* merge the split data should equal to the origin data. */ assertTrue(ArrayUtils.isEquals(data, out.toByteArray())); }
public void initialize(int length, int bufferSize) throws IOException { ByteArrayInputStream source; int i; m_expect = new byte[length]; if (s_random != null) { s_random.nextBytes(m_expect); } else { for (i = m_expect.length; --i >= 0; ) { m_expect[i] = (byte) i; } } source = new ByteArrayInputStream(m_expect); m_fixture = new SplitInputStream(source, 2, bufferSize); m_input = m_fixture.getStream(0); assertEquals(2, m_fixture.getStreamCount()); }
@Test public void availableNeverReads() throws IOException { SplitInputStream split; InputStream source, stream; source = new InputStream() { public int read() { fail(); return (0); } }; split = new SplitInputStream(source, 1); stream = split.getStream(0); assertEquals(0, stream.available()); }
@Test public void readEveryByteValue() throws IOException { ByteArrayInputStream source; int i; m_expect = new byte[256]; for (i = 256; --i >= 0; ) { m_expect[i] = (byte) i; } source = new ByteArrayInputStream(m_expect); m_fixture = new SplitInputStream(source, 2, 256); m_input = m_fixture.getStream(0); for (i = 0; i < 256; i++) { assertEquals(i, m_input.read()); } }
@Test public void wrap() throws IOException { int i, length; length = 2 * SplitInputStream.INITIAL_BUFFER_SIZE; initialize(length, SplitInputStream.INITIAL_BUFFER_SIZE); for (i = 0; i < SplitInputStream.INITIAL_BUFFER_SIZE - 5; i++) { assertEquals(m_expect[i] & 0x0FF, m_input.read()); } m_input = m_fixture.getStream(1); for (i = 0; i < SplitInputStream.INITIAL_BUFFER_SIZE; i++) { assertEquals(m_expect[i] & 0x0FF, m_input.read()); } assertEquals(SplitInputStream.INITIAL_BUFFER_SIZE, m_input.available()); }