CachedLayer writeTarLayer(DescriptorDigest diffId, Blob compressedBlob) throws IOException {
    Files.createDirectories(cacheStorageFiles.getLocalDirectory());
    Files.createDirectories(cacheStorageFiles.getTemporaryDirectory());
    try (TempDirectoryProvider tempDirectoryProvider = new TempDirectoryProvider()) {
      Path temporaryLayerDirectory =
          tempDirectoryProvider.newDirectory(cacheStorageFiles.getTemporaryDirectory());
      Path temporaryLayerFile = cacheStorageFiles.getTemporaryLayerFile(temporaryLayerDirectory);

      BlobDescriptor layerBlobDescriptor;
      try (OutputStream fileOutputStream =
          new BufferedOutputStream(Files.newOutputStream(temporaryLayerFile))) {
        layerBlobDescriptor = compressedBlob.writeTo(fileOutputStream);
      }

      // Renames the temporary layer file to its digest
      // (temp/temp -> temp/<digest>)
      String fileName = layerBlobDescriptor.getDigest().getHash();
      Path digestLayerFile = temporaryLayerDirectory.resolve(fileName);
      moveIfDoesNotExist(temporaryLayerFile, digestLayerFile);

      // Moves the temporary directory to directory named with diff ID
      // (temp/<digest> -> <diffID>/<digest>)
      Path destination = cacheStorageFiles.getLocalDirectory().resolve(diffId.getHash());
      moveIfDoesNotExist(temporaryLayerDirectory, destination);

      return CachedLayer.builder()
          .setLayerDigest(layerBlobDescriptor.getDigest())
          .setLayerDiffId(diffId)
          .setLayerSize(layerBlobDescriptor.getSize())
          .setLayerBlob(Blobs.from(destination.resolve(fileName)))
          .build();
    }
  }
Saves a local base image layer.

@param diffId the layer blob's diff ID
@param compressedBlob the blob to save
@throws IOException if an I/O exception occurs
jib-core/src/main/java/com/google/cloud/tools/jib/cache/CacheStorageWriter.java