Java Code Examples for com.google.common.io.Files#asByteSink()

The following examples show how to use com.google.common.io.Files#asByteSink() . 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: JarFileServiceWrapper.java    From bistoury with GNU General Public License v3.0 6 votes vote down vote up
private void unPackJar(final String jarFilePath, final File target) {
    try (JarFile jarFile = new JarFile(URLUtil.removeProtocol(jarFilePath))) {
        Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            if (entry.isDirectory()) {
                new File(target, entry.getName()).mkdirs();
            } else {
                File file = new File(target, entry.getName());
                if (file.createNewFile()) {
                    try (InputStream inputStream = jarFile.getInputStream(entry)) {
                        ByteSink byteSink = Files.asByteSink(file);
                        byteSink.writeFrom(inputStream);
                    }

                }
            }

        }
    } catch (Exception e) {
        logger.error("", "unpack jar error", e);
    }
}
 
Example 2
Source File: GuavaIOUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenWriteUsingByteSink_thenWritten() throws IOException {
    final String expectedValue = "Hello world";
    final File file = new File("src/test/resources/test.out");
    final ByteSink sink = Files.asByteSink(file);

    sink.write(expectedValue.getBytes());

    final String result = Files.toString(file, Charsets.UTF_8);
    assertEquals(expectedValue, result);
}
 
Example 3
Source File: FstInputOutput.java    From jopenfst with MIT License 4 votes vote down vote up
public static void writeFstToBinaryFile(Fst fst, File file) throws IOException {
  ByteSink bs = Files.asByteSink(file);
  try (ObjectOutputStream oos = new ObjectOutputStream(new GZIPOutputStream(bs.openBufferedStream()))) {
    writeFstToBinaryStream(fst, oos);
  }
}