com.google.common.io.CharStreams#readLines ( )源码实例Demo

下面列出了com.google.common.io.CharStreams#readLines ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: astor   文件: VariableMap.java
/**
 * 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());
}
 
源代码2 项目: kite   文件: GrokDictionaries.java
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);
  }      
}
 
源代码3 项目: biojava   文件: FastqParser.java
/**
 * 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);
	}
}
 
源代码4 项目: buck   文件: SplitZipStepTest.java
@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]));
}
 
源代码5 项目: YUNoMakeGoodMap   文件: PlatformCommand.java
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;
}
 
源代码6 项目: buck   文件: Symbols.java
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"));
  }
}
 
@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);
}
 
源代码8 项目: jpmml-sklearn   文件: CountVectorizer.java
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);
	}
}
 
源代码9 项目: jpmml-lightgbm   文件: LightGBMUtil.java
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();
}
 
源代码10 项目: ArchUnit   文件: PlantUmlParser.java
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);
    }
}
 
源代码11 项目: qpid-broker-j   文件: VirtualHostLoggerTest.java
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();
    }
}
 
源代码12 项目: incubator-retired-wave   文件: PstFileDescriptor.java
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.)");
  }
}
 
源代码13 项目: jpmml-xgboost   文件: XGBoostUtil.java
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();
}
 
源代码14 项目: Dream-Catcher   文件: SslUtil.java
/**
 * 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();
    }
}
 
源代码15 项目: jpmml-sklearn   文件: Booster.java
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);
	}
}
 
源代码16 项目: nomulus   文件: CanonicalizeLabelsCommand.java
@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));
}
 
源代码17 项目: swellrt   文件: PstFileDescriptor.java
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.)");
  }
}
 
源代码18 项目: vjtools   文件: IOUtil.java
/**
 * 简单读取Reader的每行内容到List<String>
 */
public static List<String> toLines(final InputStream input) throws IOException {
	return CharStreams.readLines(new BufferedReader(new InputStreamReader(input, Charsets.UTF_8)));
}
 
源代码19 项目: vjtools   文件: IOUtil.java
/**
 * 简单读取Reader的每行内容到List<String>
 */
public static List<String> toLines(final InputStream input) throws IOException {
	return CharStreams.readLines(new BufferedReader(new InputStreamReader(input, Charsets.UTF_8)));
}
 
源代码20 项目: vjtools   文件: IOUtil.java
/**
 * 简单读取Reader的每行内容到List<String>
 * 
 * @see {@link CharStreams#readLines}
 */
public static List<String> toLines(final Reader input) throws IOException {
	return CharStreams.readLines(toBufferedReader(input));
}