下面列出了com.google.common.io.CharSource#openBufferedStream ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public CorpusEventLinking loadCorpusEventFrames(final CharSource source)
throws IOException {
int lineNo = 1;
try (final BufferedReader in = source.openBufferedStream()) {
final ImmutableSet.Builder<CorpusEventFrame> ret = ImmutableSet.builder();
String line;
while ((line = in.readLine()) != null) {
if (!line.isEmpty() && !line.startsWith("#")) {
// check for blank or comment lines
ret.add(parseLine(line));
}
}
return CorpusEventLinking.of(ret.build());
} catch (Exception e) {
throw new IOException("Error on line " + lineNo + " of " + source, e);
}
}
private void writeMetaData(JsonOutput out) throws IOException {
CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8);
try (Reader reader = charSource.openBufferedStream();
JsonInput input = json.newInput(reader)) {
input.beginObject();
while (input.hasNext()) {
String name = input.nextName();
switch (name) {
case CAPABILITIES:
case DESIRED_CAPABILITIES:
case REQUIRED_CAPABILITIES:
input.skipValue();
break;
default:
out.name(name);
out.write(input.read(Object.class));
break;
}
}
}
}
private @Nullable Map<String, Object> getAlwaysMatch() throws IOException {
CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8);
try (Reader reader = charSource.openBufferedStream();
JsonInput input = json.newInput(reader)) {
input.beginObject();
while (input.hasNext()) {
String name = input.nextName();
if (CAPABILITIES.equals(name)) {
input.beginObject();
while (input.hasNext()) {
name = input.nextName();
if (ALWAYS_MATCH.equals(name)) {
return input.read(MAP_TYPE);
}
input.skipValue();
}
input.endObject();
} else {
input.skipValue();
}
}
}
return null;
}
private @Nullable Collection<Map<String, Object>> getFirstMatch() throws IOException {
CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8);
try (Reader reader = charSource.openBufferedStream();
JsonInput input = json.newInput(reader)) {
input.beginObject();
while (input.hasNext()) {
String name = input.nextName();
if (CAPABILITIES.equals(name)) {
input.beginObject();
while (input.hasNext()) {
name = input.nextName();
if (FIRST_MATCH.equals(name)) {
return input.read(LIST_OF_MAPS_TYPE);
}
input.skipValue();
}
input.endObject();
} else {
input.skipValue();
}
}
}
return null;
}
private void writeMetaData(JsonOutput out) throws IOException {
CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8);
try (Reader reader = charSource.openBufferedStream();
JsonInput input = json.newInput(reader)) {
input.beginObject();
while (input.hasNext()) {
String name = input.nextName();
switch (name) {
case "capabilities":
case "desiredCapabilities":
case "requiredCapabilities":
input.skipValue();
break;
default:
out.name(name);
out.write(input.read(Object.class));
break;
}
}
}
}
private Map<String, Object> getOss() throws IOException {
CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8);
try (Reader reader = charSource.openBufferedStream();
JsonInput input = json.newInput(reader)) {
input.beginObject();
while (input.hasNext()) {
String name = input.nextName();
if ("desiredCapabilities".equals(name)) {
return input.read(MAP_TYPE);
} else {
input.skipValue();
}
}
}
return null;
}
private Map<String, Object> getAlwaysMatch() throws IOException {
CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8);
try (Reader reader = charSource.openBufferedStream();
JsonInput input = json.newInput(reader)) {
input.beginObject();
while (input.hasNext()) {
String name = input.nextName();
if ("capabilities".equals(name)) {
input.beginObject();
while (input.hasNext()) {
name = input.nextName();
if ("alwaysMatch".equals(name)) {
return input.read(MAP_TYPE);
} else {
input.skipValue();
}
}
input.endObject();
} else {
input.skipValue();
}
}
}
return null;
}
private Collection<Map<String, Object>> getFirstMatches() throws IOException {
CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8);
try (Reader reader = charSource.openBufferedStream();
JsonInput input = json.newInput(reader)) {
input.beginObject();
while (input.hasNext()) {
String name = input.nextName();
if ("capabilities".equals(name)) {
input.beginObject();
while (input.hasNext()) {
name = input.nextName();
if ("firstMatch".equals(name)) {
return input.read(LIST_OF_MAPS_TYPE);
} else {
input.skipValue();
}
}
input.endObject();
} else {
input.skipValue();
}
}
}
return null;
}
public static BufferedReader asBufferedReader(String resource) {
URL url = com.google.common.io.Resources.getResource(resource);
try {
CharSource charSource = com.google.common.io.Resources.asCharSource(url, Charsets.UTF_8);
return charSource.openBufferedStream();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
public static BufferedReader asBufferedReader(String resource) {
URL url = com.google.common.io.Resources.getResource(resource);
try {
CharSource charSource = com.google.common.io.Resources.asCharSource(url, Charsets.UTF_8);
return charSource.openBufferedStream();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
private void process(Context context, String templateName, String targetPath) throws IOException {
URL templateUrl = Resources.getResource(IpmiCommandNameTest.class, templateName);
CharSource source = Resources.asCharSource(templateUrl, StandardCharsets.UTF_8);
File file = new File(targetPath);
CharSink sink = Files.asCharSink(file, StandardCharsets.UTF_8);
try (Reader r = source.openBufferedStream()) {
try (Writer w = sink.openBufferedStream()) {
engine.evaluate(context, w, file.getName(), r);
}
}
}
private @Nullable Map<String, Object> getOss() throws IOException {
CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8);
try (Reader reader = charSource.openBufferedStream();
JsonInput input = json.newInput(reader)) {
input.beginObject();
while (input.hasNext()) {
String name = input.nextName();
if (DESIRED_CAPABILITIES.equals(name)) {
return input.read(MAP_TYPE);
}
input.skipValue();
}
}
return null;
}
/**
* Finds the separator used by the specified CSV file.
* <p>
* The search includes comma, semicolon, colon, tab and pipe (in that order of priority).
* <p>
* The algorithm operates in a number of steps.
* Firstly, it looks for occurrences where a separator is followed by valid quoted text.
* If this matches, the separator is assumed to be correct.
* Secondly, it looks for lines that only consist of a separator.
* If this matches, the separator is assumed to be correct.
* Thirdly, it looks to see which separator is the most common on the line.
* If that separator is also the most common on the next line, and the number of columns matches,
* the separator is assumed to be correct. Otherwise another line is processed.
* Thus to match a separator, there must be two lines with the same number of columns.
* At most, 100 content lines are read from the file.
* The default is comma if the file is empty.
*
* @param source the source to read as CSV
* @return the CSV file
* @throws UncheckedIOException if an IO exception occurs
* @throws IllegalArgumentException if the file cannot be parsed
*/
public static char findSeparator(CharSource source) {
String possibleSeparators = ",;\t:|";
try (BufferedReader breader = source.openBufferedStream()) {
int bestCount = 0;
char bestSeparator = ',';
String line = breader.readLine();
int contentLines = 0;
while (line != null && contentLines <= 100) {
if (line.length() == 0 || line.startsWith("#")) {
// comment
} else if (line.startsWith(";") && bestCount == 0 && bestSeparator == ',') {
// if we see semicolon it could be a start of comment or a semicolon separator
bestSeparator = ';';
} else {
line = simplifyLine(line);
int lineBestCount = 0;
char lineBestSeparator = ',';
for (char separator : possibleSeparators.toCharArray()) {
// a quote following a separator is a strong marker for the separator
if (line.contains(separator + "\"\"")) {
return separator;
}
// a line only formed of separators is a strong marker for the separator
if (line.length() > 1 && line.equals(Strings.repeat(Character.toString(separator), line.length()))) {
return separator;
}
// a minimal line of a separator is a weak marker for the separator
if (line.length() == 1 && line.charAt(0) == separator && bestCount == 0 && bestSeparator == ',') {
bestCount = 1;
bestSeparator = separator;
}
// parse the row and see if it is the best match
if (line.length() > 1) {
int count = CsvFile.parseLine(line, separator).size();
if (count > lineBestCount) {
lineBestCount = count;
lineBestSeparator = separator;
}
}
}
if (lineBestCount > 0) {
contentLines++;
if (bestCount > 0 && bestCount == lineBestCount && bestSeparator == lineBestSeparator) {
break;
}
bestCount = lineBestCount;
bestSeparator = lineBestSeparator;
}
}
line = breader.readLine();
}
return bestSeparator;
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}