/** * 简单读取Reader的每行内容到List<String> */ public static List<String> toLines(final InputStream input) throws IOException { return CharStreams.readLines(new BufferedReader(new InputStreamReader(input, Charsets.UTF_8))); }
/** * 简单读取Reader的每行内容到List<String> * * @see {@link CharStreams#readLines} */ public static List<String> toLines(final Reader input) throws IOException { return CharStreams.readLines(toBufferedReader(input)); }
/** * 简单读取Reader的每行内容到List<String> * * @see {@link CharStreams#readLines} */ public static List<String> toLines(final Reader input) throws IOException { return CharStreams.readLines(toBufferedReader(input)); }
private static List<Path> getTargetPathsFromSymlink(FileSystem fileSystem, Path symlinkDir) { try { FileStatus[] symlinks = fileSystem.listStatus(symlinkDir, HIDDEN_FILES_PATH_FILTER); List<Path> targets = new ArrayList<>(); for (FileStatus symlink : symlinks) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(fileSystem.open(symlink.getPath()), StandardCharsets.UTF_8))) { CharStreams.readLines(reader).stream() .map(Path::new) .forEach(targets::add); } } return targets; } catch (IOException e) { throw new PrestoException(HIVE_BAD_DATA, "Error parsing symlinks from: " + symlinkDir, e); } }
/** * Returns ciphers from the hard-coded list of "reasonable" default ciphers in {@link #DEFAULT_CIPHERS_LIST_RESOURCE}. * * @return ciphers from the {@link #DEFAULT_CIPHERS_LIST_RESOURCE} */ public static List<String> getBuiltInCipherList() { try (InputStream cipherListStream = SslUtil.class.getResourceAsStream(DEFAULT_CIPHERS_LIST_RESOURCE)) { if (cipherListStream == null) { return Collections.emptyList(); } Reader reader = new InputStreamReader(cipherListStream, StandardCharsets.UTF_8); return CharStreams.readLines(reader); } catch (IOException e) { return Collections.emptyList(); } }
/** * Reads lines of text from this source, processing each line as it is read using the given {@link * LineProcessor processor}. Stops when all lines have been processed or the processor returns * {@code false} and returns the result produced by the processor. * * <p>Like {@link BufferedReader#readLine()}, this method considers a line to be a sequence of * text that is terminated by (but does not include) one of {@code \r\n}, {@code \r} or {@code * \n}. If the source's content does not end in a line termination sequence, it is treated as if * it does. * * @throws IOException if an I/O error occurs while reading from this source or if {@code * processor} throws an {@code IOException} * @since 16.0 */ @Beta @CanIgnoreReturnValue // some processors won't return a useful result public <T> T readLines(LineProcessor<T> processor) throws IOException { checkNotNull(processor); Closer closer = Closer.create(); try { Reader reader = closer.register(openStream()); return CharStreams.readLines(reader, processor); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
public void testReadLines() throws IOException { List<String> lines = CharStreams.readLines(new StringReader("a\nb\nc")); assertEquals(ImmutableList.of("a", "b", "c"), lines); }
private void readExistingEntriesInto(Collection<String> services) { try { FileObject existing = getFiler().getResource(StandardLocation.CLASS_OUTPUT, key.packageName, key.relativeName); FluentIterable.from(CharStreams.readLines(existing.openReader(true))) .filter(Predicates.not(Predicates.contains(SERVICE_FILE_COMMENT_LINE))) .copyInto(services); } catch (Exception ex) { // unable to read existing file } }
/** * Reads lines of text from this source, processing each line as it is read using the given {@link * LineProcessor processor}. Stops when all lines have been processed or the processor returns * {@code false} and returns the result produced by the processor. * * <p>Like {@link BufferedReader#readLine()}, this method considers a line to be a sequence of * text that is terminated by (but does not include) one of {@code \r\n}, {@code \r} or {@code * \n}. If the source's content does not end in a line termination sequence, it is treated as if * it does. * * @throws IOException if an I/O error occurs while reading from this source or if {@code * processor} throws an {@code IOException} * @since 16.0 */ @Beta @CanIgnoreReturnValue // some processors won't return a useful result public <T> T readLines(LineProcessor<T> processor) throws IOException { checkNotNull(processor); Closer closer = Closer.create(); try { Reader reader = closer.register(openStream()); return CharStreams.readLines(reader, processor); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
private JsonParser.Stream newJsonStream(FileInputInputStream in, PluginTask task) throws IOException { InvalidEscapeStringPolicy policy = task.getInvalidEscapeStringPolicy(); switch (policy) { case SKIP: case UNESCAPE: Iterable<CharSource> lines = Lists.transform(CharStreams.readLines(new BufferedReader(new InputStreamReader(in))), invalidEscapeStringFunction(policy)); return new JsonParser().open(new ByteArrayInputStream(CharStreams.toString(CharSource.concat(lines).openStream()).getBytes(StandardCharsets.UTF_8))); case PASSTHROUGH: default: return new JsonParser().open(in); } }
@Test public void incompleteBeta() throws IOException { Splitter onComma = Splitter.on(",").trimResults(); InputSupplier<InputStreamReader> input = Resources.newReaderSupplier(Resources.getResource("beta-test-data.csv"), Charsets.UTF_8); boolean header = true; for (String line : CharStreams.readLines(input)) { if (header) { // skip header = false; } else { Iterable<String> values = onComma.split(line); double alpha = Double.parseDouble(Iterables.get(values, 0)); double beta = Double.parseDouble(Iterables.get(values, 1)); double x = Double.parseDouble(Iterables.get(values, 2)); double ref = Double.parseDouble(Iterables.get(values, 3)); double actual = Gamma.incompleteBeta(alpha, beta, x); assertEquals(alpha + "," + beta + ',' + x, ref, actual, ref * 1.0e-5); } } }
try { Reader reader = closer.register(openStream()); return CharStreams.readLines(reader, processor); } catch (Throwable e) { throw closer.rethrow(e);
@Test public void testDistributionFunctions() throws Exception { InputSupplier<InputStreamReader> input = Resources.newReaderSupplier(Resources.getResource("negative-binomial-test-data.csv"), Charsets.UTF_8); boolean header = true; for (String line : CharStreams.readLines(input)) { if (header) { // skip header = false; } else { Iterable<String> values = onComma.split(line); int k = Integer.parseInt(Iterables.get(values, 0)); double p = Double.parseDouble(Iterables.get(values, 1)); int r = Integer.parseInt(Iterables.get(values, 2)); double density = Double.parseDouble(Iterables.get(values, 3)); double cume = Double.parseDouble(Iterables.get(values, 4)); NegativeBinomial nb = new NegativeBinomial(r, p, RandomUtils.getRandom()); assertEquals("cumulative " + k + ',' + p + ',' + r, cume, nb.cdf(k), cume * 1.0e-5); assertEquals("density " + k + ',' + p + ',' + r, density, nb.pdf(k), density * 1.0e-5); } } }
@Override protected void readContent(Reader reader) throws IOException { coldClasses.addAll(CharStreams.readLines(reader)); } }
/** * Loads legacy warnings list from the file. * @return The lines of the file. */ // TODO(nicksantos): This is a weird API. static Set<String> loadWhitelistedJsWarnings(Reader reader) throws IOException { checkNotNull(reader); Set<String> result = new HashSet<>(); result.addAll(CharStreams.readLines(reader)); return result; }
private List<String> readLines(InputStream is) { try { return CharStreams.readLines(new InputStreamReader(is)); } catch (IOException e) { e.printStackTrace(); // TODO(kalman): this is a bit hacky, deal with it properly. return Collections.singletonList("(Error, couldn't read lines from the input stream. " + "Try running the command external to PST to view the output.)"); } }
private void load(String path, LineProcessor<Void> processor) throws IOException { InputStream in = Emoji.class.getResourceAsStream(path); Preconditions.checkNotNull(in, "%s not found in the classpath!", path); InputStreamReader isr = new InputStreamReader(in, UTF8); BufferedReader br = new BufferedReader(isr); CharStreams.readLines(br, processor); }
private void loadBundlesFromStream(String bundleName, InputStream stream) throws IOException { List<Type> bundleCollection = getOrCreateBundleCollection(bundleName); bundleCollection.addAll( readLines(new InputStreamReader(stream)) .stream() .map(Type::new) .collect(Collectors.toList())); }
private List<String> readFromOutput(FileSet fileSet, String relativePath) throws IOException { // small amount of data, so expect all data from just 1 file Location location = fileSet.getLocation(relativePath).append("part-m-00000"); return CharStreams.readLines(CharStreams.newReaderSupplier(Locations.newInputSupplier(location), Charsets.UTF_8)); }