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

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

Introduction

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

Prototype

public long copyTo(ByteSink sink) throws IOException 

Source Link

Document

Copies the contents of this byte source to the given ByteSink .

Usage

From source file:org.locationtech.geogig.storage.bdbje.EnvironmentBuilder.java

/**
 * @return/*  w w  w  .  jav a 2  s  .c  o m*/
 * @see com.google.inject.Provider#get()
 */
@Override
public synchronized Environment get() {

    final Optional<URL> repoUrl = new ResolveGeogigDir(platform).call();
    if (!repoUrl.isPresent() && absolutePath == null) {
        throw new IllegalStateException("Can't find geogig repository home");
    }
    final File storeDirectory;

    if (absolutePath != null) {
        storeDirectory = absolutePath;
    } else {
        File currDir;
        try {
            currDir = new File(repoUrl.get().toURI());
        } catch (URISyntaxException e) {
            throw Throwables.propagate(e);
        }
        File dir = currDir;
        for (String subdir : path) {
            dir = new File(dir, subdir);
        }
        storeDirectory = dir;
    }

    if (!storeDirectory.exists() && !storeDirectory.mkdirs()) {
        throw new IllegalStateException(
                "Unable to create Environment directory: '" + storeDirectory.getAbsolutePath() + "'");
    }
    EnvironmentConfig envCfg;
    if (this.forceConfig == null) {
        File conf = new File(storeDirectory, "je.properties");
        if (!conf.exists()) {
            String resource = stagingDatabase ? "je.properties.staging" : "je.properties.objectdb";
            ByteSource from = Resources.asByteSource((getClass().getResource(resource)));
            try {
                from.copyTo(Files.asByteSink(conf));
            } catch (IOException e) {
                Throwables.propagate(e);
            }
        }

        // use the default settings
        envCfg = new EnvironmentConfig();
        envCfg.setAllowCreate(true);
        envCfg.setCacheMode(CacheMode.MAKE_COLD);
        envCfg.setLockTimeout(5, TimeUnit.SECONDS);
        envCfg.setDurability(Durability.COMMIT_SYNC);
        // envCfg.setReadOnly(readOnly);
    } else {
        envCfg = this.forceConfig;
    }

    // // envCfg.setSharedCache(true);
    // //
    // final boolean transactional = false;
    // envCfg.setTransactional(transactional);
    // envCfg.setCachePercent(75);// Use up to 50% of the heap size for the shared db cache
    // envCfg.setConfigParam(EnvironmentConfig.LOG_FILE_MAX, String.valueOf(256 * 1024 * 1024));
    // // check <http://www.oracle.com/technetwork/database/berkeleydb/je-faq-096044.html#35>
    // envCfg.setConfigParam("je.evictor.lruOnly", "false");
    // envCfg.setConfigParam("je.evictor.nodesPerScan", "100");
    //
    // envCfg.setConfigParam(EnvironmentConfig.CLEANER_MIN_UTILIZATION, "25");
    // envCfg.setConfigParam(EnvironmentConfig.CHECKPOINTER_HIGH_PRIORITY, "true");
    //
    // envCfg.setConfigParam(EnvironmentConfig.CLEANER_THREADS, "4");
    // // TODO: check whether we can set is locking to false
    // envCfg.setConfigParam(EnvironmentConfig.ENV_IS_LOCKING, String.valueOf(transactional));
    //
    // envCfg.setConfigParam(EnvironmentConfig.ENV_RUN_CHECKPOINTER,
    // String.valueOf(!transactional));
    // envCfg.setConfigParam(EnvironmentConfig.ENV_RUN_CLEANER, String.valueOf(!transactional));
    //
    // // envCfg.setConfigParam(EnvironmentConfig.ENV_RUN_EVICTOR, "false");

    Environment env;
    try {
        env = new Environment(storeDirectory, envCfg);
    } catch (RuntimeException lockedEx) {
        // lockedEx.printStackTrace();
        if (readOnly) {
            // this happens when trying to open the env in read only mode when its already open
            // in read/write mode inside the same process. So we re-open it read-write but the
            // database itself will be open read-only by JEObjectDatabase.
            envCfg.setReadOnly(true);
            env = new Environment(storeDirectory, envCfg);
        } else {
            throw lockedEx;
        }
    }
    return env;
}

From source file:exercise.basic.ResponseDemoResource.java

/**
 * @return/*from   ww w.j a v  a  2 s. c  o  m*/
 */
@GET
@Path("sampleimage.png")
public Response anydata() {
    URL resurl = Resources.getResource("exercise/sampleimage.png");
    final ByteSource byteSource = Resources.asByteSource(resurl);
    StreamingOutput stream = new StreamingOutput() {
        @Override
        public void write(OutputStream os) throws IOException, WebApplicationException {
            byteSource.copyTo(os);
            os.flush();
        }
    };
    return Response.status(Status.OK).type("image/png").entity(stream).build();
}

From source file:eu.esdihumboldt.hale.io.jdbc.msaccess.test.MsAccessDataReaderTestSuit.java

/**
 * Copies the source database to a temporary file.
 * //from   www . j a  va  2 s .co  m
 * @throws IOException if temp file can't be created
 */
public void createSourceTempFile() throws IOException {
    ByteSource source = Resources
            .asByteSource(MsAccessDataReaderTestSuit.class.getClassLoader().getResource(SOURCE_DB_PATH));
    ByteSink dest = Files.asByteSink(getSourceTempFilePath());
    source.copyTo(dest);
}

From source file:org.openqa.selenium.safari.SafariExtensions.java

/**
 * Copy a Safari extension to the target location. Any existing file is backed up.
 *///from  www .  j  av a  2  s.co m
private synchronized void installExtension(ByteSource extensionSrc, File targetFile) throws IOException {
    if (targetFile.exists()) {
        backup.backup(targetFile);
    }
    extensionSrc.copyTo(Files.asByteSink(targetFile));
    installedExtensions.add(targetFile);
}

From source file:com.facebook.buck.artifact_cache.HttpArtifactCacheBinaryProtocol.java

@VisibleForTesting
static byte[] createMetadataHeader(ImmutableSet<RuleKey> ruleKeys, ImmutableMap<String, String> metadata,
        ByteSource data) throws IOException {

    ByteArrayOutputStream rawOut = new ByteArrayOutputStream();
    Hasher hasher = HASH_FUNCTION.newHasher();
    try (DataOutputStream out = new DataOutputStream(new HasherOutputStream(hasher, rawOut))) {

        // Write the rule keys to the raw metadata, including them in the end-to-end checksum.
        out.writeInt(ruleKeys.size());//www  . j a va2s  .  c  om
        for (RuleKey ruleKey : ruleKeys) {
            out.writeUTF(ruleKey.toString());
        }

        // Write out the metadata map to the raw metadata, including it in the end-to-end checksum.
        out.writeInt(metadata.size());
        for (Map.Entry<String, String> ent : metadata.entrySet()) {
            out.writeUTF(ent.getKey());
            byte[] val = ent.getValue().getBytes(Charsets.UTF_8);
            out.writeInt(val.length);
            out.write(val);
            if (out.size() > MAX_METADATA_HEADER_SIZE) {
                throw new IOException("Metadata header too big.");
            }
        }
    }

    // Add the file data contents to the end-to-end checksum.
    data.copyTo(new HasherOutputStream(hasher, ByteStreams.nullOutputStream()));

    // Finish the checksum, adding it to the raw metadata
    rawOut.write(hasher.hash().asBytes());

    // Finally, base64 encode the raw bytes to make usable in a HTTP header.
    byte[] bytes = rawOut.toByteArray();
    if (bytes.length > MAX_METADATA_HEADER_SIZE) {
        throw new IOException("Metadata header too big.");
    }
    return bytes;
}

From source file:com.proofpoint.event.collector.combiner.S3StorageSystem.java

private StoredObject createCombinedObjectSmall(CombinedStoredObject combinedObject) {
    ImmutableList.Builder<ByteSource> builder = ImmutableList.builder();
    List<URI> sourceParts = Lists.transform(combinedObject.getSourceParts(),
            StoredObject.GET_LOCATION_FUNCTION);
    for (URI sourcePart : sourceParts) {
        builder.add(getInputSupplier(sourcePart));
    }/*from ww  w . ja  va  2 s  .  c  om*/
    ByteSource source = ByteSource.concat(builder.build());

    File tempFile = null;
    try {
        tempFile = File.createTempFile(S3StorageHelper.getS3FileName(combinedObject.getLocation()),
                ".small.s3.data");
        source.copyTo(Files.asByteSink(tempFile));
        StoredObject result = putObject(combinedObject.getLocation(), tempFile);
        return result;
    } catch (IOException e) {
        throw Throwables.propagate(e);
    } finally {
        if (tempFile != null) {
            tempFile.delete();
        }
    }
}

From source file:at.ac.univie.isc.asio.platform.FileSystemConfigStore.java

@Override
public URI save(final String qualifier, final String identifier, final ByteSource content) {
    requireNonNull(content, "file content");
    final Path file = resolve(qualifier, identifier);
    try {//w  w  w .j a  v a  2 s .c  o  m
        log.debug(Scope.SYSTEM.marker(), "saving item as <{}>", file);
        lock();
        try (final OutputStream sink = Files.newOutputStream(file)) {
            content.copyTo(sink);
        }
        return file.toUri();
    } catch (IOException e) {
        throw new FileSystemAccessFailure("failed to save to <" + file + ">", e);
    } finally {
        lock.unlock();
    }
}

From source file:org.glowroot.agent.embedded.util.CappedDatabase.java

@OnlyUsedByTests
long write(final ByteSource byteSource, String type) throws IOException {
    return write(type, new Copier() {
        @Override//from   w w w  .  ja  va  2 s.  co  m
        public void copyTo(OutputStream out) throws IOException {
            byteSource.copyTo(out);
        }
    });
}

From source file:org.haiku.haikudepotserver.support.AbstractExternalToolService.java

private void execute(T context, ByteSource inputSource, ByteSink outputSink) throws IOException {

    Preconditions.checkArgument(null != inputSource, "the input source must be supplied");
    Preconditions.checkArgument(null != outputSink, "the output sink must be supplied");

    long startMs = System.currentTimeMillis();

    File temporaryInputFile = File.createTempFile("image-input", ".png");
    File temporaryOutputFile = new File(getTemporaryDirectory(), UUID.randomUUID().toString());

    try {// w w  w  .  j av  a  2  s  .co  m
        inputSource.copyTo(Files.asByteSink(temporaryInputFile));

        List<String> args = createArguments(context, temporaryInputFile, temporaryOutputFile);
        Process process = new ProcessBuilder(args).start();

        LOGGER.debug("did start " + args.get(0));

        try (InputStream inputStream = process.getErrorStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charsets.UTF_8);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader)

        ) {

            String line;

            while (null != (line = bufferedReader.readLine())) {
                LOGGER.debug(line);
            }

        }

        try {
            if (process.waitFor(TIMEOUT, TimeUnit.MILLISECONDS)) {
                if (0 != process.exitValue()) {
                    throw new RuntimeException("unable to execute " + args.get(0));
                }
            } else {
                process.destroyForcibly();
                throw new RuntimeException("unable to run " + args.get(0) + " as it has timed-out");
            }
        } catch (InterruptedException ie) {
            throw new RuntimeException("interrupted waiting for the " + args.get(0) + " process to complete",
                    ie);
        }

        // check the difference between the source and the destination.

        LOGGER.debug("did finish {} ({}ms)", args.get(0), (System.currentTimeMillis() - startMs));

        Files.asByteSource(temporaryOutputFile).copyTo(outputSink);

    } finally {
        quietlyDeleteTemporaryFile(temporaryInputFile);
        quietlyDeleteTemporaryFile(temporaryOutputFile);
    }

}

From source file:keywhiz.FileAssetServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    try {/*from   ww w. j a va  2 s.co  m*/
        ByteSource asset = loadAsset(req.getRequestURI());
        if (asset == null) {
            resp.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        final String mimeTypeOfExtension = req.getServletContext().getMimeType(req.getRequestURI());
        MediaType mediaType = DEFAULT_MEDIA_TYPE;

        if (mimeTypeOfExtension != null) {
            try {
                mediaType = MediaType.parse(mimeTypeOfExtension);
                if (mediaType.is(MediaType.ANY_TEXT_TYPE)) {
                    mediaType = mediaType.withCharset(DEFAULT_CHARSET);
                }
            } catch (IllegalArgumentException ignore) {
            }
        }

        resp.setContentType(mediaType.type() + "/" + mediaType.subtype());

        if (mediaType.charset().isPresent()) {
            resp.setCharacterEncoding(mediaType.charset().get().toString());
        }

        try (OutputStream output = resp.getOutputStream()) {
            asset.copyTo(output);
        }
    } catch (RuntimeException ignored) {
        resp.sendError(HttpServletResponse.SC_NOT_FOUND);
    }
}