下面列出了com.google.common.io.CharStreams#readLines ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Deserializes the variable map from a byte array returned by
* {@link #toBytes()}.
*/
public static VariableMap fromBytes(byte[] bytes) throws ParseException {
Iterable<String> lines;
try {
lines = CharStreams.readLines(CharStreams.newReaderSupplier(
ByteStreams.newInputStreamSupplier(bytes), Charsets.UTF_8));
} catch (IOException e) {
// Note: An IOException is never thrown while reading from a byte array.
// This try/catch is just here to appease the Java compiler.
throw new RuntimeException(e);
}
ImmutableMap.Builder<String, String> map = ImmutableMap.builder();
for (String line : lines) {
int pos = findIndexOfChar(line, SEPARATOR);
if (pos <= 0 || pos == line.length() - 1) {
throw new ParseException("Bad line: " + line, 0);
}
map.put(
unescape(line.substring(0, pos)),
unescape(line.substring(pos + 1)));
}
return new VariableMap(map.build());
}
private void loadDictionary(Reader reader) throws IOException {
for (String line : CharStreams.readLines(reader)) {
line = line.trim();
if (line.length() == 0) {
continue; // ignore empty lines
}
if (line.startsWith("#")) {
continue; // ignore comment lines
}
int i = line.indexOf(" ");
if (i < 0) {
throw new MorphlineCompilationException("Dictionary entry line must contain a space to separate name and value: " + line, getConfig());
}
if (i == 0) {
throw new MorphlineCompilationException("Dictionary entry line must contain a name: " + line, getConfig());
}
String name = line.substring(0, i);
String value = line.substring(i + 1, line.length()).trim();
if (value.length() == 0) {
throw new MorphlineCompilationException("Dictionary entry line must contain a value: " + line, getConfig());
}
dictionary.put(name, value);
}
}
/**
* Parse the specified readable.
*
* @param readable readable, must not be null
* @param listener low-level event based parser callback, must not be null
* @throws IOException if an I/O error occurs
*/
static void parse(final Readable readable, final ParseListener listener)
throws IOException
{
if (readable == null)
{
throw new IllegalArgumentException("readable must not be null");
}
FastqParserLineProcessor lineProcessor = new FastqParserLineProcessor(listener);
CharStreams.readLines(readable, lineProcessor);
if (lineProcessor.getState() == State.COMPLETE)
{
listener.complete();
lineProcessor.setState(State.DESCRIPTION);
}
if (lineProcessor.getState() != State.DESCRIPTION)
{
throw new IOException("truncated sequence"); // at line " + lineNumber);
}
}
@Test
public void testMetaList() throws IOException {
Path outJar = tempDir.newFile("test.jar").toPath();
Map<String, String> fileToClassName;
try (ZipOutputStream zipOut =
new ZipOutputStream(new BufferedOutputStream(Files.newOutputStream(outJar)))) {
fileToClassName =
ImmutableMap.of(
"com/facebook/foo.class", "com.facebook.foo",
"bar.class", "bar");
for (String entry : fileToClassName.keySet()) {
zipOut.putNextEntry(new ZipEntry(entry));
zipOut.write(new byte[] {0});
}
}
StringWriter stringWriter = new StringWriter();
try (BufferedWriter writer = new BufferedWriter(stringWriter)) {
ImmutableSet<APKModule> requires = ImmutableSet.of();
SplitZipStep.writeMetaList(
writer,
APKModule.of(SplitZipStep.SECONDARY_DEX_ID, false),
requires,
ImmutableList.of(outJar),
DexStore.JAR);
}
List<String> lines = CharStreams.readLines(new StringReader(stringWriter.toString()));
assertEquals(1, lines.size());
String line = Iterables.getLast(lines, null);
String[] data = line.split(" ");
assertEquals(3, data.length);
// Note that we cannot test data[1] (the hash) because zip files change their hash each
// time they are written due to timestamps written into the file.
assertEquals("secondary-1.dex.jar", data[0]);
assertTrue(
String.format("Unexpected class: %s", data[2]), fileToClassName.values().contains(data[2]));
}
protected List<ResourceLocation> getPlatforms()
{
if (platforms.size() == 0)
{
for (ModContainer mc : Loader.instance().getModList())
{
File src = mc.getSource();
if (src == null)
continue;
InputStream is = getClass().getResourceAsStream("/assets/" + mc.getModId() + "/structures/sky_block_platforms.txt");
if (is == null)
continue;
try
{
for (String line : CharStreams.readLines(new InputStreamReader(is)))
{
if (getClass().getResourceAsStream("/assets/" + mc.getModId() + "/structures/" + line + ".nbt") != null)
platforms.add(new ResourceLocation(mc.getModId(), line));
}
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for (File f : YUNoMakeGoodMap.instance.getStructFolder().listFiles())
{
if (!f.isFile() || !f.getName().endsWith(".nbt"))
continue;
platforms.add(new ResourceLocation("/config/", f.getName().substring(0, f.getName().length() - 4)));
}
}
return platforms;
}
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"));
}
}
@Test
public void writesListOfAsynchronousProcessAsPlainText() throws Exception {
List<AsynchronousProcess> processes = newArrayList(new AsynchronousProcess("andrew", 1L, "/a", "running"),
new AsynchronousProcess("user", 2L, "/b", "done"));
List<List<String>> expectedProcessesTable = newArrayList(newArrayList("USER", "ID", "STAT", "PATH"),
newArrayList("andrew", "1", "running", "/a"),
newArrayList("user", "2", "done", "/b"));
ByteArrayOutputStream bout = new ByteArrayOutputStream();
processListWriter.writeTo(processes, List.class,
newParameterizedType(List.class, AsynchronousProcess.class),
new Annotation[0],
MediaType.TEXT_PLAIN_TYPE,
new MultivaluedHashMap<>(),
bout);
List<String> lines = CharStreams.readLines(new StringReader(bout.toString()));
assertEquals(3, lines.size());
Pattern pattern = Pattern.compile("(\\w+)\\s+(\\w+)\\s+(\\w+)\\s+(/?\\w+)");
List<List<String>> processesTable = newArrayList();
for (String line : lines) {
Matcher matcher = pattern.matcher(line);
assertTrue(String.format("String '%s' is not matched to pattern", line), matcher.matches());
processesTable.add(getAllGroups(matcher));
}
assertEquals(expectedProcessesTable, processesTable);
}
static
private List<String> loadStopWords(String stopWords){
InputStream is = CountVectorizer.class.getResourceAsStream("/stop_words/" + stopWords + ".txt");
if(is == null){
throw new IllegalArgumentException(stopWords);
}
try(Reader reader = new InputStreamReader(is, "UTF-8")){
return CharStreams.readLines(reader);
} catch(IOException ioe){
throw new IllegalArgumentException(stopWords, ioe);
}
}
static
public Iterator<String> parseText(InputStream is) throws IOException {
Reader reader = new InputStreamReader(is, "US-ASCII");
List<String> lines = CharStreams.readLines(reader);
return lines.iterator();
}
private List<String> readLines(URL url) {
try (InputStreamReader in = new InputStreamReader(url.openStream(), UTF_8)) {
return CharStreams.readLines(in);
} catch (IOException e) {
throw new PlantUmlParseException("Could not parse diagram from " + url, e);
}
}
private int countLogFileMatches(final String url, final String searchTerm) throws Exception
{
HttpURLConnection httpCon = getHelper().openManagementConnection(url, "GET");
httpCon.connect();
try (InputStreamReader r = new InputStreamReader(httpCon.getInputStream()))
{
final List<String> strings = CharStreams.readLines(r);
return strings.stream().map(line -> line.contains(searchTerm)).collect(Collectors.toList()).size();
}
}
private List<String> readLines(InputStream is) {
try {
return CharStreams.readLines(new InputStreamReader(is));
} catch (IOException e) {
e.printStackTrace();
// TODO(kalman): this is a bit hacky, deal with it properly.
return Collections.singletonList("(Error, couldn't read lines from the input stream. " +
"Try running the command external to PST to view the output.)");
}
}
static
private Iterator<String> parseFeatureMap(InputStream is) throws IOException {
Reader reader = new InputStreamReader(is, "UTF-8");
List<String> lines = CharStreams.readLines(reader);
return lines.iterator();
}
/**
* Returns ciphers from the hard-coded list of "reasonable" default ciphers in {@link #DEFAULT_CIPHERS_LIST_RESOURCE}.
*
* @return ciphers from the {@link #DEFAULT_CIPHERS_LIST_RESOURCE}
*/
public static List<String> getBuiltInCipherList() {
try (InputStream cipherListStream = SslUtil.class.getResourceAsStream(DEFAULT_CIPHERS_LIST_RESOURCE)) {
if (cipherListStream == null) {
return Collections.emptyList();
}
Reader reader = new InputStreamReader(cipherListStream, Charset.forName("UTF-8"));
return CharStreams.readLines(reader);
} catch (IOException e) {
return Collections.emptyList();
}
}
private GBDT loadGBDT(){
String handle = getHandle();
try(StringReader reader = new StringReader(handle)){
List<String> lines = CharStreams.readLines(reader);
return LightGBMUtil.loadGBDT(lines.iterator());
} catch(IOException ioe){
throw new RuntimeException(ioe);
}
}
@Override
public void run() throws IOException {
Set<String> labels = new TreeSet<>();
for (String label :
mainParameters.isEmpty()
? CharStreams.readLines(new InputStreamReader(stdin, UTF_8))
: Files.readLines(new File(mainParameters.get(0)), UTF_8)) {
label = label.trim();
if (label.startsWith("-")) {
label = label.substring(1);
}
if (label.endsWith("-")) {
label = label.substring(0, label.length() - 1);
}
String canonical = canonicalize(label);
if (canonical.startsWith(DomainNameUtils.ACE_PREFIX)
&& Idn.toUnicode(canonical).equals(canonical)) {
System.err.println("Bad IDN: " + label);
continue; // Bad IDN code points.
}
labels.add(canonical);
if (!canonical.startsWith("xn--")) {
// Using both "" and "-" to canonicalize labels.
labels.add(canonicalize(label.replaceAll(" ", "")));
labels.add(canonicalize(label.replaceAll(" ", "-")));
labels.add(canonicalize(label.replaceAll("_", "")));
labels.add(canonicalize(label.replaceAll("_", "-")));
}
}
labels.remove(""); // We used "" for invalid labels.
System.out.println(Joiner.on('\n').join(labels));
}
private List<String> readLines(InputStream is) {
try {
return CharStreams.readLines(new InputStreamReader(is));
} catch (IOException e) {
e.printStackTrace();
// TODO(kalman): this is a bit hacky, deal with it properly.
return Collections.singletonList("(Error, couldn't read lines from the input stream. " +
"Try running the command external to PST to view the output.)");
}
}
/**
* 简单读取Reader的每行内容到List<String>
*/
public static List<String> toLines(final InputStream input) throws IOException {
return CharStreams.readLines(new BufferedReader(new InputStreamReader(input, Charsets.UTF_8)));
}
/**
* 简单读取Reader的每行内容到List<String>
*/
public static List<String> toLines(final InputStream input) throws IOException {
return CharStreams.readLines(new BufferedReader(new InputStreamReader(input, Charsets.UTF_8)));
}
/**
* 简单读取Reader的每行内容到List<String>
*
* @see {@link CharStreams#readLines}
*/
public static List<String> toLines(final Reader input) throws IOException {
return CharStreams.readLines(toBufferedReader(input));
}