public static int tryReadFully(final InputStream in, final byte[] buf) throws IOException {
        int totalRead = 0;
        while (totalRead != buf.length) {
            int read = in.read(buf, totalRead, buf.length - totalRead);
            if (read == -1) {
                break;
            }
            totalRead += read;
        }
        return totalRead;
    }
@Test
    void testTryReadFullyFromShorterStream() throws IOException {
        ByteArrayInputStream inputStream =
                new ByteArrayInputStream("t".getBytes(StandardCharsets.UTF_8));

        byte[] out = new byte[4];
        int read = IOUtils.tryReadFully(inputStream, out);

        assertThat(Arrays.copyOfRange(out, 0, read))
                .containsExactly("t".getBytes(StandardCharsets.UTF_8));
    }