Java Code Examples for com.google.common.io.CharSource#openBufferedStream()

The following examples show how to use com.google.common.io.CharSource#openBufferedStream() . 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: CorpusEventFrameLoader2016.java    From tac-kbp-eal with MIT License 6 votes vote down vote up
@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);
  }
}
 
Example 2
Source File: NewAppiumSessionPayload.java    From java-client with Apache License 2.0 6 votes vote down vote up
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;
            }
        }
    }
}
 
Example 3
Source File: NewAppiumSessionPayload.java    From java-client with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: NewAppiumSessionPayload.java    From java-client with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 5
Source File: NewSessionPayload.java    From selenium with Apache License 2.0 6 votes vote down vote up
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;
      }
    }
  }
}
 
Example 6
Source File: NewSessionPayload.java    From selenium with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 7
Source File: NewSessionPayload.java    From selenium with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 8
Source File: NewSessionPayload.java    From selenium with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 9
Source File: Resources.java    From StubbornJava with MIT License 5 votes vote down vote up
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);
    }
}
 
Example 10
Source File: Resources.java    From StubbornJava with MIT License 5 votes vote down vote up
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);
    }
}
 
Example 11
Source File: IpmiCommandNameTest.java    From ipmi4j with Apache License 2.0 5 votes vote down vote up
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);
        }
    }
}
 
Example 12
Source File: NewAppiumSessionPayload.java    From java-client with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 13
Source File: CsvFile.java    From Strata with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
  }
}