Java Code Examples for com.google.common.io.CharSink#write()

The following examples show how to use com.google.common.io.CharSink#write() . 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: MethodsListGenerator.java    From google-ads-java with Apache License 2.0 6 votes vote down vote up
public static void main(String args[])
    throws ClassNotFoundException, IOException, NoSuchMethodException {
  Method latestVersionMethod = GoogleAdsAllVersions.class.getMethod("getLatestVersion");
  Class cls = Class.forName(latestVersionMethod.getReturnType().getName());
  List<Method> methods = Arrays.asList(cls.getDeclaredMethods());

  StringBuilder output = new StringBuilder();
  for (Method method : methods) {
    output.append(method + "\n");
  }

  System.out.println("Writing the following methods to file:");
  System.out.printf(output.toString());

  File file = new File("./google-ads/src/test/resources/testdata/avail_service_clients.txt");
  CharSink sink = Files.asCharSink(file, Charsets.UTF_8);
  sink.write(output);
}
 
Example 2
Source File: DefaultCorpusQueryWriter.java    From tac-kbp-eal with MIT License 6 votes vote down vote up
@Override
public void writeQueries(final CorpusQuerySet2016 queries,
    final CharSink sink) throws IOException {
  final StringBuilder sb = new StringBuilder();

  int numEntryPoints = 0;
  for (final CorpusQuery2016 query : QUERY_ORDERING.sortedCopy(queries)) {
    for (final CorpusQueryEntryPoint entryPoint : ENTRY_POINT_ORDERING
        .sortedCopy(query.entryPoints())) {
      sb.append(entryPointString(query, entryPoint)).append("\n");
      ++numEntryPoints;
    }
  }
  log.info("Writing {} queries with {} entry points to {}", queries.queries().size(),
      numEntryPoints, sink);
  sink.write(sb.toString());
}
 
Example 3
Source File: JavaXToWriterUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenUsingGuava_whenConvertingByteArrayIntoWriter_thenCorrect() throws IOException {
    final byte[] initialArray = "With Guava".getBytes();

    final String buffer = new String(initialArray);
    final StringWriter stringWriter = new StringWriter();
    final CharSink charSink = new CharSink() {
        @Override
        public final Writer openStream() throws IOException {
            return stringWriter;
        }
    };
    charSink.write(buffer);

    stringWriter.close();

    assertEquals("With Guava", stringWriter.toString());
}
 
Example 4
Source File: Formatter.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Format the given input (a Java compilation unit) into the output stream.
 *
 * @throws FormatterException if the input cannot be parsed
 */
public void formatSource(CharSource input, CharSink output)
        throws FormatterException, IOException {
    // TODO(cushon): proper support for streaming input/output. Input may
    // not be feasible (parsing) but output should be easier.
    output.write(formatSource(input.read()));
}
 
Example 5
Source File: Formatter.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Format the given input (a Java compilation unit) into the output stream.
 *
 * @throws FormatterException if the input cannot be parsed
 */
public void formatSource(CharSource input, CharSink output)
        throws FormatterException, IOException {
    // TODO(cushon): proper support for streaming input/output. Input may
    // not be feasible (parsing) but output should be easier.
    output.write(formatSource(input.read()));
}
 
Example 6
Source File: Formatter.java    From google-java-format with Apache License 2.0 5 votes vote down vote up
/**
 * Format the given input (a Java compilation unit) into the output stream.
 *
 * @throws FormatterException if the input cannot be parsed
 */
public void formatSource(CharSource input, CharSink output)
    throws FormatterException, IOException {
  // TODO(cushon): proper support for streaming input/output. Input may
  // not be feasible (parsing) but output should be easier.
  output.write(formatSource(input.read()));
}
 
Example 7
Source File: KBPAssessmentDiff.java    From tac-kbp-eal with MIT License 5 votes vote down vote up
private static void logCoverage(String leftName, Set<Symbol> leftDocIds, String rightName,
    Set<Symbol> rightDocIds,
    CharSink out) throws IOException {
  final String msg = String.format(
      "%d documents in %s; %d in %s. %d in common, %d left-only, %d right-only",
      leftDocIds.size(), leftName, rightDocIds.size(), rightName,
      Sets.intersection(leftDocIds, rightDocIds).size(),
      Sets.difference(leftDocIds, rightDocIds).size(),
      Sets.difference(rightDocIds, leftDocIds).size());
  log.info(msg);
  out.write(msg);
}
 
Example 8
Source File: GuavaIOUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenWriteUsingCharSink_thenWritten() throws IOException {
    final String expectedValue = "Hello world";
    final File file = new File("src/test/resources/test.out");
    final CharSink sink = Files.asCharSink(file, Charsets.UTF_8);

    sink.write(expectedValue);

    final String result = Files.toString(file, Charsets.UTF_8);
    assertEquals(expectedValue, result);
}
 
Example 9
Source File: AppendToFileManualTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenAppendToFileUsingGuava_thenCorrect() throws IOException {
    File file = new File(fileName);
    CharSink chs = com.google.common.io.Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND);
    chs.write("Spain\r\n");

    assertThat(StreamUtils.getStringFromInputStream(new FileInputStream(fileName))).isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
}
 
Example 10
Source File: FMT.java    From fmt-maven-plugin with MIT License 2 votes vote down vote up
/**
 * Hook called when the processd file is not compliant with the formatter.
 *
 * @param file the file that is not compliant
 * @param formatted the corresponding formatted of the file.
 */
@Override
protected void onNonComplyingFile(File file, String formatted) throws IOException {
  CharSink sink = Files.asCharSink(file, Charsets.UTF_8);
  sink.write(formatted);
}