Java Code Examples for com.google.common.io.CharStreams#readLines()

The following examples show how to use com.google.common.io.CharStreams#readLines() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FastqParser.java    From biojava with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Parse the specified readable.
 *
 * @param readable readable, must not be null
 * @param listener low-level event based parser callback, must not be null
 * @throws IOException if an I/O error occurs
 */
static void parse(final Readable readable, final ParseListener listener)
	throws IOException
{
	if (readable == null)
	{
		throw new IllegalArgumentException("readable must not be null");
	}
	FastqParserLineProcessor lineProcessor = new FastqParserLineProcessor(listener);
	CharStreams.readLines(readable, lineProcessor);
	if (lineProcessor.getState() == State.COMPLETE)
	{
		listener.complete();
		lineProcessor.setState(State.DESCRIPTION);
	}
	if (lineProcessor.getState() != State.DESCRIPTION)
	{
		throw new IOException("truncated sequence"); // at line " + lineNumber);
	}
}
 
Example 2
Source File: VariableMap.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Deserializes the variable map from a byte array returned by
 * {@link #toBytes()}.
 */
public static VariableMap fromBytes(byte[] bytes) throws ParseException {
  Iterable<String> lines;
  try {
    lines = CharStreams.readLines(CharStreams.newReaderSupplier(
        ByteStreams.newInputStreamSupplier(bytes), Charsets.UTF_8));
  } catch (IOException e) {
    // Note: An IOException is never thrown while reading from a byte array.
    // This try/catch is just here to appease the Java compiler.
    throw new RuntimeException(e);
  }

  ImmutableMap.Builder<String, String> map = ImmutableMap.builder();

  for (String line : lines) {
    int pos = findIndexOfChar(line, SEPARATOR);
    if (pos <= 0 || pos == line.length() - 1) {
      throw new ParseException("Bad line: " + line, 0);
    }
    map.put(
        unescape(line.substring(0, pos)),
        unescape(line.substring(pos + 1)));
  }
  return new VariableMap(map.build());
}
 
Example 3
Source File: GrokDictionaries.java    From kite with Apache License 2.0 6 votes vote down vote up
private void loadDictionary(Reader reader) throws IOException {
  for (String line : CharStreams.readLines(reader)) {
    line = line.trim();
    if (line.length() == 0) {
      continue; // ignore empty lines
    }
    if (line.startsWith("#")) {
      continue; // ignore comment lines
    }
    int i = line.indexOf(" ");
    if (i < 0) {
      throw new MorphlineCompilationException("Dictionary entry line must contain a space to separate name and value: " + line, getConfig());
    }
    if (i == 0) {
      throw new MorphlineCompilationException("Dictionary entry line must contain a name: " + line, getConfig());
    }
    String name = line.substring(0, i);
    String value = line.substring(i + 1, line.length()).trim();
    if (value.length() == 0) {
      throw new MorphlineCompilationException("Dictionary entry line must contain a value: " + line, getConfig());
    }
    dictionary.put(name, value);
  }      
}
 
Example 4
Source File: PlantUmlParser.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
private List<String> readLines(URL url) {
    try (InputStreamReader in = new InputStreamReader(url.openStream(), UTF_8)) {
        return CharStreams.readLines(in);
    } catch (IOException e) {
        throw new PlantUmlParseException("Could not parse diagram from " + url, e);
    }
}
 
Example 5
Source File: PstFileDescriptor.java    From swellrt with Apache License 2.0 5 votes vote down vote up
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.)");
  }
}
 
Example 6
Source File: CanonicalizeLabelsCommand.java    From nomulus with Apache License 2.0 5 votes vote down vote up
@Override
public void run() throws IOException {
  Set<String> labels = new TreeSet<>();
  for (String label :
      mainParameters.isEmpty()
          ? CharStreams.readLines(new InputStreamReader(stdin, UTF_8))
          : Files.readLines(new File(mainParameters.get(0)), UTF_8)) {
    label = label.trim();
    if (label.startsWith("-")) {
      label = label.substring(1);
    }
    if (label.endsWith("-")) {
      label = label.substring(0, label.length() - 1);
    }
    String canonical = canonicalize(label);
    if (canonical.startsWith(DomainNameUtils.ACE_PREFIX)
        && Idn.toUnicode(canonical).equals(canonical)) {
      System.err.println("Bad IDN: " + label);
      continue;  // Bad IDN code points.
    }
    labels.add(canonical);
    if (!canonical.startsWith("xn--")) {
      // Using both "" and "-" to canonicalize labels.
      labels.add(canonicalize(label.replaceAll(" ", "")));
      labels.add(canonicalize(label.replaceAll(" ", "-")));
      labels.add(canonicalize(label.replaceAll("_", "")));
      labels.add(canonicalize(label.replaceAll("_", "-")));
    }
  }
  labels.remove("");  // We used "" for invalid labels.
  System.out.println(Joiner.on('\n').join(labels));
}
 
Example 7
Source File: Booster.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
private GBDT loadGBDT(){
	String handle = getHandle();

	try(StringReader reader = new StringReader(handle)){
		List<String> lines = CharStreams.readLines(reader);

		return LightGBMUtil.loadGBDT(lines.iterator());
	} catch(IOException ioe){
		throw new RuntimeException(ioe);
	}
}
 
Example 8
Source File: SslUtil.java    From Dream-Catcher with MIT License 5 votes vote down vote up
/**
 * 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, Charset.forName("UTF-8"));

        return CharStreams.readLines(reader);
    } catch (IOException e) {
        return Collections.emptyList();
    }
}
 
Example 9
Source File: XGBoostUtil.java    From jpmml-xgboost with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private Iterator<String> parseFeatureMap(InputStream is) throws IOException {
	Reader reader = new InputStreamReader(is, "UTF-8");

	List<String> lines = CharStreams.readLines(reader);

	return lines.iterator();
}
 
Example 10
Source File: PstFileDescriptor.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
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.)");
  }
}
 
Example 11
Source File: VirtualHostLoggerTest.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
private int countLogFileMatches(final String url, final String searchTerm) throws Exception
{
    HttpURLConnection httpCon = getHelper().openManagementConnection(url, "GET");
    httpCon.connect();

    try (InputStreamReader r = new InputStreamReader(httpCon.getInputStream()))
    {
        final List<String> strings = CharStreams.readLines(r);
        return strings.stream().map(line -> line.contains(searchTerm)).collect(Collectors.toList()).size();
    }
}
 
Example 12
Source File: SplitZipStepTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void testMetaList() throws IOException {
  Path outJar = tempDir.newFile("test.jar").toPath();
  Map<String, String> fileToClassName;
  try (ZipOutputStream zipOut =
      new ZipOutputStream(new BufferedOutputStream(Files.newOutputStream(outJar)))) {
    fileToClassName =
        ImmutableMap.of(
            "com/facebook/foo.class", "com.facebook.foo",
            "bar.class", "bar");
    for (String entry : fileToClassName.keySet()) {
      zipOut.putNextEntry(new ZipEntry(entry));
      zipOut.write(new byte[] {0});
    }
  }

  StringWriter stringWriter = new StringWriter();
  try (BufferedWriter writer = new BufferedWriter(stringWriter)) {
    ImmutableSet<APKModule> requires = ImmutableSet.of();
    SplitZipStep.writeMetaList(
        writer,
        APKModule.of(SplitZipStep.SECONDARY_DEX_ID, false),
        requires,
        ImmutableList.of(outJar),
        DexStore.JAR);
  }
  List<String> lines = CharStreams.readLines(new StringReader(stringWriter.toString()));
  assertEquals(1, lines.size());

  String line = Iterables.getLast(lines, null);
  String[] data = line.split(" ");
  assertEquals(3, data.length);

  // Note that we cannot test data[1] (the hash) because zip files change their hash each
  // time they are written due to timestamps written into the file.
  assertEquals("secondary-1.dex.jar", data[0]);
  assertTrue(
      String.format("Unexpected class: %s", data[2]), fileToClassName.values().contains(data[2]));
}
 
Example 13
Source File: LightGBMUtil.java    From jpmml-lightgbm with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public Iterator<String> parseText(InputStream is) throws IOException {
	Reader reader = new InputStreamReader(is, "US-ASCII");

	List<String> lines = CharStreams.readLines(reader);

	return lines.iterator();
}
 
Example 14
Source File: CountVectorizer.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private List<String> loadStopWords(String stopWords){
	InputStream is = CountVectorizer.class.getResourceAsStream("/stop_words/" + stopWords + ".txt");

	if(is == null){
		throw new IllegalArgumentException(stopWords);
	}

	try(Reader reader = new InputStreamReader(is, "UTF-8")){
		return CharStreams.readLines(reader);
	} catch(IOException ioe){
		throw new IllegalArgumentException(stopWords, ioe);
	}
}
 
Example 15
Source File: AsynchronousProcessListWriterTest.java    From everrest with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void writesListOfAsynchronousProcessAsPlainText() throws Exception {
    List<AsynchronousProcess> processes = newArrayList(new AsynchronousProcess("andrew", 1L, "/a", "running"),
                                                       new AsynchronousProcess("user", 2L, "/b", "done"));

    List<List<String>> expectedProcessesTable = newArrayList(newArrayList("USER", "ID", "STAT", "PATH"),
                                                             newArrayList("andrew", "1", "running", "/a"),
                                                             newArrayList("user", "2", "done", "/b"));

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    processListWriter.writeTo(processes, List.class,
                              newParameterizedType(List.class, AsynchronousProcess.class),
                              new Annotation[0],
                              MediaType.TEXT_PLAIN_TYPE,
                              new MultivaluedHashMap<>(),
                              bout);

    List<String> lines = CharStreams.readLines(new StringReader(bout.toString()));
    assertEquals(3, lines.size());

    Pattern pattern = Pattern.compile("(\\w+)\\s+(\\w+)\\s+(\\w+)\\s+(/?\\w+)");

    List<List<String>> processesTable = newArrayList();
    for (String line : lines) {
        Matcher matcher = pattern.matcher(line);
        assertTrue(String.format("String '%s' is not matched to pattern", line), matcher.matches());
        processesTable.add(getAllGroups(matcher));
    }

    assertEquals(expectedProcessesTable, processesTable);
}
 
Example 16
Source File: Symbols.java    From buck with Apache License 2.0 5 votes vote down vote up
private static void runObjdump(
    ProcessExecutor executor,
    Tool objdump,
    SourcePathResolverAdapter resolver,
    Path lib,
    ImmutableList<String> flags,
    LineProcessor<Unit> lineProcessor)
    throws IOException, InterruptedException {
  ImmutableList<String> args =
      ImmutableList.<String>builder()
          .addAll(objdump.getCommandPrefix(resolver))
          .addAll(flags)
          .add(lib.toString())
          .build();

  ProcessExecutorParams params =
      ProcessExecutorParams.builder()
          .setCommand(args)
          .setRedirectError(ProcessBuilder.Redirect.INHERIT)
          .build();
  ProcessExecutor.LaunchedProcess p = executor.launchProcess(params);
  BufferedReader output = new BufferedReader(new InputStreamReader(p.getStdout()));
  CharStreams.readLines(output, lineProcessor);
  ProcessExecutor.Result result = executor.waitForLaunchedProcess(p);

  if (result.getExitCode() != 0) {
    throw new RuntimeException(result.getMessageForUnexpectedResult("Objdump"));
  }
}
 
Example 17
Source File: PlatformCommand.java    From YUNoMakeGoodMap with Apache License 2.0 5 votes vote down vote up
protected List<ResourceLocation> getPlatforms()
{
    if (platforms.size() == 0)
    {
        for (ModContainer mc : Loader.instance().getModList())
        {
            File src = mc.getSource();
            if (src == null)
                continue;

            InputStream is = getClass().getResourceAsStream("/assets/" + mc.getModId() + "/structures/sky_block_platforms.txt");
            if (is == null)
                continue;
            try
            {
                for (String line : CharStreams.readLines(new InputStreamReader(is)))
                {
                    if (getClass().getResourceAsStream("/assets/" + mc.getModId() + "/structures/" + line + ".nbt") != null)
                        platforms.add(new ResourceLocation(mc.getModId(), line));
                }
            } catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        for (File f : YUNoMakeGoodMap.instance.getStructFolder().listFiles())
        {
            if (!f.isFile() || !f.getName().endsWith(".nbt"))
                continue;
            platforms.add(new ResourceLocation("/config/", f.getName().substring(0, f.getName().length() - 4)));
        }
    }

    return platforms;
}
 
Example 18
Source File: IOUtil.java    From vjtools with Apache License 2.0 4 votes vote down vote up
/**
 * 简单读取Reader的每行内容到List<String>
 */
public static List<String> toLines(final InputStream input) throws IOException {
	return CharStreams.readLines(new BufferedReader(new InputStreamReader(input, Charsets.UTF_8)));
}
 
Example 19
Source File: IOUtil.java    From vjtools with Apache License 2.0 4 votes vote down vote up
/**
 * 简单读取Reader的每行内容到List<String>
 */
public static List<String> toLines(final InputStream input) throws IOException {
	return CharStreams.readLines(new BufferedReader(new InputStreamReader(input, Charsets.UTF_8)));
}
 
Example 20
Source File: IOUtil.java    From vjtools with Apache License 2.0 2 votes vote down vote up
/**
 * 简单读取Reader的每行内容到List<String>
 * 
 * @see {@link CharStreams#readLines}
 */
public static List<String> toLines(final Reader input) throws IOException {
	return CharStreams.readLines(toBufferedReader(input));
}