Example usage for com.google.common.io CharSource copyTo

List of usage examples for com.google.common.io CharSource copyTo

Introduction

In this page you can find the example usage for com.google.common.io CharSource copyTo.

Prototype

public long copyTo(CharSink sink) throws IOException 

Source Link

Document

Copies the contents of this source to the given sink.

Usage

From source file:net.derquinse.common.io.DurableFiles.java

/**
 * Copies to a file all characters from a {@link CharSource} and {@link Closeable} object supplied
 * by a factory, using the given character set. The file is sync'd before being closed.
 * @param from source data/*from   w w w. ja v a  2s  .co  m*/
 * @param to the destination file
 * @param charset the character set used when writing the file
 * @throws IOException if an I/O error occurs
 */
public static <R extends Readable & Closeable> void copy(CharSource from, File to, Charset charset)
        throws IOException {
    checkNotNull(from);
    checkNotNull(to);
    checkNotNull(charset);
    boolean threw = true;
    FileOutputStream os = new FileOutputStream(to);
    try {
        OutputStreamWriter w = new OutputStreamWriter(os, charset);
        try {
            from.copyTo(w);
            w.flush();
            os.flush();
            os.getFD().sync();
            threw = false;
        } finally {
            Closeables.close(w, threw);
        }
    } finally {
        Closeables.close(os, threw);
    }
}

From source file:org.glowroot.local.store.CappedDatabase.java

long write(CharSource charSource) throws IOException {
    synchronized (lock) {
        if (closing) {
            return -1;
        }/*from www  . j a  va2 s.  c o m*/
        out.startBlock();
        Writer compressedWriter = new OutputStreamWriter(new LZFOutputStream(out), Charsets.UTF_8);
        charSource.copyTo(compressedWriter);
        compressedWriter.flush();
        return out.endBlock();
    }
}