com.google.common.io.LineProcessor Java Examples
The following examples show how to use
com.google.common.io.LineProcessor.
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: CountLinesTextFile.java From levelup-java-examples with Apache License 2.0 | 6 votes |
@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); }
Example #2
Source File: AdbController.java From android-test with Apache License 2.0 | 6 votes |
private boolean apkHashMatchesInstalledApk(String apkPath, String installedApkPath) throws IOException { String apkHash = Files.asByteSource(new File(apkPath)).hash(Hashing.md5()).toString(); Pattern hashPattern = Pattern.compile(String.format("^%s .*", apkHash)); LineProcessor<Boolean> md5Processor = regexpProcessorBuilder .withPattern(hashPattern).buildRegexpPresentProcessor(); SubprocessCommunicator.Builder builder = communicatorBuilderProvider.get() .withStdoutProcessor(md5Processor) .withStderrProcessor(md5Processor); makeAdbCall( builder, getDefaultTimeout(), "shell", "md5", installedApkPath, "||", "md5sum", installedApkPath); return md5Processor.getResult(); }
Example #3
Source File: TextFileUtils.java From tassal with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * 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; } }); }
Example #4
Source File: MongoAdapterTest.java From calcite with Apache License 2.0 | 6 votes |
private static void populate(MongoCollection<Document> collection, URL resource) throws IOException { Objects.requireNonNull(collection, "collection"); if (collection.countDocuments() > 0) { // delete any existing documents (run from a clean set) collection.deleteMany(new BsonDocument()); } MongoCollection<BsonDocument> bsonCollection = collection.withDocumentClass(BsonDocument.class); Resources.readLines(resource, StandardCharsets.UTF_8, new LineProcessor<Void>() { @Override public boolean processLine(String line) throws IOException { bsonCollection.insertOne(BsonDocument.parse(line)); return true; } @Override public Void getResult() { return null; } }); }
Example #5
Source File: DependencyClasspathEntry.java From takari-lifecycle with Eclipse Public License 1.0 | 6 votes |
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); }
Example #6
Source File: TextFileUtils.java From api-mining with GNU General Public License v3.0 | 6 votes |
/** * 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; } }); }
Example #7
Source File: BulkUploadHandler.java From hawkbit with Eclipse Public License 1.0 | 6 votes |
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); }
Example #8
Source File: CoreLibHelper.java From purplejs with Apache License 2.0 | 6 votes |
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; } } ); }
Example #9
Source File: VinSampler.java From log-synth with Apache License 2.0 | 6 votes |
private static Map<String, String> mapResource(String name) throws IOException { final Splitter onTab = Splitter.on("\t"); //noinspection UnstableApiUsage 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) { 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; } }); }
Example #10
Source File: VinSampler.java From log-synth with Apache License 2.0 | 6 votes |
private static SetMultimap<String, String> multiMapResource(String name) throws IOException { final Splitter onTab = Splitter.on("\t"); //noinspection UnstableApiUsage 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) { 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; } }); }
Example #11
Source File: PositiveAnalyser.java From n4js with Eclipse Public License 1.0 | 6 votes |
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()); } }
Example #12
Source File: Util.java From log-synth with Apache License 2.0 | 6 votes |
public static void readData(String resource, Function<String, Void> callback) throws IOException { //noinspection UnstableApiUsage Resources.readLines(Resources.getResource(resource), Charsets.UTF_8, new LineProcessor<Void>() { @Override public boolean processLine(String line) { if (!line.startsWith("# ")) { callback.apply(line); } return true; } @Override public Void getResult() { return null; } }); }
Example #13
Source File: Symbols.java From buck with Apache License 2.0 | 5 votes |
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 #14
Source File: Symbols.java From buck with Apache License 2.0 | 5 votes |
public static ImmutableSet<String> getDtNeeded( ProcessExecutor executor, Tool objdump, SourcePathResolverAdapter resolver, Path lib) throws IOException, InterruptedException { ImmutableSet.Builder<String> builder = ImmutableSet.builder(); Pattern re = Pattern.compile("^ *NEEDED *(\\S*)$"); runObjdump( executor, objdump, resolver, lib, ImmutableList.of("-p"), new LineProcessor<Unit>() { @Override public boolean processLine(String line) { Matcher m = re.matcher(line); if (!m.matches()) { return true; } builder.add(m.group(1)); return true; } @Override public Unit getResult() { return Unit.UNIT; } }); return builder.build(); }
Example #15
Source File: ElasticSearchAdapterTest.java From calcite with Apache License 2.0 | 5 votes |
/** * Used to create {@code zips} index and insert zip data in bulk. * @throws Exception when instance setup failed */ @BeforeAll public static void setupInstance() throws Exception { final Map<String, String> mapping = ImmutableMap.of("city", "keyword", "state", "keyword", "pop", "long"); NODE.createIndex(ZIPS, mapping); // load records from file final List<ObjectNode> bulk = new ArrayList<>(); Resources.readLines(ElasticSearchAdapterTest.class.getResource("/zips-mini.json"), StandardCharsets.UTF_8, new LineProcessor<Void>() { @Override public boolean processLine(String line) throws IOException { line = line.replace("_id", "id"); // _id is a reserved attribute in ES bulk.add((ObjectNode) NODE.mapper().readTree(line)); return true; } @Override public Void getResult() { return null; } }); if (bulk.isEmpty()) { throw new IllegalStateException("No records to index. Empty file ?"); } NODE.insertBulk(ZIPS, bulk); }
Example #16
Source File: MatchTest.java From calcite with Apache License 2.0 | 5 votes |
/** * Used to create {@code zips} index and insert zip data in bulk. * @throws Exception when instance setup failed */ @BeforeAll public static void setup() throws Exception { final Map<String, String> mapping = ImmutableMap.of("city", "text", "state", "keyword", "pop", "long"); NODE.createIndex(ZIPS, mapping); // load records from file final List<ObjectNode> bulk = new ArrayList<>(); Resources.readLines(ElasticSearchAdapterTest.class.getResource("/zips-mini.json"), StandardCharsets.UTF_8, new LineProcessor<Void>() { @Override public boolean processLine(String line) throws IOException { line = line.replace("_id", "id"); // _id is a reserved attribute in ES bulk.add((ObjectNode) NODE.mapper().readTree(line)); return true; } @Override public Void getResult() { return null; } }); if (bulk.isEmpty()) { throw new IllegalStateException("No records to index. Empty file ?"); } NODE.insertBulk(ZIPS, bulk); }
Example #17
Source File: MappingProviderSrg.java From Mixin with MIT License | 5 votes |
@Override public void read(final File input) throws IOException { // Locally scoped to avoid synthetic accessor final BiMap<String, String> packageMap = this.packageMap; final BiMap<String, String> classMap = this.classMap; final BiMap<MappingField, MappingField> fieldMap = this.fieldMap; final BiMap<MappingMethod, MappingMethod> methodMap = this.methodMap; Files.readLines(input, Charset.defaultCharset(), new LineProcessor<String>() { @Override public String getResult() { return null; } @Override public boolean processLine(String line) throws IOException { if (Strings.isNullOrEmpty(line) || line.startsWith("#")) { return true; } String type = line.substring(0, 2); String[] args = line.substring(4).split(" "); if (type.equals("PK")) { packageMap.forcePut(args[0], args[1]); } else if (type.equals("CL")) { classMap.forcePut(args[0], args[1]); } else if (type.equals("FD")) { fieldMap.forcePut(new MappingFieldSrg(args[0]).copy(), new MappingFieldSrg(args[1]).copy()); } else if (type.equals("MD")) { methodMap.forcePut(new MappingMethod(args[0], args[1]), new MappingMethod(args[2], args[3])); } else { throw new MixinException("Invalid SRG file: " + input); } return true; } }); }
Example #18
Source File: AdbController.java From android-test with Apache License 2.0 | 5 votes |
public void broadcastAction(String action, String flags, String packageName, Map<String, String> extras) { checkNotNull(action); SubprocessCommunicator.Builder builder = communicatorBuilderProvider.get(); LineProcessor<Boolean> stderrProcessor = getStartActivityErrorPresentProcessor(); LineProcessor<Boolean> stdoutProcessor = getStartActivityErrorPresentProcessor(); builder.withStdoutProcessor(stdoutProcessor); builder.withStderrProcessor(stderrProcessor); List<String> adbArgs = Lists.newArrayList("shell", "am", "broadcast"); adbArgs.add("-a"); adbArgs.add(action); if (device.getApiVersion() > 16) { adbArgs.add("-f"); adbArgs.add(flags); } for (Map.Entry<String, String> extra : extras.entrySet()) { adbArgs.add("-e"); adbArgs.add(extra.getKey()); adbArgs.add(extra.getValue()); } // explicitly send this to our package. (only supported after API level 10) if (device.getApiVersion() > 10) { adbArgs.add(packageName); } logger.info("Broadcast cmdline: " + adbArgs); makeAdbCall(builder, getDefaultTimeout(), adbArgs.toArray(new String[0])); String checkLogs = "Could not broadcast, check adb logs."; checkState(!stdoutProcessor.getResult(), checkLogs); checkState(!stderrProcessor.getResult(), checkLogs); }
Example #19
Source File: AdbController.java From android-test with Apache License 2.0 | 5 votes |
private String getApkPathForInstalledApp(String appPackageName) { LineProcessor<String> lineProcessor = regexpProcessorBuilder .withPattern(PATH_PATTERN).buildFirstMatchProcessor(); SubprocessCommunicator.Builder builder = communicatorBuilderProvider.get() .withStdoutProcessor(lineProcessor) .withStderrProcessor(lineProcessor); makeAdbCall( builder, getShortDefaultTimeout(), "shell", "pm", "path", appPackageName, "||", "true"); return lineProcessor.getResult(); }
Example #20
Source File: RuntimeEnv.java From Quicksql with MIT License | 5 votes |
public static void init() throws IOException { System.out.println("Elasticsearch Embedded Server is starting up, waiting...."); final Map<String, String> mapping = ImmutableMap.of("stu_id", "keyword", "type", "keyword", "city", "keyword", "digest", "long", "province", "keyword"); NODE.createIndex("student", mapping); // load records from file final List<ObjectNode> bulk = new ArrayList<>(); Resources.readLines(CsvJoinWithEsExample.class.getResource("/student.json"), StandardCharsets.UTF_8, new LineProcessor<Void>() { @Override public boolean processLine(String line) throws IOException { line = line.replaceAll("_id", "id"); bulk.add((ObjectNode) NODE.mapper().readTree(line)); return true; } @Override public Void getResult() { return null; } }); if (bulk.isEmpty()) { throw new IllegalStateException("No records to index. Empty file ?"); } NODE.insertBulk("student", bulk); System.out.println("Elasticsearch Embedded Server has started!! Your query is running..."); }
Example #21
Source File: AdbController.java From android-test with Apache License 2.0 | 5 votes |
/** * Launches an activity thru the activity manager. * * The map of extras are passed to the activity in the natural order of map iteration. * * @param action the action to send in the launching intent * @param activityName the android_component/activity_class_name * @param extras a map of key-value pairs that specify extra values to pass to the activity * @param waitForLaunch block till activity is launched. */ public void startActivity(ActivityAction action, String activityName, Map<String, String> extras, boolean waitForLaunch) { checkNotNull(action); checkNotNull(activityName); checkNotNull(extras); SubprocessCommunicator.Builder builder = communicatorBuilderProvider.get(); LineProcessor<Boolean> stderrProcessor = getStartActivityErrorPresentProcessor(); LineProcessor<Boolean> stdoutProcessor = getStartActivityErrorPresentProcessor(); builder.withStdoutProcessor(stdoutProcessor); builder.withStderrProcessor(stderrProcessor); List<String> adbArgs = Lists.newArrayList("shell", "am", "start"); if (waitForLaunch) { adbArgs.add("-W"); } adbArgs.addAll(Lists.newArrayList("-a", action.getActionName(), "-n", activityName)); if (extras != null) { for (Map.Entry<String, String> extra : extras.entrySet()) { adbArgs.add("-e"); adbArgs.add(extra.getKey()); adbArgs.add(extra.getValue()); } } makeAdbCall(builder, getDefaultTimeout(), adbArgs.toArray(new String[0])); String checkLogs = "Could not start activity, check adb logs."; checkState(!stdoutProcessor.getResult(), checkLogs); checkState(!stderrProcessor.getResult(), checkLogs); }
Example #22
Source File: AdbController.java From android-test with Apache License 2.0 | 5 votes |
/** * Returns {@code true} if uninstall of the given app from the device was successful. */ public boolean uninstallApp(String appPackageName) { checkNotNull(appPackageName); LineProcessor<List<String>> stdoutProcessor = adbLineListProvider.get(); SubprocessCommunicator.Builder builder = communicatorBuilderProvider.get().withStdoutProcessor(stdoutProcessor); makeAdbCall(builder, getDefaultTimeout(), "uninstall", appPackageName); return isTextPresentInProcessorResult(stdoutProcessor, "Success"); }
Example #23
Source File: AdbController.java From android-test with Apache License 2.0 | 5 votes |
private boolean isTextPresentInProcessorResult( LineProcessor<List<String>> processor, String text) { for (String line : processor.getResult()) { if (line.contains(text)) { return true; } } return false; }
Example #24
Source File: GooGoo.java From react-java-goos with Apache License 2.0 | 5 votes |
/** * 读取模版文件并做变量替换 */ 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); }
Example #25
Source File: ProjectUtils.java From xds-ide with Eclipse Public License 1.0 | 5 votes |
public static String getParseRootModuleName(XdsProjectSettings xdsProjectSettings) { String compilationRootName = xdsProjectSettings.getCompilationRootName(); if (XdsFileUtils.isXdsProjectFile(compilationRootName)) { try { return Files.readLines(ResourceUtils.getAbsoluteFile(getPrjFile(xdsProjectSettings)), Charset.defaultCharset(), new LineProcessor<String>() { final Pattern linePattern = Pattern.compile("[!]module (\\w+(?:[.]\\w{3})?)"); String result; @Override public boolean processLine(String line) throws IOException { Matcher matcher = linePattern.matcher(line); if (matcher.find()) { String moduleName = matcher.group(1); if (XdsFileUtils.isCompilationUnitFile(moduleName) || FilenameUtils.getExtension(moduleName).isEmpty()) { result = moduleName; return false; } } return true; } @Override public String getResult() { return result; } }); } catch (IOException e) { LogHelper.logError(e); } return null; } else { return compilationRootName; } }
Example #26
Source File: QueryProcedureTest.java From Quicksql with MIT License | 5 votes |
/** * test. * * @throws IOException io exception */ @BeforeClass public static void setupInstance() throws IOException { final Map<String, String> mapping = ImmutableMap.of("stu_id", "long", "type", "long", "city", "keyword", "digest", "long", "province", "keyword"); NODE.createIndex("student", mapping); // load records from file final List<ObjectNode> bulk = new ArrayList<>(); Resources.readLines(QueryProcedureTest.class.getResource("/student.json"), StandardCharsets.UTF_8, new LineProcessor<Void>() { @Override public boolean processLine(String line) throws IOException { line = line.replaceAll("_id", "id"); bulk.add((ObjectNode) NODE.mapper().readTree(line)); return true; } @Override public Void getResult() { return null; } }); if (bulk.isEmpty()) { throw new IllegalStateException("No records to index. Empty file ?"); } NODE.insertBulk("student", bulk); producer = new QueryProcedureProducer( "inline: " + MetadataPostman.getCalciteModelSchema(tableNames), SqlRunner.builder()); }
Example #27
Source File: SqlLogicalPlanViewTest.java From Quicksql with MIT License | 5 votes |
/** * test. * * @throws IOException io exception */ @BeforeClass public static void setupInstance() throws IOException { final Map<String, String> mapping = ImmutableMap.of("stu_id", "long", "type", "long", "city", "keyword", "digest", "long", "province", "keyword"); NODE.createIndex("student", mapping); // load records from file final List<ObjectNode> bulk = new ArrayList<>(); Resources.readLines(QueryProcedureTest.class.getResource("/student.json"), StandardCharsets.UTF_8, new LineProcessor<Void>() { @Override public boolean processLine(String line) throws IOException { line = line.replaceAll("_id", "id"); bulk.add((ObjectNode) NODE.mapper().readTree(line)); return true; } @Override public Void getResult() { return null; } }); if (bulk.isEmpty()) { throw new IllegalStateException("No records to index. Empty file ?"); } NODE.insertBulk("student", bulk); producer = new QueryProcedureProducer( "inline: " + MetadataPostman.getCalciteModelSchema(tableNames), SqlRunner.builder()); }
Example #28
Source File: AdbController.java From android-test with Apache License 2.0 | 4 votes |
@VisibleForTesting LineProcessor<Boolean> getStartActivityErrorPresentProcessor() { return new RegexpPresentProcessor(Pattern.compile("Error:.*")); }
Example #29
Source File: RemappingReferenceMapper.java From Mixin with MIT License | 4 votes |
/** * Read srgs from the specified file resource. The mappings are cached * internally so this will only read the file the first time it is called * with a particulare filename. * * @param fileName srg file to read * @return srgs read from file or empty map if the file could not be read */ private static Map<String, String> loadSrgs(String fileName) { if (RemappingReferenceMapper.srgs.containsKey(fileName)) { return RemappingReferenceMapper.srgs.get(fileName); } final Map<String, String> map = new HashMap<String, String>(); RemappingReferenceMapper.srgs.put(fileName, map); File file = new File(fileName); if (!file.isFile()) { return map; } try { Files.readLines(file, Charsets.UTF_8, new LineProcessor<Object>() { @Override public Object getResult() { return null; } @Override public boolean processLine(String line) throws IOException { if (Strings.isNullOrEmpty(line) || line.startsWith("#")) { return true; } int fromPos = 0, toPos = 0; if ((toPos = line.startsWith("MD: ") ? 2 : line.startsWith("FD: ") ? 1 : 0) > 0) { String[] entries = line.substring(4).split(" ", 4); map.put( entries[fromPos].substring(entries[fromPos].lastIndexOf('/') + 1), entries[toPos].substring(entries[toPos].lastIndexOf('/') + 1) ); } return true; } }); } catch (IOException ex) { RemappingReferenceMapper.logger.warn("Could not read input SRG file: {}", fileName); RemappingReferenceMapper.logger.catching(ex); } return map; }
Example #30
Source File: AdbController.java From android-test with Apache License 2.0 | 4 votes |
private void checkAdbInstallResult(LineProcessor<List<String>> processor) { if (!isTextPresentInProcessorResult(processor, "Success")) { throw new RuntimeException("adb install failed. Output: " + processor.getResult()); } }