StreamUtil { public static int eagerRead(final InputStream stream, final byte[] buffer) throws IOException { int read = 0; int offset = 0; int remaining = buffer.length; while (remaining > 0 && (read = stream.read(buffer, offset, remaining)) != -1) { remaining = remaining - read; offset = offset + read; } if (offset == 0) { return -1; } return offset; } private  StreamUtil(); static String streamToString(final InputStream stream); static String streamToString(final InputStream stream, final boolean close); static String streamToString(final InputStream stream, final Charset charset); static String streamToString(final InputStream stream, final Charset charset, final boolean close); static Stream<String> streamToLines(final InputStream stream); static Stream<String> streamToLines(final InputStream stream, final Charset charset, final boolean close); static byte[] streamToBytes(final InputStream stream); static ByteArrayOutputStream streamToBuffer(final InputStream stream); static ByteArrayOutputStream streamToBuffer(final InputStream stream, final boolean close); static void stringToFile(final String string, final Path file); static void stringToFile(final String string, final Path file, final Charset charset); static void stringToStream(final String string, final OutputStream outputStream); static void stringToStream(final String string, final OutputStream outputStream, final Charset charset); static String fileToString(final Path file); static String fileToString(final Path file, final Charset charset); static void streamToFile(final InputStream inputStream, final String outputFileName); static void streamToFile(final InputStream inputStream, final Path file); static void copyFile(final Path fromFile, final Path toFile); static long streamToStream(final InputStream inputStream, final OutputStream outputStream); static InputStream stringToStream(final String string); static InputStream stringToStream(final String string, final Charset charset); static int eagerRead(final InputStream stream, final byte[] buffer); static long skip(final InputStream inputStream, final long totalBytesToSkip); static void fillBuffer(final InputStream stream, final byte[] buffer); static void fillBuffer(final InputStream stream, final byte[] buffer, final int offset, final int len); static OutputStream ignoreClose(final OutputStream outputStream); @SuppressWarnings(value = "DM_DEFAULT_ENCODING") static String exceptionCallStack(final Throwable throwable); static void close(final OutputStream outputStream); static void close(final InputStream inputStream); static ByteSlice getByteSlice(final String string); static final int BUFFER_SIZE; static final Charset DEFAULT_CHARSET; static final String DEFAULT_CHARSET_NAME; static final Locale DEFAULT_LOCALE; }
@Test void testFullRead() throws IOException { byte[] buffer = new byte[10]; InputStream testStream = new TestInputStream(); Assertions.assertThat(StreamUtil.eagerRead(testStream, buffer)).isEqualTo(10); assertThat(StreamUtil.eagerRead(testStream, buffer)).isEqualTo(10); assertThat(StreamUtil.eagerRead(testStream, buffer)).isEqualTo(5); assertThat(StreamUtil.eagerRead(testStream, buffer)).isEqualTo(-1); assertThat(StreamUtil.eagerRead(testStream, buffer)).isEqualTo(-1); }