/** * Get the contents of an <code>InputStream</code> as a list of Strings, * one entry per line, using the default character encoding of the platform. * <p> * This method buffers the input internally, so there is no need to use a * <code>BufferedInputStream</code>. * * @param input the <code>InputStream</code> to read from, not null * @return the list of Strings, never null * @throws NullPointerException if the input is null * @throws IOException if an I/O error occurs * @since Commons IO 1.1 */ public static List<String> readLines(InputStream input) throws IOException { InputStreamReader reader = new InputStreamReader(input, UTF_8); return readLines(reader); }
/** * Get the contents of an <code>InputStream</code> as a list of Strings, * one entry per line, using the specified character encoding. * <p> * Character encoding names can be found at * <a href="http://www.iana.org/assignments/character-sets">IANA</a>. * <p> * This method buffers the input internally, so there is no need to use a * <code>BufferedInputStream</code>. * * @param input the <code>InputStream</code> to read from, not null * @param encoding the encoding to use, null means platform default * @return the list of Strings, never null * @throws NullPointerException if the input is null * @throws IOException if an I/O error occurs * @since Commons IO 1.1 */ public static List<String> readLines(InputStream input, String encoding) throws IOException { if (encoding == null) { return readLines(input); } else { InputStreamReader reader = new InputStreamReader(input, encoding); return readLines(reader); } }
private Map<String, String> getTestLanguages(String resourceName) throws IOException { Map<String, String> result = new HashMap<>(); List<String> languages = IOUtils.readLines(OptimaizeLangDetectorTest.class.getResourceAsStream(resourceName)); for (String line : languages) { line = line.trim(); if (line.isEmpty() || line.startsWith("#")) { continue; } String[] pieces = line.split("\t", 2); if (pieces.length != 2) { throw new IllegalArgumentException("Invalid language data line: " + line); } result.put(pieces[0], pieces[1]); } return result; }
private void findAndValidate(MuleArtifactClassLoader classLoader, String request, String resourceName, URL expectedArtifactLocation, String expectedLine) throws IOException { URL resource = classLoader.findResource(request + ":" + resourceName); assertThat(resource, is(notNullValue())); assertThat(resource, is(equalTo(new URL("jar:" + expectedArtifactLocation.toString() + "!/" + resourceName)))); assertThat(readLines(resource.openStream()).get(0), is(expectedLine)); }
protected String[] getTestLanguages() throws IOException { List<String> result = new ArrayList<>(); List<String> lines = IOUtils.readLines(LanguageDetectorTest.class.getResourceAsStream("language-codes.txt")); for (String line : lines) { line = line.trim(); if (line.isEmpty() || line.startsWith("#")) { continue; } String[] parsed = line.split("\t"); String language = parsed[0]; if (hasTestLanguage(language)) { result.add(language); } } return result.toArray(new String[result.size()]); }
/** * Get the contents of an <code>InputStream</code> as a list of Strings, * one entry per line, using the default character encoding of the platform. * <p> * This method buffers the input internally, so there is no need to use a * <code>BufferedInputStream</code>. * * @param input the <code>InputStream</code> to read from, not null * @return the list of Strings, never null * @throws NullPointerException if the input is null * @throws IOException if an I/O error occurs * @since Commons IO 1.1 */ public static List<String> readLines(InputStream input) throws IOException { InputStreamReader reader = new InputStreamReader(input, UTF_8); return readLines(reader); }
@Test public void test() throws Exception { assumeTrue(TextLangDetector.canRun()); LanguageDetector detector = new TextLangDetector(); LanguageWriter writer = new LanguageWriter(detector); List<String> lines = IOUtils.readLines(TextLangDetectorTest.class.getResourceAsStream("text-test.tsv")); for (String line : lines) { String[] data = line.split("\t"); if (data.length != 2) continue; writer.reset(); writer.append(data[1]); LanguageResult result = detector.detect(); assertNotNull(result); assertEquals(data[0], result.getLanguage()); } writer.close(); } }
/** * Get the contents of an <code>InputStream</code> as a list of Strings, * one entry per line, using the specified character encoding. * <p> * Character encoding names can be found at * <a href="http://www.iana.org/assignments/character-sets">IANA</a>. * <p> * This method buffers the input internally, so there is no need to use a * <code>BufferedInputStream</code>. * * @param input the <code>InputStream</code> to read from, not null * @param encoding the encoding to use, null means platform default * @return the list of Strings, never null * @throws NullPointerException if the input is null * @throws IOException if an I/O error occurs * @since Commons IO 1.1 */ public static List<String> readLines(InputStream input, String encoding) throws IOException { if (encoding == null) { return readLines(input); } else { InputStreamReader reader = new InputStreamReader(input, encoding); return readLines(reader); } }
/** * Get the contents of an <code>InputStream</code> as a list of Strings, * one entry per line, using the default character encoding of the platform. * <p> * This method buffers the input internally, so there is no need to use a * <code>BufferedInputStream</code>. * * @param input the <code>InputStream</code> to read from, not null * @return the list of Strings, never null * @throws NullPointerException if the input is null * @throws IOException if an I/O error occurs * @since Commons IO 1.1 */ public static List<String> readLines(InputStream input) throws IOException { InputStreamReader reader = new InputStreamReader(input, UTF_8); return readLines(reader); }
/** * Get the contents of an <code>InputStream</code> as a list of Strings, * one entry per line, using the specified character encoding. * <p> * Character encoding names can be found at * <a href="http://www.iana.org/assignments/character-sets">IANA</a>. * <p> * This method buffers the input internally, so there is no need to use a * <code>BufferedInputStream</code>. * * @param input the <code>InputStream</code> to read from, not null * @param encoding the encoding to use, null means platform default * @return the list of Strings, never null * @throws NullPointerException if the input is null * @throws IOException if an I/O error occurs * @since Commons IO 1.1 */ public static List<String> readLines(InputStream input, String encoding) throws IOException { if (encoding == null) { return readLines(input); } else { InputStreamReader reader = new InputStreamReader(input, encoding); return readLines(reader); } }
@Test public void testLanguageDetection() throws Exception { LanguageDetector detector = new Lingo24LangDetector(); assumeTrue(((Lingo24LangDetector) detector).isAvailable()); LanguageWriter writer = new LanguageWriter(detector); // Reusing the test data from OptimaizeLangDetectorTest List<String> lines = IOUtils.readLines(Lingo24LangDetectorTest.class.getResourceAsStream("text-test.tsv")); for (String line : lines) { String[] data = line.split("\t"); if (data.length != 2) continue; writer.reset(); writer.append(data[1]); // Only check supported languages if (detector.hasModel(data[0])) { LanguageResult result = detector.detect(); assertNotNull(result); assertEquals(data[0], result.getLanguage()); } } writer.close(); } }