public static String readAllBytes(InputStream input, Charset charset) throws IOException {
    if (charset == null) {
      input = ensureMarkSupport(input);
      input.mark(4);
      byte[] buffer = new byte[4];
      int bytesRead = fillBuffer(input, buffer);
      input.reset();

      charset = detectUtfCharset0(buffer, bytesRead);

      if (charset == null) {
        throw new IOException("Unsupported UCS-4 variant (neither UTF-32BE nor UTF32-LE)");
      }
    }
    Reader reader = new InputStreamReader(input, charset);
    return readAllChars(reader);
  }
@Test
  void validateTextConversionFromStreams() throws IOException {
    assertEquals("A",
        UtfTextUtils.readAllBytes(new ByteArrayInputStream(hexBytes("EFBBBF41")), StandardCharsets.UTF_8));
    assertEquals("A", UtfTextUtils.readAllBytes(new ByteArrayInputStream(hexBytes("EFBBBF41")), null));

    assertEquals("A", UtfTextUtils.readAllBytes(new ByteArrayInputStream(hexBytes("41")), StandardCharsets.UTF_8));
    assertEquals("A", UtfTextUtils.readAllBytes(new ByteArrayInputStream(hexBytes("41")), null));

    // Invalid UCS-4 encoding should throw an IOException instead of an
    // IllegalArgumentException.
    assertThrows(IOException.class,
        () -> UtfTextUtils.readAllBytes(new ByteArrayInputStream(hexBytes("0000FFFE")), null));
  }