下面列出了com.google.common.io.CharSource#readLines ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
} );
}
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);
}
}
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();
}
}
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();
}
}
@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;
}
/**
* @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();
}
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();
}
@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();
}
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());
}