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:com.android.builder.internal.packaging.zip.ZFile.java

/**
 * Writes an entry's data in the zip file. This includes everything: the local header and
 * the data itself. After writing, the entry is updated with the offset and its source replaced
 * with a source that reads from the zip file.
 *
 * @param entry the entry to write/*from   www. j a  v a 2  s.  c o  m*/
 * @param offset the offset at which the entry should be written
 * @throws IOException failed to write the entry
 */
private void writeEntry(@NonNull StoredEntry entry, long offset) throws IOException {
    Preconditions.checkArgument(entry.getDataDescriptorType() == DataDescriptorType.NO_DATA_DESCRIPTOR,
            "Cannot write entries with a data " + "descriptor.");
    Preconditions.checkNotNull(mRaf, "mRaf == null");
    Preconditions.checkState(mState == ZipFileState.OPEN_RW, "mState != ZipFileState.OPEN_RW");

    /*
     * Place the cursor and write the local header.
     */
    byte[] headerData = entry.toHeaderData();
    directWrite(offset, headerData);

    /*
     * Get the raw source data to write.
     */
    ProcessedAndRawByteSources source = entry.getSource();
    ByteSource rawContents = source.getRawByteSource();

    /*
     * Write the source data.
     */
    byte[] chunk = new byte[IO_BUFFER_SIZE];
    int r;
    long writeOffset = offset + headerData.length;
    InputStream is = rawContents.openStream();
    while ((r = is.read(chunk)) >= 0) {
        directWrite(writeOffset, chunk, 0, r);
        writeOffset += r;
    }

    is.close();

    /*
     * Set the entry's offset and create the entry source.
     */
    entry.replaceSourceFromZip(offset);
}