public void testToByteArray_withSize_givenLargerSize() throws IOException { InputStream in = new ByteArrayInputStream(PRE_FILLED_100); byte[] b = ByteStreams.toByteArray(in, 120); assertEquals(PRE_FILLED_100, b); }
public void testNullOutputStream() throws Exception { // create a null output stream OutputStream nos = ByteStreams.nullOutputStream(); // write to the output stream nos.write('n'); String test = "Test string for NullOutputStream"; nos.write(test.getBytes()); nos.write(test.getBytes(), 2, 10); // nothing really to assert? assertSame(ByteStreams.nullOutputStream(), ByteStreams.nullOutputStream()); }
public void testNewDataInput_readBoolean() { ByteArrayDataInput in = ByteStreams.newDataInput(bytes); assertTrue(in.readBoolean()); }
public void testToByteArray_emptyStream() throws IOException { InputStream in = newTestStream(0); byte[] b = ByteStreams.toByteArray(in); assertEquals(new byte[0], b); }
public void testToByteArray_largeStream() throws IOException { // well, large enough to require multiple buffers byte[] expected = newPreFilledByteArray(10000000); InputStream in = new ByteArrayInputStream(expected); byte[] b = ByteStreams.toByteArray(in); assertEquals(expected, b); }
public void testSkipFully() throws IOException { byte[] bytes = newPreFilledByteArray(100); skipHelper(0, 0, new ByteArrayInputStream(bytes)); skipHelper(50, 50, new ByteArrayInputStream(bytes)); skipHelper(50, 50, new SlowSkipper(new ByteArrayInputStream(bytes), 1)); skipHelper(50, 50, new SlowSkipper(new ByteArrayInputStream(bytes), 0)); skipHelper(100, -1, new ByteArrayInputStream(bytes)); try { skipHelper(101, 0, new ByteArrayInputStream(bytes)); fail("expected exception"); } catch (EOFException e) { } }
public void testNewDataInput_normal() { ByteArrayDataInput in = ByteStreams.newDataInput(bytes); assertEquals(0x12345678, in.readInt()); assertEquals(0x76543210, in.readInt()); try { in.readInt(); fail("expected exception"); } catch (IllegalStateException expected) { } }
ByteStreams.readFully(newTestStream(10), null, 0, 10); fail("expected exception"); } catch (NullPointerException e) { fail("expected exception"); } catch (NullPointerException e) { ByteStreams.readFully(newTestStream(10), b, -1, 10); fail("expected exception"); } catch (IndexOutOfBoundsException e) { ByteStreams.readFully(newTestStream(10), b, 0, -1); fail("expected exception"); } catch (IndexOutOfBoundsException e) { ByteStreams.readFully(newTestStream(10), b, 0, -1); fail("expected exception"); } catch (IndexOutOfBoundsException e) { ByteStreams.readFully(newTestStream(10), b, 2, 10); fail("expected exception"); } catch (IndexOutOfBoundsException e) { ByteStreams.readFully(newTestStream(5), b, 0, 10); fail("expected exception"); } catch (EOFException e) { ByteStreams.readFully(newTestStream(10), b, 0, 0); assertEquals(new byte[10], b);
public void testCopyFileChannel() throws IOException { final int chunkSize = 14407; // Random prime, unlikely to match any internal chunk size ByteArrayOutputStream out = new ByteArrayOutputStream(); WritableByteChannel outChannel = Channels.newChannel(out); File testFile = createTempFile(); FileOutputStream fos = new FileOutputStream(testFile); byte[] dummyData = newPreFilledByteArray(chunkSize); try { for (int i = 0; i < 500; i++) { fos.write(dummyData); } } finally { fos.close(); } ReadableByteChannel inChannel = new RandomAccessFile(testFile, "r").getChannel(); try { ByteStreams.copy(inChannel, outChannel); } finally { inChannel.close(); } byte[] actual = out.toByteArray(); for (int i = 0; i < 500 * chunkSize; i += chunkSize) { assertEquals(dummyData, Arrays.copyOfRange(actual, i, i + chunkSize)); } }
public void testLimit_markNotSet() { byte[] big = newPreFilledByteArray(5); InputStream bin = new ByteArrayInputStream(big); InputStream lin = ByteStreams.limit(bin, 2); try { lin.reset(); fail(); } catch (IOException expected) { assertThat(expected).hasMessageThat().isEqualTo("Mark not set"); } }
public void testNewDataInput_empty() { byte[] b = new byte[0]; ByteArrayDataInput in = ByteStreams.newDataInput(b); try { in.readInt(); fail("expected exception"); } catch (IllegalStateException expected) { } }
private static InputStream newTestStream(int n) { return new ByteArrayInputStream(newPreFilledByteArray(n)); }
public void testCopyChannel() throws IOException { byte[] expected = newPreFilledByteArray(100); ByteArrayOutputStream out = new ByteArrayOutputStream(); WritableByteChannel outChannel = Channels.newChannel(out); ReadableByteChannel inChannel = Channels.newChannel(new ByteArrayInputStream(expected)); ByteStreams.copy(inChannel, outChannel); assertEquals(expected, out.toByteArray()); }
public void testNewDataInput_offset() { ByteArrayDataInput in = ByteStreams.newDataInput(bytes, 2); assertEquals(0x56787654, in.readInt()); try { in.readInt(); fail("expected exception"); } catch (IllegalStateException expected) { } }
ByteStreams.readFully(newTestStream(10), null, 0, 10); fail("expected exception"); } catch (NullPointerException e) { fail("expected exception"); } catch (NullPointerException e) { ByteStreams.readFully(newTestStream(10), b, -1, 10); fail("expected exception"); } catch (IndexOutOfBoundsException e) { ByteStreams.readFully(newTestStream(10), b, 0, -1); fail("expected exception"); } catch (IndexOutOfBoundsException e) { ByteStreams.readFully(newTestStream(10), b, 0, -1); fail("expected exception"); } catch (IndexOutOfBoundsException e) { ByteStreams.readFully(newTestStream(10), b, 2, 10); fail("expected exception"); } catch (IndexOutOfBoundsException e) { ByteStreams.readFully(newTestStream(5), b, 0, 10); fail("expected exception"); } catch (EOFException e) { ByteStreams.readFully(newTestStream(10), b, 0, 0); assertEquals(new byte[10], b);
public void testExhaust() throws IOException { InputStream in = newTestStream(100); assertEquals(100, ByteStreams.exhaust(in)); assertEquals(-1, in.read()); assertEquals(0, ByteStreams.exhaust(in)); InputStream empty = newTestStream(0); assertEquals(0, ByteStreams.exhaust(empty)); assertEquals(-1, empty.read()); }
public void testSkipFully() throws IOException { byte[] bytes = newPreFilledByteArray(100); skipHelper(0, 0, new ByteArrayInputStream(bytes)); skipHelper(50, 50, new ByteArrayInputStream(bytes)); skipHelper(50, 50, new SlowSkipper(new ByteArrayInputStream(bytes), 1)); skipHelper(50, 50, new SlowSkipper(new ByteArrayInputStream(bytes), 0)); skipHelper(100, -1, new ByteArrayInputStream(bytes)); try { skipHelper(101, 0, new ByteArrayInputStream(bytes)); fail("expected exception"); } catch (EOFException e) { } }
public void testCopyFileChannel() throws IOException { final int chunkSize = 14407; // Random prime, unlikely to match any internal chunk size ByteArrayOutputStream out = new ByteArrayOutputStream(); WritableByteChannel outChannel = Channels.newChannel(out); File testFile = createTempFile(); FileOutputStream fos = new FileOutputStream(testFile); byte[] dummyData = newPreFilledByteArray(chunkSize); try { for (int i = 0; i < 500; i++) { fos.write(dummyData); } } finally { fos.close(); } ReadableByteChannel inChannel = new RandomAccessFile(testFile, "r").getChannel(); try { ByteStreams.copy(inChannel, outChannel); } finally { inChannel.close(); } byte[] actual = out.toByteArray(); for (int i = 0; i < 500 * chunkSize; i += chunkSize) { assertEquals(dummyData, Arrays.copyOfRange(actual, i, i + chunkSize)); } }
public void testLimit_markNotSet() { byte[] big = newPreFilledByteArray(5); InputStream bin = new ByteArrayInputStream(big); InputStream lin = ByteStreams.limit(bin, 2); try { lin.reset(); fail(); } catch (IOException expected) { assertThat(expected).hasMessage("Mark not set"); } }
public void testLimit_markNotSupported() { InputStream lin = ByteStreams.limit(new UnmarkableInputStream(), 2); try { lin.reset(); fail(); } catch (IOException expected) { assertThat(expected).hasMessageThat().isEqualTo("Mark not supported"); } }