@Test public void testDrainTo_all() throws Exception { Message m = new Message(); byte[] bytes = IoUtils.toByteArray(marshaller.stream(m)); InputStream is = marshaller.stream(m); Drainable d = (Drainable) is; ByteArrayOutputStream baos = new ByteArrayOutputStream(); int drained = d.drainTo(baos); assertEquals(baos.size(), drained); assertArrayEquals(bytes, baos.toByteArray()); assertEquals(0, is.available()); }
@Test public void testDrainTo_none() throws Exception { Message m = new Message(); byte[] bytes = IoUtils.toByteArray(marshaller.stream(m)); InputStream is = marshaller.stream(m); byte[] unused = IoUtils.toByteArray(is); Drainable d = (Drainable) is; ByteArrayOutputStream baos = new ByteArrayOutputStream(); assertEquals(0, d.drainTo(baos)); assertArrayEquals(new byte[0], baos.toByteArray()); assertEquals(0, is.available()); }
@Test public void testDrainTo_all() throws Exception { byte[] golden = ByteStreams.toByteArray(marshaller.stream(proto)); InputStream is = marshaller.stream(proto); Drainable d = (Drainable) is; ByteArrayOutputStream baos = new ByteArrayOutputStream(); int drained = d.drainTo(baos); assertEquals(baos.size(), drained); assertArrayEquals(golden, baos.toByteArray()); assertEquals(0, is.available()); }
@Test public void testDrainTo_none() throws Exception { InputStream is = marshaller.stream(proto); byte[] unused = ByteStreams.toByteArray(is); Drainable d = (Drainable) is; ByteArrayOutputStream baos = new ByteArrayOutputStream(); assertEquals(0, d.drainTo(baos)); assertArrayEquals(new byte[0], baos.toByteArray()); assertEquals(0, is.available()); }
private static int writeToOutputStream(InputStream message, OutputStream outputStream) throws IOException { if (message instanceof Drainable) { return ((Drainable) message).drainTo(outputStream); } else { // This makes an unnecessary copy of the bytes when bytebuf supports array(). However, we // expect performance-critical code to support flushTo(). long written = IoUtils.copy(message, outputStream); checkArgument(written <= Integer.MAX_VALUE, "Message size overflow: %s", written); return (int) written; } }