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

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

源代码1 项目: purplejs   文件: CoreLibHelper.java
public void processLines( final Object stream, final Consumer<String> callback )
    throws Exception
{
    final CharSource source = toCharSource( stream );
    source.readLines( new LineProcessor<Object>()
    {
        @Override
        public boolean processLine( final String line )
            throws IOException
        {
            callback.accept( line );
            return true;
        }

        @Override
        public Object getResult()
        {
            return null;
        }
    } );
}
 
源代码2 项目: jopenfst   文件: Convert.java
private static Optional<MutableSymbolTable> importSymbolsFrom(CharSource cs) {
  try {
    ImmutableList<String> lines = cs.readLines();
    MutableSymbolTable newTable = new MutableSymbolTable();
    for (String line : lines) {
      if (isBlank(line)) {
        continue;
      }
      String[] tokens = line.split("\\s+");
      String sym = tokens[0];
      Integer index = Integer.parseInt(tokens[1]);
      newTable.put(sym, index);
    }
    return Optional.of(newTable);
  } catch (IOException e1) {
    throw Throwables.propagate(e1);
  }
}
 
源代码3 项目: qconfig   文件: IpPushConfigServlet.java
private List<String> readLines(HttpServletRequest req) throws IOException {
    try (final InputStream inputStream = req.getInputStream()) {
        CharSource charSource = new CharSource() {
            @Override
            public Reader openStream() throws IOException {
                return new InputStreamReader(inputStream, Constants.UTF_8);
            }
        };
        return charSource.readLines();
    }
}
 
源代码4 项目: qconfig   文件: PushConfigServlet.java
private List<String> readLines(HttpServletRequest req) throws IOException {
    try (final InputStream inputStream = req.getInputStream()) {
        CharSource charSource = new CharSource() {
            @Override
            public Reader openStream() throws IOException {
                return new InputStreamReader(inputStream, Constants.UTF_8);
            }
        };
        return charSource.readLines();
    }
}
 
源代码5 项目: qconfig   文件: RequestParserV2Impl.java
@Override
public List<CheckRequest> parse(HttpServletRequest req) throws IOException {
    List<String> list;
    try (final InputStream inputStream = req.getInputStream()) {
        CharSource charSource = new CharSource() {
            @Override
            public Reader openStream() throws IOException {
                return new InputStreamReader(inputStream, Constants.UTF_8);
            }
        };
        list = charSource.readLines();
    }

    String profile = clientInfoService.getProfile();
    List<CheckRequest> result = Lists.newArrayList();
    for (String aList : list) {
        Iterator<String> iterator = SPLITTER.split(aList).iterator();
        CheckRequest request = new CheckRequest();
        try {
            request.setGroup(iterator.next());
            request.setDataId(iterator.next());
            request.setVersion(Integer.valueOf(iterator.next()));
            request.setLoadProfile(iterator.next());
            request.setProfile(profile);
        } catch (Exception e) {
            logger.warn("iterator no next, line: {}", aList, e);
            return ImmutableList.of();
        }
        result.add(request);
    }
    return result;

}
 
@Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
	if (IS_FLUTTER_PROJECT_PROPERTY.equalsIgnoreCase(property)) {
		IResource resource = Adapters.adapt(receiver, IResource.class);
		if (resource == null) {
			return false;
		}

		IProject project = resource.getProject();
		if (project == null) {
			return false;
		}
		IResource pubspec = project.findMember(GlobalConstants.PUBSPEC_YAML);
		if (pubspec == null) {
			return false;
		}
		File pubspecFile = pubspec.getRawLocation().toFile();
		CharSource pubspecContent = Files.asCharSource(pubspecFile, Charset.defaultCharset());
		try {
			for (String line : pubspecContent.readLines()) {
				if (FLUTTER_SDK.matcher(line).matches()) {
					return true;
				}
			}
		} catch (IOException e) {
			LOG.log(DartLog.createError("Could not open pubspec.yaml", e));
		}
	}
	return false;
}
 
源代码7 项目: n4js   文件: JSLibSingleTestConfigProvider.java
/**
 * @param resourceName
 *            the classpath-relative location of the to-be-read resource
 */
private static List<String> getFileLines(final String resourceName) throws IOException {
	ByteSource byteSource = new ByteSource() {
		@Override
		public InputStream openStream() throws IOException {
			return Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);
		}
	};

	CharSource charSrc = byteSource.asCharSource(Charsets.UTF_8);
	return charSrc.readLines();
}
 
public final CorpusQueryAssessments loadFrom(final CharSource source) throws IOException {
  final List<String> lines = source.readLines();
  final List<QueryResponse2016> queries = Lists.newArrayList();
  final Map<QueryResponse2016, String> metadata = Maps.newHashMap();
  final ImmutableMultimap.Builder<QueryResponse2016, Symbol> responsesToSystems =
      ImmutableMultimap.builder();
  final Map<QueryResponse2016, QueryAssessment2016> assessments = Maps.newHashMap();
  Optional<String> lastMetadata = Optional.absent();
  for (final String line : lines) {
    if (line.startsWith("#")) {
      lastMetadata = Optional.of(line.trim().substring(1));
    } else {
      final String[] parts = line.trim().split("\t");
      checkArgument(parts.length == 5, "expected five columns, but got " + parts.length);
      final Symbol queryID = Symbol.from(parts[0]);
      final Symbol docID = Symbol.from(parts[1]);
      final String[] systemIDs = parts[2].split(",");
      final ImmutableSortedSet<CharOffsetSpan> spans = extractPJSpans(parts[3]);
      final QueryAssessment2016 assessment = QueryAssessment2016.valueOf(parts[4]);
      final QueryResponse2016 query =
          QueryResponse2016.builder().queryID(queryID).docID(docID)
              .addAllPredicateJustifications(spans).build();
      queries.add(query);
      for(final String systemID: systemIDs) {
        responsesToSystems.put(query, Symbol.from(systemID));
      }
      if (!assessment.equals(QueryAssessment2016.UNASSESSED)) {
        assessments.put(query, assessment);
      }
      if (lastMetadata.isPresent()) {
        metadata.put(query, lastMetadata.get());
        lastMetadata = Optional.absent();
      }
    }
  }
  return CorpusQueryAssessments.builder().addAllQueryReponses(queries).assessments(assessments)
      .queryResponsesToSystemIDs(responsesToSystems.build())
      .metadata(metadata).build();
}
 
源代码9 项目: takari-lifecycle   文件: PropertiesWriter.java
private static void write(CharSource charSource, String comment, OutputStream out) throws IOException {
  List<String> lines = new ArrayList<>(charSource.readLines());
  lines.remove(comment != null ? 1 : 0);
  BufferedWriter w = new BufferedWriter(new OutputStreamWriter(out, ENCODING));
  for (String line : lines) {
    w.write(line);
    w.newLine();
  }
  w.flush();
}
 
源代码10 项目: tac-kbp-eal   文件: AbstractKBPSpecLinkingLoader.java
@Override
public ResponseLinking read(Symbol docID, CharSource source, Set<Response> responses,
    Optional<ImmutableMap<String, String>> foreignResponseIDToLocal,
    final Optional<ImmutableMap.Builder<String, String>> foreignLinkingIdToLocal)
    throws IOException {
  final Map<String, Response> responsesByUID = Maps.uniqueIndex(responses,
      ResponseFunctions.uniqueIdentifier());

  final ImmutableSet.Builder<ResponseSet> responseSetsB = ImmutableSet.builder();
  Optional<ImmutableSet<Response>> incompleteResponses = Optional.absent();
  ImmutableMap.Builder<String, ResponseSet> responseSetIds = ImmutableMap.builder();

  int lineNo = 0;
  try {
    for (final String line : source.readLines()) {
      ++lineNo;
      // empty lines are allowed, and comments on lines
      // beginning with '#'
      if (line.isEmpty() || line.charAt(0) == '#') {
        continue;
      }
      final List<String> parts = StringUtils.onTabs().splitToList(line);
      if (line.startsWith("INCOMPLETE")) {
        if (!incompleteResponses.isPresent()) {
          incompleteResponses = Optional.of(parseResponses(skip(parts, 1),
              foreignResponseIDToLocal, responsesByUID));
        } else {
          throw new IOException("Cannot have two INCOMPLETE lines");
        }
      } else {
        final ImmutableLinkingLine linkingLine = parseResponseSetLine(parts,
            foreignResponseIDToLocal, responsesByUID);
        responseSetsB.add(linkingLine.responses());
        if (linkingLine.id().isPresent()) {
          responseSetIds.put(linkingLine.id().get(), linkingLine.responses());
        }
      }
    }
  } catch (Exception e) {
    throw new IOException("While reading " + docID + ", on line " + lineNo + ": ", e);
  }

  final ImmutableSet<ResponseSet> responseSets = responseSetsB.build();
  final ResponseLinking.Builder responseLinking =
      ResponseLinking.builder().docID(docID).responseSets(responseSets)
          .incompleteResponses(incompleteResponses.or(ImmutableSet.<Response>of()));
  handleResponseSetIDs(responseLinking, responseSets, responseSetIds.build(),
      foreignLinkingIdToLocal);
  return responseLinking.build();
}
 
源代码11 项目: tac-kbp-eal   文件: AssessmentSpecFormats.java
private synchronized AnswerKey uncachedRead(final Symbol docid) throws IOException {
  final ImmutableList.Builder<AssessedResponse> annotated = ImmutableList.builder();
  final ImmutableList.Builder<Response> unannotated = ImmutableList.builder();
  final CorefAnnotation.Builder corefBuilder = assessmentCreator.corefBuilder(docid);

  final File f = bareOrWithSuffix(directory, docid.asString(), ACCEPTABLE_SUFFIXES);

  final CharSource source = Files.asCharSource(f, UTF_8);
  for (final String line : source.readLines()) {
    try {
      if (line.isEmpty() || line.startsWith("#")) {
        continue;
      }
      final String[] parts = line.split("\t");
      final List<String> annotationParts = Arrays.asList(parts).subList(11, parts.length);

      if (annotationParts.isEmpty()) {
        throw new IOException(String.format(
            "The assessment store file for document ID %s appears to be a system " +
                "output file with no assessment columns.", docid));
      }

      final Response response = parseArgumentFields(format, ImmutableList.copyOf(parts));
      final AssessmentCreator.AssessmentParseResult annotation =
          parseAnnotation(annotationParts);

      if (annotation.assessment().isPresent()) {
        annotated.add(AssessedResponse.of(response, annotation.assessment().get()));
      } else {
        unannotated.add(response);
      }

      if (annotation.corefId().isPresent()) {
        corefBuilder.corefCAS(response.canonicalArgument(), annotation.corefId().get());
      } else {
        corefBuilder.addUnannotatedCAS(response.canonicalArgument());
      }
    } catch (Exception e) {
      throw new IOException(String.format(
          "While reading answer key for document %s, error on line %s", docid, line), e);
    }
  }

  return assessmentCreator.createAnswerKey(docid, annotated.build(), unannotated.build(),
      corefBuilder.build());
}