下面列出了怎么用com.google.common.io.LineProcessor的API类实例代码及写法,或者点击链接到github查看源代码。
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());
}
}
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 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);
}
/**
* 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;
}
});
}
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();
}
/**
* 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;
}
});
}
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;
}
});
}
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");
//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;
}
});
}
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;
}
});
}
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;
}
});
}
@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);
}
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...");
}
/**
* 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());
}
/**
* 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());
}
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;
}
}
/**
* 读取模版文件并做变量替换
*/
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 boolean isTextPresentInProcessorResult(
LineProcessor<List<String>> processor, String text) {
for (String line : processor.getResult()) {
if (line.contains(text)) {
return true;
}
}
return false;
}
/**
* 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");
}
/**
* 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);
}
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);
}
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();
}
@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;
}
});
}
/**
* 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);
}
/**
* 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);
}
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();
}
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"));
}
}
public static List<IoBuffer> loadBuffersFromResource ( final Class<?> clazz, final String resourceName ) throws IOException
{
logger.debug ( "Loading buffer - {}", resourceName );
final URL url = Resources.getResource ( clazz, resourceName );
return Resources.readLines ( url, Charset.forName ( "UTF-8" ), new LineProcessor<List<IoBuffer>> () {
private final List<IoBuffer> result = new LinkedList<> ();
private IoBuffer buffer = null;
protected void pushBuffer ()
{
if ( this.buffer == null )
{
return;
}
this.buffer.flip ();
this.result.add ( this.buffer );
this.buffer = null;
}
@Override
public boolean processLine ( String line ) throws IOException
{
line = line.replaceAll ( "#.*", "" ); // clear comments
if ( line.isEmpty () )
{
pushBuffer ();
return true;
}
final String[] toks = line.split ( "\\s+" );
if ( toks.length <= 0 )
{
pushBuffer ();
return true;
}
if ( this.buffer == null )
{
// start a new buffer
this.buffer = IoBuffer.allocate ( 0 );
this.buffer.setAutoExpand ( true );
}
for ( final String tok : toks )
{
this.buffer.put ( Byte.parseByte ( tok, 16 ) );
}
return true;
}
@Override
public List<IoBuffer> getResult ()
{
pushBuffer (); // last chance to add something
return this.result;
}
} );
}
public Builder withStdoutProcessor(LineProcessor<?> stdoutProcessor) {
this.stdoutProcessor = stdoutProcessor;
return this;
}
public Builder withStderrProcessor(LineProcessor<?> stderrProcessor) {
this.stderrProcessor = stderrProcessor;
return this;
}