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

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

Introduction

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

Prototype

public abstract InputStream openStream() throws IOException;

Source Link

Document

Opens a new InputStream for reading from this source.

Usage

From source file:org.haiku.haikudepotserver.pkg.job.PkgScreenshotImportArchiveJobRunner.java

private int consumeScreenshotArchiveEntries(ByteSource byteSource,
        Consumer<ArchiveEntryWithPkgNameAndOrdering> archiveEntryConsumer) throws IOException {
    int counter = 0;

    try (InputStream inputStream = byteSource.openStream();
            GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream);
            ArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(gzipInputStream)) {
        ArchiveEntry archiveEntry;/*w w w.j  a  v a2 s .  com*/

        while (null != (archiveEntry = tarArchiveInputStream.getNextEntry())) {

            Matcher matcher = PATTERN_PATH.matcher(archiveEntry.getName());

            if (matcher.matches()) {
                archiveEntryConsumer
                        .accept(new ArchiveEntryWithPkgNameAndOrdering(tarArchiveInputStream, archiveEntry,
                                matcher.group(GROUP_PKGNAME), Integer.parseInt(matcher.group(GROUP_LEAFNAME))));

                counter++;
            }
        }
    }

    return counter;
}

From source file:com.basistech.ReleaseNoteMojo.java

private KeyStore readTrustStore(File trustStore) throws IOException, NoSuchAlgorithmException,
        KeyStoreException, CertificateException, KeyManagementException {
    KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
    ByteSource keystoreByteSource = Files.asByteSource(trustStore);
    InputStream keystoreStream = null;
    try {/*  ww w  .j  a  v  a  2 s. c  om*/
        keystoreStream = keystoreByteSource.openStream();
        //TODO: deal with the actual password whatever it is.
        // there's no good reason to use a fancy password on a trust store.
        keystore.load(keystoreStream, "changeit".toCharArray());
    } finally {
        IOUtils.closeQuietly(keystoreStream);
    }
    return keystore;
}

From source file:org.codice.alliance.nsili.endpoint.requests.OrderRequestImpl.java

private void writeFile(FileLocation destination, PackagingSpecFormatType packagingSpecFormatType,
        String filename, List<String> sentFiles, ByteSource contents) throws IOException {
    String filenameWithExt = filename + packagingSpecFormatType.getExtension();
    writeFile(destination, contents.openStream(), contents.size(), filenameWithExt,
            packagingSpecFormatType.getContentType());
    sentFiles.add(filenameWithExt);/* ww  w  .  j  av  a  2  s  .  c  o  m*/
}

From source file:org.haiku.haikudepotserver.job.LocalJobServiceImpl.java

@Override
public JobData storeSuppliedData(String useCode, String mediaTypeCode, ByteSource byteSource)
        throws IOException {
    Preconditions.checkArgument(null != byteSource, "the byte source must be supplied to provide data");
    String guid = UUID.randomUUID().toString();
    JobData data;//from   w w  w . j  ava2  s.  com
    long len;

    try (InputStream inputStream = byteSource.openStream()) {

        // TODO; constrain this to a sensible size

        len = dataStorageService.put(guid).writeFrom(inputStream);
        data = new JobData(guid, JobDataType.SUPPLIED, useCode, mediaTypeCode);

        synchronized (this) {
            datas.add(data);
        }
    }

    LOGGER.info("did supply {}b job data; {}", len, data);

    return data;
}

From source file:org.codice.alliance.nsili.endpoint.requests.OrderRequestImpl.java

private List<String> writeData(FileLocation destination, PackagingSpecFormatType packagingSpecFormatType,
        List<ResourceContainer> files, String filename) throws IOException {

    List<String> sentFiles = new ArrayList<>();

    if (!files.isEmpty()) {
        if (files.size() > 1) {
            int totalNum = files.size() + 1;
            String totalNumPortion = String.format(FILE_COUNT_FORMAT, totalNum);

            switch (packagingSpecFormatType) {
            case FILESUNC: {
                int currNum = 1;
                for (ResourceContainer file : files) {
                    String currNumPortion = String.format(FILE_COUNT_FORMAT, currNum);
                    String currFileName = filename + "." + currNumPortion + "." + totalNumPortion;
                    writeFile(destination, file.getInputStream(), file.getSize(), currFileName,
                            file.getMimeTypeValue());
                    currNum++;/*ww  w.  ja v  a2  s .  c o m*/
                    sentFiles.add(currFileName);
                }
            }
                break;
            case FILESCOMPRESS: {
                int currNum = 1;
                for (ResourceContainer file : files) {
                    try (TemporaryFileBackedOutputStream fos = new TemporaryFileBackedOutputStream(
                            MAX_MEMORY_SIZE); ZipOutputStream zipOut = new ZipOutputStream(fos)) {
                        getZip(zipOut, file.getInputStream(), file.getName());
                        ByteSource contents = fos.asByteSource();
                        String currNumPortion = String.format(FILE_COUNT_FORMAT, currNum);
                        String currFileName = filename + "." + currNumPortion + "." + totalNumPortion
                                + packagingSpecFormatType.getExtension();
                        writeFile(destination, contents.openStream(), contents.size(), currFileName,
                                packagingSpecFormatType.getContentType());
                        sentFiles.add(currFileName);
                        currNum++;
                    }
                }
            }
                break;
            case FILESZIP: {
                try (TemporaryFileBackedOutputStream fos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                        ZipOutputStream zipOut = new ZipOutputStream(fos)) {
                    getZip(zipOut, files);
                    ByteSource zip = fos.asByteSource();
                    writeFile(destination, packagingSpecFormatType, filename, sentFiles, zip);
                }
            }
                break;
            case FILESGZIP: {
                int currNum = 1;
                for (ResourceContainer file : files) {
                    try (TemporaryFileBackedOutputStream fos = new TemporaryFileBackedOutputStream(
                            MAX_MEMORY_SIZE); GZIPOutputStream zipOut = new GZIPOutputStream(fos)) {
                        getGzip(zipOut, file.getInputStream());
                        ByteSource contents = fos.asByteSource();
                        String currNumPortion = String.format(FILE_COUNT_FORMAT, currNum);
                        String currFileName = filename + "." + currNumPortion + "." + totalNumPortion
                                + packagingSpecFormatType.getExtension();
                        writeFile(destination, contents.openStream(), contents.size(), currFileName,
                                packagingSpecFormatType.getContentType());
                        sentFiles.add(currFileName);
                        currNum++;
                    }
                }
            }
                break;
            case TARUNC: {
                try (TemporaryFileBackedOutputStream fos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                        TarOutputStream tarOut = new TarOutputStream(fos)) {
                    getTar(tarOut, files);
                    ByteSource tar = fos.asByteSource();
                    writeFile(destination, packagingSpecFormatType, filename, sentFiles, tar);
                }
            }
                break;
            case TARZIP: {
                try (TemporaryFileBackedOutputStream tarFos = new TemporaryFileBackedOutputStream(
                        MAX_MEMORY_SIZE); TarOutputStream tarOut = new TarOutputStream(tarFos)) {
                    getTar(tarOut, files);
                    try (TemporaryFileBackedOutputStream zipFos = new TemporaryFileBackedOutputStream(
                            MAX_MEMORY_SIZE); ZipOutputStream zipOut = new ZipOutputStream(zipFos)) {
                        getZip(zipOut, tarFos.asByteSource().openStream(), filename + ".tar");
                        ByteSource zip = zipFos.asByteSource();
                        writeFile(destination, packagingSpecFormatType, filename, sentFiles, zip);
                    }
                }
            }
                break;
            case TARGZIP: {
                try (TemporaryFileBackedOutputStream tarFos = new TemporaryFileBackedOutputStream(
                        MAX_MEMORY_SIZE); TarOutputStream tarOut = new TarOutputStream(tarFos)) {
                    getTar(tarOut, files);
                    try (TemporaryFileBackedOutputStream gzipFos = new TemporaryFileBackedOutputStream(
                            MAX_MEMORY_SIZE); GZIPOutputStream zipOut = new GZIPOutputStream(gzipFos)) {
                        getGzip(zipOut, tarFos.asByteSource().openStream());
                        ByteSource zip = gzipFos.asByteSource();
                        writeFile(destination, packagingSpecFormatType, filename, sentFiles, zip);
                    }
                }
            }
                break;
            case TARCOMPRESS: {
                try (TemporaryFileBackedOutputStream tarFos = new TemporaryFileBackedOutputStream(
                        MAX_MEMORY_SIZE); TarOutputStream tarOut = new TarOutputStream(tarFos)) {
                    getTar(tarOut, files);
                    try (TemporaryFileBackedOutputStream zipFos = new TemporaryFileBackedOutputStream(
                            MAX_MEMORY_SIZE); ZipOutputStream zipOut = new ZipOutputStream(zipFos)) {
                        getZip(zipOut, tarFos.asByteSource().openStream(), filename + ".tar");
                        writeFile(destination, packagingSpecFormatType, filename, sentFiles,
                                zipFos.asByteSource());
                    }
                }
            }
                break;
            default:
                break;
            }

        } else {
            ResourceContainer file = files.iterator().next();

            switch (packagingSpecFormatType) {
            case FILESUNC: {
                writeFile(destination, file.getInputStream(), file.getSize(), filename,
                        file.getMimeTypeValue());
                sentFiles.add(filename);
            }
                break;
            case FILESCOMPRESS: {
                try (TemporaryFileBackedOutputStream fos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                        ZipOutputStream zipOut = new ZipOutputStream(fos)) {
                    getZip(zipOut, file.getInputStream(), file.getName());
                    ByteSource contents = fos.asByteSource();

                    writeFile(destination, packagingSpecFormatType, filename, sentFiles, contents);
                }
            }
                break;
            case TARUNC:
                try (TemporaryFileBackedOutputStream fos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                        TarOutputStream tarOut = new TarOutputStream(fos)) {
                    getTar(tarOut, file);
                    ByteSource contents = fos.asByteSource();
                    writeFile(destination, packagingSpecFormatType, filename, sentFiles, contents);
                }
                break;
            case TARZIP: {
                try (TemporaryFileBackedOutputStream tarFos = new TemporaryFileBackedOutputStream(
                        MAX_MEMORY_SIZE); TarOutputStream tarOut = new TarOutputStream(tarFos)) {
                    getTar(tarOut, file);
                    try (TemporaryFileBackedOutputStream zipFos = new TemporaryFileBackedOutputStream(
                            MAX_MEMORY_SIZE); ZipOutputStream zipOut = new ZipOutputStream(zipFos)) {
                        getZip(zipOut, tarFos.asByteSource().openStream(), filename + ".tar");
                        ByteSource contents = zipFos.asByteSource();

                        writeFile(destination, packagingSpecFormatType, filename, sentFiles, contents);
                    }
                }
            }
                break;
            case FILESZIP:
                try (TemporaryFileBackedOutputStream fos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                        GZIPOutputStream zipOut = new GZIPOutputStream(fos)) {
                    getGzip(zipOut, file.getInputStream());
                    ByteSource contents = fos.asByteSource();
                    writeFile(destination, packagingSpecFormatType, filename, sentFiles, contents);
                }
                break;
            case TARGZIP: {
                try (TemporaryFileBackedOutputStream tarFos = new TemporaryFileBackedOutputStream(
                        MAX_MEMORY_SIZE); TarOutputStream tarOut = new TarOutputStream(tarFos)) {
                    getTar(tarOut, file);
                    try (TemporaryFileBackedOutputStream gzipFos = new TemporaryFileBackedOutputStream(
                            MAX_MEMORY_SIZE); GZIPOutputStream zipOut = new GZIPOutputStream(gzipFos)) {
                        getGzip(zipOut, tarFos.asByteSource().openStream());
                        ByteSource contents = gzipFos.asByteSource();
                        writeFile(destination, packagingSpecFormatType, filename, sentFiles, contents);
                    }
                }
            }
                break;
            case FILESGZIP:
                try (TemporaryFileBackedOutputStream fos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                        GZIPOutputStream zipOut = new GZIPOutputStream(fos)) {
                    getGzip(zipOut, file.getInputStream());
                    ByteSource contents = fos.asByteSource();
                    writeFile(destination, packagingSpecFormatType, filename, sentFiles, contents);
                }
                break;
            case TARCOMPRESS: {
                try (TemporaryFileBackedOutputStream tarFos = new TemporaryFileBackedOutputStream(
                        MAX_MEMORY_SIZE); TarOutputStream tarOut = new TarOutputStream(tarFos)) {
                    getTar(tarOut, file);
                    try (TemporaryFileBackedOutputStream zipFos = new TemporaryFileBackedOutputStream(
                            MAX_MEMORY_SIZE); ZipOutputStream zipOut = new ZipOutputStream(zipFos)) {
                        getZip(zipOut, tarFos.asByteSource().openStream(), filename + ".tar");
                        ByteSource contents = zipFos.asByteSource();

                        writeFile(destination, packagingSpecFormatType, filename, sentFiles, contents);
                    }
                }
            }
                break;
            default:
                break;
            }
        }
    }

    return sentFiles;
}

From source file:com.tinspx.util.io.ByteUtils.java

/**
 * Used instead of {@link #toByteArray(ByteSource)} when the size of the
 * {@code ByteSource} is known and {@link ByteSource#size() size()} should
 * not be called on {@code source}./*from  w  w  w . j a  va2 s. co m*/
 */
@ThreadLocalArray(8192)
public static byte[] toByteArray(ByteSource source, int expectedSize) throws IOException {
    final Closer closer = Closer.create();
    try {
        return toByteArray(closer.register(source.openStream()), expectedSize);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:com.tinspx.util.io.ByteUtils.java

/**
 * Copies as many bytes as possible from {@code from} into {@code to},
 * returning the total number of bytes copied.
 * /*from   w  w  w. j  a v  a2  s  .co  m*/
 * @param from the source to read bytes from
 * @param to the destination to copy bytes read from {@code from} into
 * @return the total number of bytes copied from {@code from} to {@code to}
 * @throws IOException if an IOException occurs
 * @throws NullPointerException if either {@code from} or {@code to} is null
 */
@ThreadLocalArray(8192)
public static int copy(@NonNull ByteSource from, @NonNull ByteBuffer to) throws IOException {
    final Closer closer = Closer.create();
    try {
        return copy(closer.register(from.openStream()), to);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:com.tinspx.util.io.ByteUtils.java

@ThreadLocalArray(8192)
static boolean contentEqualsImpl(@NonNull ByteSource source1, @NonNull ByteSource source2) throws IOException {
    final Closer closer = Closer.create();
    try {/*from w ww .j a v a2s. c  o  m*/
        return contentEquals(closer.register(source1.openStream()), closer.register(source2.openStream()));
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:com.tinspx.util.io.ByteUtils.java

@ThreadLocalArray(8192)
public static boolean contentEquals(@NonNull ByteSource source1, @NonNull ByteBuffer buffer)
        throws IOException {
    final Closer closer = Closer.create();
    try {/*from www .  j  a  va  2 s  .  c  o  m*/
        return contentEquals(closer.register(source1.openStream()), buffer);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:com.tinspx.util.io.ByteUtils.java

@ThreadLocalArray(8192)
public static boolean contentEquals(@NonNull ByteSource source, @NonNull @WillNotClose InputStream in)
        throws IOException {
    final Closer closer = Closer.create();
    try {//www .java  2s .c o  m
        return contentEquals(closer.register(source.openStream()), in);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}