Skip to content

Commit

Permalink
Decouple Blob from TarStreamBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
chanseokoh committed Apr 19, 2019
1 parent 305f254 commit 02b42ad
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ public void writeTo(OutputStream out) throws IOException {
JsonTemplateMapper.toBlob(Collections.singletonList(manifestTemplate))),
MANIFEST_JSON_FILE_NAME);

tarStreamBuilder.toBlob().writeTo(out);
tarStreamBuilder.writeAsTarArchiveTo(out);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.cloud.tools.jib.image;

import com.google.cloud.tools.jib.blob.Blob;
import com.google.cloud.tools.jib.blob.Blobs;
import com.google.cloud.tools.jib.tar.TarStreamBuilder;
import com.google.common.base.Verify;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -132,6 +133,6 @@ public Blob build() {
tarStreamBuilder.addTarArchiveEntry(entry);
}

return tarStreamBuilder.toBlob();
return Blobs.from(outputStream -> tarStreamBuilder.writeAsTarArchiveTo(outputStream));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public class TarStreamBuilder {
/**
* Writes each entry in the filesystem to the tarball archive stream.
*
* @param tarByteStream the stream to write to.
* @param out the stream to write to.
* @throws IOException if building the tarball fails.
*/
private void writeEntriesAsTarArchive(OutputStream tarByteStream) throws IOException {
public void writeAsTarArchiveTo(OutputStream out) throws IOException {
try (TarArchiveOutputStream tarArchiveOutputStream =
new TarArchiveOutputStream(tarByteStream, StandardCharsets.UTF_8.name())) {
new TarArchiveOutputStream(out, StandardCharsets.UTF_8.name())) {
// Enables PAX extended headers to support long file names.
tarArchiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
for (Map.Entry<TarArchiveEntry, Blob> entry : archiveMap.entrySet()) {
Expand Down Expand Up @@ -90,9 +90,4 @@ public void addBlobEntry(Blob blob, long size, String name) {
entry.setSize(size);
archiveMap.put(entry, blob);
}

/** @return a new {@link Blob} that can stream the uncompressed tarball archive BLOB. */
public Blob toBlob() {
return Blobs.from(this::writeEntriesAsTarArchive);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.google.cloud.tools.jib.tar;

import com.google.cloud.tools.jib.blob.Blob;
import com.google.cloud.tools.jib.blob.Blobs;
import com.google.common.io.ByteStreams;
import com.google.common.io.Resources;
Expand Down Expand Up @@ -101,12 +100,11 @@ public void testToBlob_multiByte() throws IOException {
testTarStreamBuilder.addByteEntry("asdf".getBytes(StandardCharsets.UTF_8), "crepecake");
testTarStreamBuilder.addBlobEntry(
Blobs.from("jib"), "jib".getBytes(StandardCharsets.UTF_8).length, "jib");
Blob blob = testTarStreamBuilder.toBlob();

// Writes the BLOB and captures the output.
ByteArrayOutputStream tarByteOutputStream = new ByteArrayOutputStream();
OutputStream compressorStream = new GZIPOutputStream(tarByteOutputStream);
blob.writeTo(compressorStream);
testTarStreamBuilder.writeAsTarArchiveTo(compressorStream);

// Rearrange the output into input for verification.
ByteArrayInputStream byteArrayInputStream =
Expand Down Expand Up @@ -173,12 +171,10 @@ private void setUpWithStringsAndTarEntries() {

/** Creates a compressed blob from the TarStreamBuilder and verifies it. */
private void verifyBlobWithCompression() throws IOException {
Blob blob = testTarStreamBuilder.toBlob();

// Writes the BLOB and captures the output.
ByteArrayOutputStream tarByteOutputStream = new ByteArrayOutputStream();
OutputStream compressorStream = new GZIPOutputStream(tarByteOutputStream);
blob.writeTo(compressorStream);
testTarStreamBuilder.writeAsTarArchiveTo(compressorStream);

// Rearrange the output into input for verification.
ByteArrayInputStream byteArrayInputStream =
Expand All @@ -190,11 +186,9 @@ private void verifyBlobWithCompression() throws IOException {

/** Creates an uncompressed blob from the TarStreamBuilder and verifies it. */
private void verifyBlobWithoutCompression() throws IOException {
Blob blob = testTarStreamBuilder.toBlob();

// Writes the BLOB and captures the output.
ByteArrayOutputStream tarByteOutputStream = new ByteArrayOutputStream();
blob.writeTo(tarByteOutputStream);
testTarStreamBuilder.writeAsTarArchiveTo(tarByteOutputStream);

// Rearrange the output into input for verification.
ByteArrayInputStream byteArrayInputStream =
Expand Down

0 comments on commit 02b42ad

Please sign in to comment.