private void ociWriteTo(OutputStream out) throws IOException {
    TarStreamBuilder tarStreamBuilder = new TarStreamBuilder();
    OciManifestTemplate manifest = new OciManifestTemplate();

    // Adds all the layers to the tarball and manifest
    for (Layer layer : image.getLayers()) {
      DescriptorDigest digest = layer.getBlobDescriptor().getDigest();
      long size = layer.getBlobDescriptor().getSize();

      tarStreamBuilder.addBlobEntry(
          layer.getBlob(), size, BLOB_PREFIX + digest.getHash(), TAR_ENTRY_MODIFICATION_TIME);
      manifest.addLayer(size, digest);
    }

    // Adds the container configuration to the tarball and manifest
    JsonTemplate containerConfiguration =
        new ImageToJsonTranslator(image).getContainerConfiguration();
    BlobDescriptor configDescriptor = Digests.computeDigest(containerConfiguration);
    manifest.setContainerConfiguration(configDescriptor.getSize(), configDescriptor.getDigest());
    tarStreamBuilder.addByteEntry(
        JsonTemplateMapper.toByteArray(containerConfiguration),
        BLOB_PREFIX + configDescriptor.getDigest().getHash(),
        TAR_ENTRY_MODIFICATION_TIME);

    // Adds the manifest to the tarball
    BlobDescriptor manifestDescriptor = Digests.computeDigest(manifest);
    tarStreamBuilder.addByteEntry(
        JsonTemplateMapper.toByteArray(manifest),
        BLOB_PREFIX + manifestDescriptor.getDigest().getHash(),
        TAR_ENTRY_MODIFICATION_TIME);

    // Adds the oci-layout and index.json
    tarStreamBuilder.addByteEntry(
        "{\"imageLayoutVersion\": \"1.0.0\"}".getBytes(StandardCharsets.UTF_8),
        "oci-layout",
        TAR_ENTRY_MODIFICATION_TIME);
    OciIndexTemplate index = new OciIndexTemplate();
    // TODO: figure out how to tag with allTargetImageTags
    index.addManifest(manifestDescriptor, imageReference.toStringWithQualifier());
    tarStreamBuilder.addByteEntry(
        JsonTemplateMapper.toByteArray(index), "index.json", TAR_ENTRY_MODIFICATION_TIME);

    tarStreamBuilder.writeAsTarArchiveTo(out);
  }
Writes image tar bar in configured {@link Image#getImageFormat()} of OCI or Docker to output
stream.

@param out the target output stream
@throws IOException if an error occurs writing out the image to stream
jib-core/src/main/java/com/google/cloud/tools/jib/image/ImageTarball.java