IOUtils { public static byte[] readInputStream(InputStream inputStream) throws IOException { byte[] buf = new byte[BUFFER_SIZE]; ByteArrayOutputStream out = new ByteArrayOutputStream(); int n; while ((n = inputStream.read(buf)) >= 0) { out.write(buf, 0, n); } return out.toByteArray(); }  static byte[] readInputStream(InputStream inputStream);  }
@Test public void testByteBufToString() throws IOException { ByteBuf byteBuf = Unpooled.buffer(16); byteBuf.writeInt(123); byteBuf.writeInt(456); ByteBufInputStream inputStream = new ByteBufInputStream(byteBuf); byte[] bytes = IOUtils.readInputStream(inputStream); Assert.assertEquals(8, bytes.length); ByteBuffer buf = ByteBuffer.wrap(bytes); Assert.assertEquals(123, buf.getInt(0)); Assert.assertEquals(456, buf.getInt(4)); }