private String withLineNumbers(String code) { try { return CharStreams.readLines(new StringReader(code), new LineProcessor<String>() { private final StringBuilder lines = new StringBuilder(); private int lineNo = 1; @Override public boolean processLine(String line) throws IOException { lines.append(Strings.padStart(String.valueOf(lineNo++), 3, ' ')).append(": ").append(line) .append("\n"); return true; } @Override public String getResult() { return lines.toString(); } }); } catch (IOException e) { throw new IllegalStateException(e.getMessage()); } }
private List<String> findFirst10Errors_guava(File file) throws IOException { List<String> lines = Files.readLines(file, Charsets.UTF_8, new LineProcessor<List<String>>() { List<String> accumulator = new ArrayList<String>(); @Override public boolean processLine(String line) throws IOException { if (line.startsWith(ERROR_PREFIX)) { accumulator.add(line); } return accumulator.size() < 10; } @Override public List<String> getResult() { return accumulator; } }); return lines; }
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 List<String> load(final String path) throws IOException { return Files.readLines(new File(path), Charsets.UTF_8, new LineProcessor<List<String>>() { private final List<String> result = Lists.newArrayList(); private final StringBuilder stringBuilder = new StringBuilder(); @Override public boolean processLine(final String line) throws IOException { if (line.isEmpty()) { if (this.stringBuilder.length() > 0) this.stringBuilder.deleteCharAt(this.stringBuilder.length() - 1); this.result.add(this.stringBuilder.toString()); this.stringBuilder.setLength(0); } else this.stringBuilder.append(line).append("\n"); return true; } @Override public List<String> getResult() { return this.result; } }); }
private BigDecimal getTotalNumberOfLines() { try { return new BigDecimal(Files.readLines(tempFile, Charset.defaultCharset(), new LineProcessor<Integer>() { private int count; @Override public Integer getResult() { return count; } @Override public boolean processLine(final String line) { count++; return true; } })); } catch (final IOException e) { LOG.error("Error while reading temp file for upload.", e); } return new BigDecimal(0); }
/** * Read file that has not any multi-line text * * @param file * @param charset * @return * @throws IOException */ public List<String> readRegularLines(@NonNull final File file, @NonNull final Charset charset) throws IOException { final List<String> loadedDataLines = new ArrayList<>(); Files.readLines(file, charset, new LineProcessor<Void>() { @Override public boolean processLine(final String line) throws IOException { loadedDataLines.add(line); return true; } @Override public Void getResult() { return null; } }); return loadedDataLines; }
public static List<Rewrite> fromCharSource(CharSource source) throws IOException { return source.readLines(new LineProcessor<List<Rewrite>>() { private List<Rewrite> refactorings = new ArrayList<>(); private final Splitter SPLITTER = Splitter.on("->").trimResults().omitEmptyStrings(); @Override public List<Rewrite> getResult() { return Collections.unmodifiableList(refactorings); } @Override public boolean processLine(String line) { List<String> parts = SPLITTER.splitToList(line); refactorings.add(Rewrite.of(parts.get(0), parts.get(1), Rewrite.Visit.Expressions)); return true; } }); }
/** * Return the number of lines in this file. * * @param f * @return * @throws IOException */ public static int numberOfLinesInFile(final File f) throws IOException { return Files.readLines(f, Charset.defaultCharset(), new LineProcessor<Integer>() { int count = 0; @Override public Integer getResult() { return count; } @Override public boolean processLine(final String line) { count++; return true; } }); }
public static Set<String> stringSetFrom(final CharSource supplier) throws IOException { final LineProcessor<Set<String>> callback = new LineProcessor<Set<String>>() { private final ImmutableSet.Builder<String> builder = ImmutableSet.builder(); @Override public boolean processLine(final String s) { builder.add(s.trim()); return true; } @Override public Set<String> getResult() { return builder.build(); } }; supplier.readLines(callback); return callback.getResult(); }
public void parse() throws Exception { Files.readLines(Paths.get(path).toFile(), Charset.defaultCharset(), new LineProcessor<String>() { @Override public boolean processLine(String line) throws IOException { HistogramResult result = JSON.parseObject(line, HistogramResult.class); String histogramString = result.getHistogram(); System.out.println(histogramString); ByteBuffer buffer = ByteBuffer.wrap(DatatypeConverter.parseBase64Binary(histogramString)); try { Histogram histogram = Histogram.decodeFromCompressedByteBuffer(buffer, 0); histogram.setStartTimeStamp(result.getStartTime()); histogram.setEndTimeStamp(result.getEndTime()); //TODO } catch (DataFormatException e) { e.printStackTrace(); } return true; } @Override public String getResult() { return null; } }); }
public List<InputRecord> read(CharSource source) throws IOException { return source.readLines(new LineProcessor<List<InputRecord>>() { private final List<InputRecord> recs = Lists.newArrayList(); @Override public boolean processLine(String line) throws IOException { if (isBlank(line)) { return true; } if (isComment(line)) { return true; } InputRecord maybe = reader.parse(line); if (maybe != null) { recs.add(maybe); } return true; } @Override public List<InputRecord> getResult() { return recs; } }); }
@Test public void shouldGetResourceWithLineProcessorFromContext() throws Exception { // given String classResource = "../base/Either.class"; // when String classString = getResourceWith(Resources.class, classResource, new LineProcessor<String>() { StringBuilder builder = new StringBuilder(); @Override public boolean processLine(String line) throws IOException { builder.append(line).append("\n"); return true; } @Override public String getResult() { return builder.toString(); } }); // then assertThat(classString, isNotEmptyString()); assertThat(classString, containsString("com/bluecatcode/common/base/Either")); }
@Test public void shouldWrapExceptionInGetResourceWithLineProcessorFromContext() throws Exception { // given String classResource = "../base/Either.class"; // expect exception.expect(IllegalStateException.class); // when getResourceWith(Resources.class, classResource, new LineProcessor<String>() { @Override public boolean processLine(String line) throws IOException { throw new IOException("test"); } @Override public String getResult() { return null; } }); }
protected static Collection<String> parseExportPackage(InputStream is) throws IOException { LineProcessor<List<String>> processor = new LineProcessor<List<String>>() { final List<String> result = new ArrayList<String>(); @Override public boolean processLine(String line) throws IOException { result.add(line.replace('.', '/')); return true; // keep reading } @Override public List<String> getResult() { return result; } }; return CharStreams.readLines(new InputStreamReader(is, Charsets.UTF_8), processor); }
private static Map<String, String> mapResource(String name) throws IOException { final Splitter onTab = Splitter.on("\t"); return Resources.readLines(Resources.getResource(name), Charsets.UTF_8, new LineProcessor<Map<String, String>>() { final Map<String, String> r = Maps.newHashMap(); @Override public boolean processLine(String line) throws IOException { Iterator<String> pieces = onTab.split(line).iterator(); String key = pieces.next(); r.put(key, pieces.next()); return true; } @Override public Map<String, String> getResult() { return r; } }); }
private static SetMultimap<String, String> multiMapResource(String name) throws IOException { final Splitter onTab = Splitter.on("\t"); return Resources.readLines(Resources.getResource(name), Charsets.UTF_8, new LineProcessor<SetMultimap<String, String>>() { final SetMultimap<String, String> r = HashMultimap.create(); @Override public boolean processLine(String line) throws IOException { Iterator<String> pieces = onTab.split(line).iterator(); String key = pieces.next(); r.put(key, pieces.next()); return true; } @Override public SetMultimap<String, String> getResult() { return r; } }); }
@Test public void count_lines_text_file_guava() throws IOException { long linesCounted = com.google.common.io.Files.readLines( Paths.get(fileLocation).toFile(), Charsets.UTF_8, new LineProcessor<Long>() { long numberOfLinesInTextFile = 0; @Override public boolean processLine(String line) throws IOException { numberOfLinesInTextFile++; return true; } @Override public Long getResult() { return numberOfLinesInTextFile; } }); assertEquals(10, linesCounted); }
private StringBuilder loadRefGenome(String refGenomePath, String chr) throws IOException { return CharStreams.readLines(new FileReader(String.format("%s/%s.fa", refGenomePath, chr)), new LineProcessor<StringBuilder>() { StringBuilder refChrBuilder = new StringBuilder(100000000); @Override public boolean processLine(String s) throws IOException { if (!s.startsWith(">")) { refChrBuilder.append(s); } return true; } @Override public StringBuilder getResult() { return refChrBuilder; } }); }
/** * Returns a new line processor for the cache file. The processor generates map of song IDs to * file names. */ static LineProcessor<Map<Integer, String>> newCacheFileLineProcessor() { return new LineProcessor<Map<Integer, String>>() { @SuppressWarnings("CanBeFinal") Map<Integer, String> map = Maps.newHashMap(); @Override public boolean processLine(String line) throws IOException { String[] split = line.split("\\|"); map.put(Integer.valueOf(split[0]), split[1]); return true; } @Override public Map<Integer, String> getResult() { return map; } }; }
public static <T extends Reader> List<Refactoring> fromInputSupplier(InputSupplier<T> supplier) throws IOException{ return CharStreams.readLines(supplier, new LineProcessor<List<Refactoring>>() { private List<Refactoring> refactorings = Lists.newArrayList(); private final Splitter SPLITTER = Splitter.on("->").trimResults().omitEmptyStrings(); @Override public List<Refactoring> getResult() { return Collections.unmodifiableList(refactorings); } @Override public boolean processLine(String line) { Iterator<String> parts = SPLITTER.split(line).iterator(); String from = parts.next(); String to = parts.next(); refactorings.add(Refactoring.of(from, to, Refactoring.Visit.Expressions)); return true; } }); }
@Test public void verifySourceFilesDoNotContainTabCharacters() throws IOException { final Iterator<File> iter = FileUtils.iterateFiles(new File("src"), new CodeStyleFileFilter(), TrueFileFilter.INSTANCE); final List<String> filesContainingTabs = new ArrayList<>(); while (iter.hasNext()) { final File file = iter.next(); Files.readLines(file, StandardCharsets.US_ASCII, new LineProcessor<Void>() { @Override public boolean processLine(@Nonnull final String line) throws IOException { boolean result = true; if (line.contains("\t")) { filesContainingTabs.add(file.getCanonicalPath()); result = false; } return result; } @Override public Void getResult() { return null; } }); } assertEmpty(filesContainingTabs, "The following files contain tab characters: "); }
public static ImmutableList<String> getWhiteList() { File file = new File("nu-whitelist.txt"); try { return Files.readLines(file, Charsets.UTF_8, new LineProcessor<ImmutableList<String>>() { List<String> result = Lists.newLinkedList(); @Override public boolean processLine(String line) { if (!line.startsWith("#") && !line.isEmpty()) result.add(line); return true; } @Override public ImmutableList<String> getResult() { return ImmutableList.copyOf(result); } }); } catch (IOException e) { if (!file.isFile()) try { if (file.createNewFile()) { String whitelist = Joiner.on('\n').join(DEFAULT_WHITE_LIST); logger.info("Create file: nu-whitelist.txt\n" + whitelist); Files.write(whitelist, file, Charsets.UTF_8); return DEFAULT_WHITE_LIST; } } catch (IOException ex) { ex.printStackTrace(); } return ImmutableList.of(); } }
/** * 读取模版文件并做变量替换 */ private static List<String> renderTemplate(String templateFileName, final Map<String, String> params) throws MalformedURLException, IOException { final Pattern p = Pattern.compile("\\{(.*?)\\}"); // 定义每行的处理逻辑 LineProcessor<List<String>> processor = new LineProcessor<List<String>>() { private List<String> result = Lists.newArrayList(); @Override public boolean processLine(String line) throws IOException { String tmp = line; Matcher m = p.matcher(line); while (m.find()) { String key = m.group(1); if (params.containsKey(key)) { tmp = tmp.replaceAll("\\{" + key + "\\}", params.get(key)); } } result.add(tmp); return true; } @Override public List<String> getResult() { return result; } }; return Resources.readLines(Resources.getResource(templateFileName), Charsets.UTF_8, processor); }
private List<News> load() throws IOException { if (!file.exists()) { return new ArrayList<>(); } return Files.readLines(file, CHARSET, new LineProcessor<List<News>>() { List<News> loadedNews = new ArrayList<>(); @Override public boolean processLine(String line) throws IOException { try { line = line.trim(); if (!line.isEmpty()) { int indexOfSpace1 = line.indexOf(' '); String createdAsString = line.substring(0, indexOfSpace1); int indexOfSpace2 = line.indexOf(' ', indexOfSpace1 + 1); String uuidAsString = line.substring(indexOfSpace1 + 1, indexOfSpace2); String textAsString = line.substring(indexOfSpace2 + 1); Instant createdOn = dateTimeFormatter.parse(createdAsString, Instant::from); UUID uuid = UUID.fromString(uuidAsString); User byUser = getUser(uuid); Text message = textSerializer.deserialize(textAsString); loadedNews.add(ImmutableNews.builder().createdOn(createdOn).author(byUser).message(message).build()); } return true; } catch (StringIndexOutOfBoundsException | DateTimeParseException | TextParseException | UserNotFoundException e) { throw new IOException("Failed to read line of file " + file + ": " + line, e); } } @Override public List<News> getResult() { return loadedNews; } }); }
private Map<String, String> readCSVs() throws IOException { final Map<String, String> csvData = Maps.newHashMap(); File[] csvs = new File[] { fieldCSV == null ? null : fieldCSV, methodCSV == null ? null : methodCSV }; for (File f : csvs) { if (f == null) continue; Files.readLines(f, Charset.defaultCharset(), new LineProcessor<Object>() { @Override public boolean processLine(String line) throws IOException { String[] s = line.split(","); csvData.put(s[0], s[1]); return true; } @Override public Object getResult() { return null; } }); } return csvData; }
private Map<String, String> createClassMap(Map<String, String> markerMap, final List<String> interfaces) throws IOException { Map<String, String> excMap = Files.readLines(excConfig, Charset.defaultCharset(), new LineProcessor<Map<String, String>>() { Map<String, String> tmp = Maps.newHashMap(); @Override public boolean processLine(String line) throws IOException { if (line.contains(".") || !line.contains("=") || line.startsWith("#")) return true; String[] s = line.split("="); if (!interfaces.contains(s[0])) tmp.put(s[0], s[1] + "_"); return true; } @Override public Map<String, String> getResult() { return tmp; } }); Map<String, String> map = Maps.newHashMap(); for (Entry<String, String> e : excMap.entrySet()) { String renamed = markerMap.get(e.getValue()); if (renamed != null) { map.put(e.getKey(), renamed); } } return map; }
/** * @see com.google.common.io.LineProcessor * @see com.google.common.io.Resources#getResource(String) * @see com.google.common.io.Resources#readLines(URL, Charset, LineProcessor) */ public static <T> T getResourceWith(String resourceName, LineProcessor<T> lineProcessor) { checkNotNull(lineProcessor); URL url = Resources.getResource(checkNotEmpty(resourceName)); try { return Resources.readLines(url, Charsets.UTF_8, lineProcessor); } catch (IOException e) { throw new IllegalStateException(e); } }
private CharArraySet getStopWordSet() { File stopWordsFile = new File(Configuration.getInstance().getStopWord()); try { return Files.readLines(stopWordsFile, Charsets.UTF_8, new LineProcessor<CharArraySet>() { CharArraySet result = new CharArraySet(Configuration .getInstance() .getStopWordSize(), true); @Override public CharArraySet getResult() { return result; } @Override public boolean processLine(String line) { if (Strings.isNotBlank(line)) { result.add(line.trim()); } return true; } }); } catch (IOException e) { throw new RuntimeException("Unable to find Stopwords file: ", e); } }
default Mappings parse(Readable readable) throws IOException { LineReader lineReader = new LineReader(readable); LineProcessor<Mappings> lineProcessor = createLineProcessor(); String line; while ((line = lineReader.readLine()) != null) { if (!lineProcessor.processLine(line)) { break; } } return lineProcessor.getResult(); }
private void loadToMultitable(final CharSource source, final ImmutableMultitable.Builder<R, C, V> ret) throws IOException { source.readLines(new LineProcessor<Void>() { @Override public boolean processLine(final String line) throws IOException { final List<String> fields = fieldSplitter().splitToList(line); if (fields.size() == 3) { final R rowKey = interpret(fields.get(0), rowInterpreter(), "row key", line); final C columnKey = interpret(fields.get(1), columnInterpreter(), "column key", line); if (valueListSplitter().isPresent()) { for (final String value : valueListSplitter().get().split(fields.get(2))) { ret.put(rowKey, columnKey, interpret(value, valueInterpreter(), "value", line)); } } else { ret.put(rowKey, columnKey, interpret(fields.get(2), valueInterpreter(), "value", line)); } } else { throw new IOException("Cannot parse lines as multitable entries:\n" + line); } // we never stop procesisng lines early return true; } @Override public Void getResult() { return null; } }); }
/** * Reads all of the lines from an {@link InputStream} object. The lines include the line-termination characters, and also include other leading * and trailing whitespace. * <p> * Does not close the {@code InputStream}. * * @param is The stream to read from. * @throws IOException if an I/O error occurs. */ public static String readLines(InputStream is) throws IOException { InputStreamReader isr = new InputStreamReader(is); try { return CharStreams.readLines(isr, new LineProcessor<String>() { final StringBuilder lines = new StringBuilder(); @Override public boolean processLine(String line) throws IOException { lines.append(line).append(Strings2.NEW_LINE); return true; } @Override public String getResult() { return lines.toString(); } }); } finally { closeQuietly(isr); } }
public static void main(String[] args) throws IOException { AvscSchemaBuilder asb = new AvscSchemaBuilder(Metric.class); final DataFileWriter<Metric2> dataFileWriter = new DataFileWriter<Metric2>(new SpecificDatumWriter<Metric2>(asb.createSchema())); dataFileWriter.create(asb.createSchema(), new File("v1.avro")); Path p = Paths.get("v1.log"); Files.readLines(p.toFile(), Charset.defaultCharset(), new LineProcessor<String>() { @Override public boolean processLine(String line) throws IOException { String[] vals = line.split(" "); Metric2 mc = Metric2.builder().dataVersion(Integer.valueOf(vals[0])).salt(Integer.valueOf(vals[1])).applicationId(Long.valueOf(vals[2])) .timeScope(Integer.valueOf(vals[3])).metricTypeId(Long.valueOf(vals[4])).metricId(Long.valueOf(vals[5])).time(Integer.valueOf(vals[6])) .agentRunId(Long.valueOf(vals[7])).uuid(vals[8]).num1(Double.valueOf(vals[9])).num2(Double.valueOf(vals[10])).num3(Double.valueOf(vals[11])) .num4(Double.valueOf(vals[12])).num5(Double.valueOf(vals[13])).num6(Double.valueOf(vals[14])).timestamp(Long.valueOf(vals[15])).build(); dataFileWriter.append(mc); return true; } @Override public String getResult() { return null; } }); dataFileWriter.flush(); dataFileWriter.close(); }
public void handle() throws IOException { Files.readLines(Paths.get(sourceDataFilePath).toFile(), Charset.defaultCharset(), new LineProcessor<String>() { @Override public boolean processLine(String line) throws IOException { service.addRecord("A", "a", System.currentTimeMillis(), Long.valueOf(line)); return true; } @Override public String getResult() { return null; } }); service.shuffle(service.acquireAllData(), resultDataFilePath); }
public void handle() throws IOException { Files.readLines(Paths.get(sourceDataFilePath).toFile(), Charset.defaultCharset(), new LineProcessor<String>() { @Override public boolean processLine(String line) throws IOException { String[] splits = line.split(" "); service.addRecord("A", "a", Long.valueOf(splits[0]), Long.valueOf(splits[1])); return true; } @Override public String getResult() { return null; } }); service.shuffle(service.acquireAllData(), resultDataFilePath); }
public List<AlignGroup> readInput(CharSource source) throws IOException { return source.readLines(new LineProcessor<List<AlignGroup>>() { final List<AlignGroup> groups = Lists.newArrayList(); String lastGroup = ""; List<Alignment> aligns = Lists.newArrayList(); @Override public boolean processLine(String line) throws IOException { List<String> fields = caretSplit.splitToList(line); if (!lastGroup.equals(fields.get(0))) { lastGroup = fields.get(0); if (!aligns.isEmpty()) { groups.add(new AlignGroup(aligns)); } aligns.clear(); } Double score = Double.parseDouble(fields.get(1)); Word input = Word.fromSpaceSeparated(fields.get(2)); Iterable<String> graphs = pipeSplit.split(fields.get(3)); Iterable<String> phones = pipeSplit.split(fields.get(4)); aligns.add(new Alignment(input, Zipper.up(graphs, phones), score)); return true; } @Override public List<AlignGroup> getResult() { if (!aligns.isEmpty()) { groups.add(new AlignGroup(aligns)); } return groups; } }); }
@Test public void testText() throws IOException { final int k = 200; final ReferenceTopK<String> ref = new ReferenceTopK<>(k); final DeterministicTopK<String> det = new DeterministicTopK<>(k); final Pattern pattern = Pattern.compile("[\\p{Punct}\\p{Space}]"); Resources.readLines(Resources.getResource("alice.txt"), Charsets.UTF_8, new LineProcessor<Void>() { @Override public boolean processLine(String line) throws IOException { Scanner scanner = new Scanner(line).useDelimiter(pattern); while (scanner.hasNext()) { String item = scanner.next().toLowerCase(); ref.add(item); det.add(item); } return true; } @Override public Void getResult() { return null; } }); Iterable<String> a = det.get(); Iterable<String> b = ref.get(); System.out.format("Ref: %s%n", ref); System.out.format("Det: %s%n", det); List<String> detSet = ImmutableList.copyOf(Iterables.limit(det.get(), 20)); List<String> refSet = ImmutableList.copyOf(Iterables.limit(ref.get(), 20)); assertThat(detSet, equalTo(refSet)); }
/** * @param contextClass the class the resource is relative to * @param resourceName the resource name * @param lineProcessor the line processor to use * @param <T> the type of object to load into * @return the loaded object of type <tt>T</tt> * @see com.google.common.io.LineProcessor * @see com.google.common.io.Resources#getResource(String) * @see com.google.common.io.Resources#readLines(java.net.URL, java.nio.charset.Charset, LineProcessor) */ public static <T> T getResourceWith(Class<?> contextClass, String resourceName, LineProcessor<T> lineProcessor) { checkArgument(contextClass != null, "Expected non-null contextClass"); checkNotEmpty(resourceName); checkArgument(lineProcessor != null, "Expected non-null lineProcessor"); URL url = getResource(contextClass, resourceName); try { return readLines(url, UTF_8, lineProcessor); } catch (IOException e) { throw new IllegalStateException(e); } }
private static <T> T readResource(final Class<?> origin, final String resource, final LineProcessor<T> processor) throws IOException { try { return readLines(getResource(origin, resource), UTF_8, processor); } catch (final IOException ioe) { throw new IOException("Error reading resource: '" + resource + "'", ioe); } }
private static String[] mergeFileArguments(final String[] rawArguments) { List<String> arguments = Lists.newArrayList(); for (String arg : rawArguments) { // If we've been given an input file if (arg.startsWith("@")) { String filename = arg.substring(1); try { List<String> nonCommentLines = Files.readLines(new File(filename), Charset.defaultCharset(), new LineProcessor<List<String>>() { List<String> lines = Lists.newArrayList(); @Override public boolean processLine(String line) throws IOException { // This is the end of the file if (line.startsWith("//")) return false; // Ignore comments and empty lines, ignore everything after a comment if (!line.startsWith("#") && line.length() > 0) lines.add(line.split("#")[0]); return true; } @Override public List<String> getResult() { return lines; } }); arguments.addAll(nonCommentLines); } catch (IOException e) { CoreUtils.msg("ERROR: Could not read file '%s'\n", arg.substring(1)); } } else { arguments.add(arg); } } return arguments.toArray(new String[arguments.size()]); }