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

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

Introduction

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

Prototype

protected ByteSource() 

Source Link

Document

Constructor for use by subclasses.

Usage

From source file:org.jclouds.io.ByteSources.java

/**
 * always returns the same stream/*from w w w  .j av a2 s  .com*/
 * 
 * @param in
 *           stream to always return
 */
public static ByteSource asByteSource(final InputStream in) {
    checkNotNull(in, "in");
    return new ByteSource() {
        @Override
        public InputStream openStream() throws IOException {
            return in;
        }
    };
}

From source file:be.nbb.sdmx.SdmxTestResources.java

public static ByteSource onResource(final String id) {
    return new ByteSource() {
        @Override//from  w w  w .j av a 2  s  . c o m
        public InputStream openStream() throws IOException {
            return SdmxTestResources.class.getResource(id).openStream();
        }
    };
}

From source file:be.nbb.sdmx.facade.connectors.SdmxTestResources.java

private static ByteSource onResource(final String id) {
    return new ByteSource() {
        @Override//from w  w w  .j a  va2  s  .co m
        public InputStream openStream() throws IOException {
            return SdmxTestResources.class.getResource(id).openStream();
        }
    };
}

From source file:eu.esdihumboldt.hale.io.xslt.internal.Templates.java

/**
 * Copy a template to the given target folder.
 * /*from   w  w  w .  j av a 2  s  . c  om*/
 * @param targetFolder the target folder
 * @param templateFileName the template file name, must lie next to the
 *            {@link Templates} class
 * @throws IOException if copying the template fails
 */
private static void copyTemplate(final File targetFolder, final String templateFileName) throws IOException {
    new ByteSource() {

        @Override
        public InputStream openStream() throws IOException {
            return Templates.class.getResourceAsStream(templateFileName);
        }

    }.copyTo(Files.asByteSink(new File(targetFolder, templateFileName)));
}

From source file:org.opendaylight.netconf.util.test.XmlFileLoader.java

public static String fileToString(final String fileName) throws IOException {
    try (InputStream resourceAsStream = XmlFileLoader.class.getClassLoader().getResourceAsStream(fileName)) {
        Preconditions.checkNotNull(resourceAsStream);
        return new ByteSource() {
            @Override/*  w  w  w.  jav a2  s.c om*/
            public InputStream openStream() {
                return resourceAsStream;
            }
        }.asCharSource(Charsets.UTF_8).read();

    }
}

From source file:com.b2international.snowowl.snomed.api.rest.SnomedExportRestRequests.java

public static File getExportFile(final String exportId) throws Exception {

    File tmpDir = null;//from   w w w. ja v a2  s.c om
    File exportArchive = null;

    try {

        tmpDir = Files.createTempDir();
        exportArchive = new File(tmpDir, "export.zip");
        new ByteSource() {
            @Override
            public InputStream openStream() throws IOException {
                return givenAuthenticatedRequest(SnomedApiTestConstants.SCT_API).contentType(ContentType.JSON)
                        .get("/exports/{id}/archive", exportId).thenReturn().asInputStream();
            }
        }.copyTo(Files.asByteSink(exportArchive));

    } catch (final Exception e) {
        throw e;
    } finally {
        if (tmpDir != null) {
            tmpDir.deleteOnExit();
        }

        if (exportArchive != null) {
            exportArchive.deleteOnExit();
        }
    }

    assertNotNull(exportArchive);
    return exportArchive;
}

From source file:com.comphenix.protocol.compat.guava.Guava17.java

@Override
public DataInputStream addHeader(final DataInputStream input, final PacketType type) {
    ByteSource header = new ByteSource() {
        @Override//w  w w .  j a v  a  2  s. c  o  m
        public InputStream openStream() throws IOException {
            byte[] data = new byte[] { (byte) type.getLegacyId() };
            return new ByteArrayInputStream(data);
        }
    };

    ByteSource data = new ByteSource() {
        @Override
        public InputStream openStream() throws IOException {
            return input;
        }
    };

    // Combine them into a single stream
    try {
        return new DataInputStream(ByteSource.concat(header, data).openStream());
    } catch (IOException e) {
        throw new RuntimeException("Cannot add header.", e);
    }
}

From source file:com.facebook.buck.io.DefaultProjectFilesystemDelegate.java

@Override
public Sha1HashCode computeSha1(Path pathRelativeToProjectRootOrJustAbsolute) throws IOException {
    final Path fileToHash = getPathForRelativePath(pathRelativeToProjectRootOrJustAbsolute);

    // Normally, we would just use `Files.hash(fileToHash.toFile(), Hashing.sha1())`, but if
    // fileToHash is backed by Jimfs, its toFile() method throws an UnsupportedOperationException.
    // Creating the input stream via java.nio.file.Files.newInputStream() avoids this issue.
    ByteSource source = new ByteSource() {
        @Override/*from w  ww .j a  v a 2 s  . c o m*/
        public InputStream openStream() throws IOException {
            // No need to wrap with BufferedInputStream because ByteSource uses ByteStreams.copy(),
            // which already buffers.
            return Files.newInputStream(fileToHash);
        }
    };
    HashCode hashCode = source.hash(Hashing.sha1());
    return Sha1HashCode.fromHashCode(hashCode);
}

From source file:com.b2international.snowowl.datastore.internal.file.DefaultFileRegistry.java

@Override
public void upload(UUID id, InputStream in) {
    final File file = toFile(id);
    if (file.exists()) {
        throw new AlreadyExistsException("Zip File", id.toString());
    }/*from  w ww  .j  a v a 2s .c o m*/

    final BufferedInputStream bin = new BufferedInputStream(in);

    try {
        new ByteSource() {
            @Override
            public InputStream openStream() throws IOException {
                return bin;
            }
        }.copyTo(Files.asByteSink(file));
    } catch (IOException e) {
        throw new SnowowlRuntimeException("Failed to upload attachment of " + id, e);
    }
}

From source file:io.druid.indexing.common.tasklogs.FileTaskLogs.java

@Override
public Optional<ByteSource> streamTaskLog(final String taskid, final long offset) throws IOException {
    final File file = fileForTask(taskid);
    if (file.exists()) {
        return Optional.<ByteSource>of(new ByteSource() {
            @Override/*from   www .ja va2  s.  com*/
            public InputStream openStream() throws IOException {
                return LogUtils.streamFile(file, offset);
            }
        });
    } else {
        return Optional.absent();
    }
}