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

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

Introduction

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

Prototype

OutputSupplier

Source Link

Usage

From source file:co.cask.cdap.data2.util.hbase.HBaseTableUtil.java

public static Location createCoProcessorJar(String filePrefix, Location jarDir,
        Iterable<? extends Class<? extends Coprocessor>> classes) throws IOException {
    StringBuilder buf = new StringBuilder();
    for (Class<? extends Coprocessor> c : classes) {
        buf.append(c.getName()).append(", ");
    }//from w w w . j  a  v  a2  s  . co  m
    if (buf.length() == 0) {
        return null;
    }

    LOG.debug("Creating jar file for coprocessor classes: " + buf.toString());
    final Hasher hasher = Hashing.md5().newHasher();
    final byte[] buffer = new byte[COPY_BUFFER_SIZE];

    final Map<String, URL> dependentClasses = new HashMap<>();
    for (Class<? extends Coprocessor> clz : classes) {
        Dependencies.findClassDependencies(clz.getClassLoader(), new ClassAcceptor() {
            @Override
            public boolean accept(String className, final URL classUrl, URL classPathUrl) {
                // Assuming the endpoint and protocol class doesn't have dependencies
                // other than those comes with HBase, Java and fastutil.
                if (className.startsWith("co.cask") || className.startsWith("it.unimi.dsi.fastutil")) {
                    if (!dependentClasses.containsKey(className)) {
                        dependentClasses.put(className, classUrl);
                    }
                    return true;
                }
                return false;
            }
        }, clz.getName());
    }

    if (!dependentClasses.isEmpty()) {
        LOG.debug("Adding " + dependentClasses.size() + " classes to jar");
        File jarFile = File.createTempFile(filePrefix, ".jar");
        try {
            try (JarOutputStream jarOutput = new JarOutputStream(new FileOutputStream(jarFile))) {
                for (Map.Entry<String, URL> entry : dependentClasses.entrySet()) {
                    try {
                        jarOutput.putNextEntry(
                                new JarEntry(entry.getKey().replace('.', File.separatorChar) + ".class"));

                        try (InputStream inputStream = entry.getValue().openStream()) {
                            int len = inputStream.read(buffer);
                            while (len >= 0) {
                                hasher.putBytes(buffer, 0, len);
                                jarOutput.write(buffer, 0, len);
                                len = inputStream.read(buffer);
                            }
                        }
                    } catch (IOException e) {
                        LOG.info("Error writing to jar", e);
                        throw Throwables.propagate(e);
                    }
                }
            }

            // Copy jar file into HDFS
            // Target path is the jarDir + jarMD5.jar
            final Location targetPath = jarDir.append("coprocessor" + hasher.hash().toString() + ".jar");

            // If the file exists and having same since, assume the file doesn't changed
            if (targetPath.exists() && targetPath.length() == jarFile.length()) {
                return targetPath;
            }

            // Copy jar file into filesystem
            if (!jarDir.mkdirs() && !jarDir.exists()) {
                throw new IOException("Fails to create directory: " + jarDir.toURI());
            }
            Files.copy(jarFile, new OutputSupplier<OutputStream>() {
                @Override
                public OutputStream getOutput() throws IOException {
                    return targetPath.getOutputStream();
                }
            });
            return targetPath;
        } finally {
            jarFile.delete();
        }
    }
    // no dependent classes to add
    return null;
}

From source file:org.akraievoy.couch.CouchDao.java

protected static OutputSupplier<OutputStream> connOutput(final HttpURLConnection connection) {
    return new OutputSupplier<OutputStream>() {
        public OutputStream getOutput() throws IOException {
            return new BufferedOutputStream(connection.getOutputStream());
        }//from  w ww  . j a v a 2 s.c om
    };
}

From source file:com.xebialabs.overthere.OverthereConnectionItestBase.java

/**
 * Tests whether getOutputStream and getInputStream have the right permission behaviour (specifically for SSH/SUDO connections).
 *//*ww  w .j  ava 2 s  .c  o m*/
@Test
@Assumption(methods = { "onUnix", "notLocal" })
public void shouldWriteFileToAndReadFileFromSudoUserHomeDirectoryOnUnix() throws IOException {
    // get handle to file in home dir
    final OverthereFile homeDir = connection.getFile(getUnixHomeDirPath());
    final OverthereFile fileInHomeDir = homeDir.getFile("file" + System.currentTimeMillis() + ".dat");
    assertThat(fileInHomeDir.exists(), equalTo(false));

    // write data to file in home dir
    final byte[] contentsWritten = generateRandomBytes(SMALL_FILE_SIZE);
    ByteStreams.write(contentsWritten, new OutputSupplier<OutputStream>() {
        @Override
        public OutputStream getOutput() throws IOException {
            return fileInHomeDir.getOutputStream();
        }
    });

    assertThat(fileInHomeDir.exists(), equalTo(true));

    // restrict access to file in home dir
    connection.execute(consoleHandler(), CmdLine.build("chmod", "0600", fileInHomeDir.getPath()));

    // read file from home dir
    byte[] contentsRead = new byte[SMALL_FILE_SIZE];
    InputStream in = fileInHomeDir.getInputStream();
    try {
        ByteStreams.readFully(in, contentsRead);
    } finally {
        in.close();
    }
    assertThat(contentsRead, equalTo(contentsWritten));

    // restrict access to file in home dir
    fileInHomeDir.delete();
    assertThat(fileInHomeDir.exists(), equalTo(false));
}

From source file:gov.nih.nci.caarray.util.CaArrayUtils.java

/**
 * returns a byte array representing the input byte array, gzipped. Use this method with caution - for large arrays,
 * you should use streams instead.//from   w  w  w.  jav  a2s  .  com
 * 
 * @param input the content to compress
 * @return the gzipped input
 * @throws IOException if there is a problem zipping the content
 */
public static byte[] gzip(byte[] input) throws IOException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ByteStreams.write(input, new OutputSupplier<OutputStream>() {
        @Override
        public OutputStream getOutput() throws IOException {
            return new GZIPOutputStream(baos);
        }
    });
    baos.close();
    return baos.toByteArray();
}

From source file:com.xebialabs.overthere.OverthereConnectionItestBase.java

private static void writeData(final OverthereFile tempFile, byte[] data) throws IOException {
    ByteStreams.write(data, new OutputSupplier<OutputStream>() {
        @Override/*from   w ww  . j a  v a  2  s.  co  m*/
        public OutputStream getOutput() throws IOException {
            return tempFile.getOutputStream();
        }
    });
}

From source file:com.xebialabs.overthere.OverthereConnectionItestBase.java

protected static byte[] writeRandomBytes(final File f, final int size) throws IOException {
    byte[] randomBytes = generateRandomBytes(size);
    ByteStreams.write(randomBytes, new OutputSupplier<OutputStream>() {
        @Override/*from  ww w .ja v  a  2 s . c  om*/
        public OutputStream getOutput() throws IOException {
            return new FileOutputStream(f);
        }
    });
    return randomBytes;
}

From source file:org.apache.twill.yarn.YarnTwillPreparer.java

private void saveArguments(Arguments arguments, final Path targetPath) throws IOException {
    LOG.debug("Creating {}", targetPath);
    ArgumentsCodec.encode(arguments, new OutputSupplier<Writer>() {
        @Override/*from   w  ww . j av a 2  s.  c om*/
        public Writer getOutput() throws IOException {
            return Files.newBufferedWriter(targetPath, StandardCharsets.UTF_8);
        }
    });
    LOG.debug("Done {}", targetPath);
}

From source file:co.cask.cdap.gateway.handlers.AppFabricHttpHandler.java

/**
 * Saves the {@link SessionInfo} to the filesystem.
 *
 * @param info to be saved.//ww  w. j  a  v a2 s  .com
 * @return true if and only if successful; false otherwise.
 */
private boolean save(SessionInfo info, String accountId) {
    try {
        Gson gson = new GsonBuilder().registerTypeAdapter(Location.class, new LocationCodec(locationFactory))
                .create();
        Location outputDir = locationFactory.create(archiveDir + "/" + accountId);
        if (!outputDir.exists()) {
            return false;
        }
        final Location sessionInfoFile = outputDir.append("session.json");
        OutputSupplier<Writer> writer = new OutputSupplier<Writer>() {
            @Override
            public Writer getOutput() throws IOException {
                return new OutputStreamWriter(sessionInfoFile.getOutputStream(), "UTF-8");
            }
        };

        Writer w = writer.getOutput();
        try {
            gson.toJson(info, w);
        } finally {
            Closeables.closeQuietly(w);
        }
    } catch (IOException e) {
        LOG.warn(e.getMessage(), e);
        return false;
    }
    return true;
}