/** * @see #readChars(InputStream, int) */ public static char[] readChars(final InputStream input) throws IOException { return readChars(input, ALL); }
/** * @see #readChars(Reader, int) */ public static char[] readChars(final Reader input) throws IOException { return readChars(input, ALL); }
/** * @see #readChars(InputStream, String, int) */ public static char[] readChars(final InputStream input, final String encoding) throws IOException { return readChars(input, encoding, ALL); }
/** * Downloads resource as String. */ public static String downloadString(String url, String encoding) throws IOException { InputStream inputStream = new URL(url).openStream(); return new String(StreamUtil.readChars(inputStream, encoding)); }
/** * Downloads resource as String. */ public static String downloadString(String url) throws IOException { InputStream inputStream = new URL(url).openStream(); return new String(StreamUtil.readChars(inputStream)); }
/** * Downloads resource as String. */ public static String downloadString(final String url, final String encoding) throws IOException { try (InputStream inputStream = new URL(url).openStream()) { return new String(StreamUtil.readChars(inputStream, encoding)); } }
/** * Downloads resource as String. */ public static String downloadString(final String url) throws IOException { try (InputStream inputStream = new URL(url).openStream()) { return new String(StreamUtil.readChars(inputStream)); } }
/** * @see #readChars(InputStream, String, int) */ public static char[] readChars(final InputStream input, final int count) throws IOException { return readChars(input, encoding(), count); }
/** * Reads file content as char array. * * @param file {@link File} to read. * @param encoding Encoding to use. * @return array of characters. * @throws IOException if something went wrong. */ public static char[] readChars(final File file, final String encoding) throws IOException { checkExists(file); checkIsFile(file); InputStream in = streamOf(file, encoding); try { return StreamUtil.readChars(in, encoding); } finally { StreamUtil.close(in); } }
@Test void testReadChars_InputStream(TestInfo testInfo) throws Exception { final String text = "jodd - Get things done!" + System.lineSeparator(); final char[] expected = text.toCharArray(); final File file = new File(BASE_DIR, testInfo.getTestMethod().get().getName()); FileUtil.writeString(file, text, "UTF-8"); char[] actual = null; try (FileInputStream inputStream = new FileInputStream(file)) { actual = StreamUtil.readChars(inputStream); } // asserts assertNotNull(actual); assertArrayEquals(expected, actual); }
@ParameterizedTest @MethodSource("testdata_testReadChars_InputStream_Encoding_CharCount") void testReadChars_InputStream_Encoding_CharCount(char[] expected, String text, String encoding, int charCount) throws Exception { char[] actual = null; try (ByteArrayInputStream inputStream = new ByteArrayInputStream(text.getBytes())) { actual = StreamUtil.readChars(inputStream, encoding, charCount); } // asserts assertNotNull(actual); assertArrayEquals(expected, actual); }
/** * Reads UTF file content as char array. * * @param file {@link File} to read. * @return array of characters. * @throws IOException if something went wrong. */ public static char[] readUTFChars(final File file) throws IOException { checkExists(file); checkIsFile(file); UnicodeInputStream in = unicodeInputStreamOf(file); try { return StreamUtil.readChars(in, detectEncoding(in)); } finally { StreamUtil.close(in); } }
@ParameterizedTest @MethodSource("testdata_testReadChars_InputStream_Encoding") void testReadChars_InputStream_Encoding(char[] expected, String text, String encoding) throws Exception { char[] actual = null; try (ByteArrayInputStream inputStream = new ByteArrayInputStream(text.getBytes())) { actual = StreamUtil.readChars(inputStream, encoding); } // asserts assertNotNull(actual); assertArrayEquals(expected, actual); }
@ParameterizedTest @MethodSource("testdata_testReadChars_Reader") void testReadChars_Reader(char[] expected, String text) throws Exception { char[] actual = null; try (StringReader reader = new StringReader(text)) { actual = StreamUtil.readChars(reader); } // asserts assertNotNull(actual); assertArrayEquals(expected, actual); }
@ParameterizedTest @MethodSource("testdata_testReadChars_Reader_CharCount") void testReadChars_Reader_CharCount(char[] expected, String text, int charCount) throws Exception { char[] actual = null; try (StringReader reader = new StringReader(text)) { actual = StreamUtil.readChars(reader, charCount); } // asserts assertNotNull(actual); assertArrayEquals(expected, actual); }
@ParameterizedTest @MethodSource("testdata_testReadChars_InputStream_CharCount") void testReadChars_InputStream_CharCount(char[] expected, String text, int charCount, TestInfo testInfo ) throws Exception { final int random = MathUtil.randomInt(1, 2500); final File file = new File(BASE_DIR, testInfo.getTestMethod().get().getName() + "." + random); FileUtil.writeString(file, text, "UTF-8"); char[] actual = null; try (FileInputStream inputStream = new FileInputStream(file)) { actual = StreamUtil.readChars(inputStream, charCount); } // asserts assertNotNull(actual); assertArrayEquals(expected, actual); }
/** * @see #readChars(InputStream, int) */ public static char[] readChars(final InputStream input) throws IOException { return readChars(input, ALL); }
/** * @see #readChars(Reader, int) */ public static char[] readChars(final Reader input) throws IOException { return readChars(input, ALL); }
/** * @see #readChars(InputStream, String, int) */ public static char[] readChars(final InputStream input, final String encoding) throws IOException { return readChars(input, encoding, ALL); }
/** * @see #readChars(InputStream, String, int) */ public static char[] readChars(final InputStream input, final int count) throws IOException { return readChars(input, encoding(), count); }