Example usage for com.google.common.io OutputSupplier getOutput

List of usage examples for com.google.common.io OutputSupplier getOutput

Introduction

In this page you can find the example usage for com.google.common.io OutputSupplier getOutput.

Prototype

T getOutput() throws IOException;

Source Link

Document

Returns an object that encapsulates a writable resource.

Usage

From source file:org.apache.twill.internal.json.ArgumentsCodec.java

public static void encode(Arguments arguments, OutputSupplier<? extends Writer> writerSupplier)
        throws IOException {
    try (Writer writer = writerSupplier.getOutput()) {
        GSON.toJson(arguments, writer);/*from w  w w  .  j av a2 s  . c o  m*/
    }
}

From source file:com.continuuity.weave.internal.json.ArgumentsCodec.java

public static void encode(Arguments arguments, OutputSupplier<? extends Writer> writerSupplier)
        throws IOException {
    Writer writer = writerSupplier.getOutput();
    try {//from  w  w  w .ja  v  a2  s.  c  o  m
        GSON.toJson(arguments, writer);
    } finally {
        writer.close();
    }
}

From source file:org.apache.twill.internal.json.JvmOptionsCodec.java

public static void encode(JvmOptions jvmOptions, OutputSupplier<? extends Writer> writerSupplier)
        throws IOException {
    try (Writer writer = writerSupplier.getOutput()) {
        GSON.toJson(jvmOptions, writer);
    }//from  w  ww. ja  v a 2s  . c o m
}

From source file:cc.recommenders.utils.Zips.java

public static void zip(File directory, File out) throws IOException {
    ZipOutputStream zos = null;//w ww  .  j a  v  a  2 s .c o  m
    try {
        OutputSupplier<FileOutputStream> s = Files.newOutputStreamSupplier(out);
        zos = new ZipOutputStream(s.getOutput());
        Collection<File> files = FileUtils.listFiles(directory, FILE, DIRECTORY);
        for (File f : files) {
            String path = removeStart(f.getPath(), directory.getAbsolutePath() + "/");
            ZipEntry e = new ZipEntry(path);
            zos.putNextEntry(e);
            byte[] data = Files.toByteArray(f);
            zos.write(data);
            zos.closeEntry();
        }
    } finally {
        Closeables.close(zos, false);
    }
}

From source file:org.eclipse.epp.internal.logging.aeri.ui.utils.Zips.java

public static void zip(File directory, File out) throws IOException {
    ZipOutputStream zos = null;/*from  ww w . ja  va 2s.co m*/
    try {
        OutputSupplier<FileOutputStream> s = Files.newOutputStreamSupplier(out);
        zos = new ZipOutputStream(s.getOutput());
        for (File f : FileUtils.listFiles(directory, FILE, DIRECTORY)) {
            String path = removeStart(f.getPath(), directory.getAbsolutePath() + File.separator);
            path = path.replace(File.separatorChar, '/');
            ZipEntry e = new ZipEntry(path);
            zos.putNextEntry(e);
            Files.asByteSource(f).copyTo(zos);
            zos.closeEntry();
        }
    } finally {
        Closeables.close(zos, false);
    }
}

From source file:org.asoem.greyfish.utils.persistence.Persisters.java

public static void serialize(final Persister persister, final Object object,
        final OutputSupplier<? extends OutputStream> outputSupplier) throws IOException {
    final OutputStream output = outputSupplier.getOutput();
    boolean threw = true;
    try {/*w w  w .j  av a  2s .  co  m*/
        persister.serialize(object, output);
        threw = false;
    } finally {
        Closeables.close(output, threw);
    }
}

From source file:co.cask.cdap.internal.app.runtime.batch.distributed.ContainerLauncherGenerator.java

/**
 * Generates a JAR file for launching MapReduce containers. The generated jar contains three classes inside
 *
 * <ul>//from w ww . j a v a 2  s . co m
 *   <li>{@link MRAppMaster}</li>
 *   <li>{@link org.apache.hadoop.mapred.YarnChild YarnChild}</li>
 *   <li>{@link MapReduceContainerLauncher}</li>
 * </ul>
 *
 * @see MapReduceContainerLauncher
 */
public static void generateLauncherJar(String launcherClassPath, String classLoaderName,
        OutputSupplier<? extends OutputStream> outputSupplier) throws IOException {
    try (JarOutputStream output = new JarOutputStream(outputSupplier.getOutput())) {
        generateLauncherClass(launcherClassPath, classLoaderName, MRAppMaster.class.getName(), output);
        generateLauncherClass(launcherClassPath, classLoaderName, "org.apache.hadoop.mapred.YarnChild", output);

        // Includes the launcher class in the JAR as well. No need to trace dependency as the launcher
        // class must be dependency free.
        String containerLauncherName = Type.getInternalName(MapReduceContainerLauncher.class) + ".class";
        output.putNextEntry(new JarEntry(containerLauncherName));
        URL launcherURL = ContainerLauncherGenerator.class.getClassLoader().getResource(containerLauncherName);

        // Can never be null
        Preconditions.checkState(launcherURL != null);
        Resources.copy(launcherURL, output);
    }
}

From source file:org.eclipse.recommenders.utils.Zips.java

public static void zip(File directory, File out) throws IOException {
    ZipOutputStream zos = null;/*from ww w.  j av a 2s.  c  om*/
    try {
        OutputSupplier<FileOutputStream> s = Files.newOutputStreamSupplier(out);
        zos = new ZipOutputStream(s.getOutput());
        for (File f : FileUtils.listFiles(directory, FILE, DIRECTORY)) {
            String path = removeStart(f.getPath(), directory.getAbsolutePath() + "/");
            ZipEntry e = new ZipEntry(path);
            zos.putNextEntry(e);
            byte[] data = Files.toByteArray(f);
            zos.write(data);
            zos.closeEntry();
        }
    } finally {
        Closeables.close(zos, false);
    }
}

From source file:io.druid.segment.MetricHolder.java

public static void writeComplexMetric(OutputSupplier<? extends OutputStream> outSupplier, String name,
        String typeName, GenericIndexedWriter column) throws IOException {
    try (OutputStream out = outSupplier.getOutput()) {
        out.write(version);//ww  w .java  2  s .  c  om
        serializerUtils.writeString(out, name);
        serializerUtils.writeString(out, typeName);

        final InputSupplier<InputStream> supplier = column.combineStreams();
        try (InputStream in = supplier.getInput()) {
            ByteStreams.copy(in, out);
        }
    }
}

From source file:com.metamx.druid.index.v1.MetricHolder.java

public static void writeComplexMetric(OutputSupplier<? extends OutputStream> outSupplier, String name,
        String typeName, GenericIndexedWriter column) throws IOException {
    OutputStream out = null;//from w ww.ja  va 2 s  . co  m
    InputStream in = null;

    try {
        out = outSupplier.getOutput();

        out.write(version);
        serializerUtils.writeString(out, name);
        serializerUtils.writeString(out, typeName);

        final InputSupplier<InputStream> supplier = column.combineStreams();
        in = supplier.getInput();

        ByteStreams.copy(in, out);
    } finally {
        Closeables.closeQuietly(out);
        Closeables.closeQuietly(in);
    }
}