2

So, I have this code in scala which I am converting to managed.

val file_out = new FileOutputStream(new java.io.File(filePath, resultFile + ".tar.gz"));
val buffer_out = new BufferedOutputStream(file_out);
val gzip_out = new GzipCompressorOutputStream(buffer_out);
val tar_out = new TarArchiveOutputStream(gzip_out);

try {
    addFileToTarGz(tar_out, filePath + "/" + resultFolder, "");
} finally {
    tar_out.finish();
    tar_out.close();
    gzip_out.close();
    buffer_out.close();
    file_out.close();
}

First attempt is:

val file = new java.io.File(filePath, s"$resultFile.tar.gz")
managed(new FileOutputStream(file))
        .acquireAndGet(stream => managed(new BufferedOutputStream(stream))
                .acquireAndGet(buffer => managed(new GzipCompressorOutputStream(buffer))
                        .acquireAndGet(gzip => managed(new TarArchiveOutputStream(gzip))
                                .acquireAndGet(tar => {
                                  try {
                                    addFileToTarGz(tar, filePath + "/" + resultFolder, "")
                                  } finally {
                                    tar.finish()
                                  }
                                }))))

However, it doesn't look very readable. Is there a better way to make it managed but also readable?

Asad Iqbal
  • 3,241
  • 4
  • 32
  • 52

2 Answers2

1

Have you considered load pattern?

def withResource[T](block: Resource => T): T = {
  val resource = new Resource
  try {
    block(resource)
  } finally {
    resource.close()
  }
}

then you would use it like:

withResourse { resource =>
  // do something with resource
}

If you used separate load for each of those files you would end up with nested blocks... (which under some circumstances might be the most reasonable choice), but here I guess it will be enough to do:

def withTarStream(filePath: String, resultFile: String)(block: TarArchiveOutputStream => T): T = {
  val fileOut = new FileOutputStream(new java.io.File(filePath, resultFile))
  val bufferOut = new BufferedOutputStream(fileOut)
  val gzipOut = new GzipCompressorOutputStream(bufferOut)
  val tarOut = new TarArchiveOutputStream(gzipOut)

  try {
    block(tarOut)
  } finally {
    tarOut.finish()
    tarOut.close()
    gzipOut.close()
    bufferOut.close()
    fileOut.close()
  }
}

used like:

withTarStream(filePath, s"$resultFile.tar.gz") { tarStream =>
  addFileToTarGz(tarStream, filePath + "/" + resultFolder, "")
}
Mateusz Kubuszok
  • 24,995
  • 4
  • 42
  • 64
0

Based on @Mateusz Kubuszok's suggestion, I tried these variations:

private def withResource[T: Resource : Manifest, X](t: T, block: T => X): X = managed(t).acquireAndGet(x => block(x))

withResource(new FileOutputStream(file),
  (x:FileOutputStream) => withResource(new BufferedOutputStream(x),
    (y: BufferedOutputStream) => withResource(new GzipCompressorOutputStream(y),
      (z: GzipCompressorOutputStream) => withResource(new TarArchiveOutputStream(z),
        (tar: TarArchiveOutputStream) => writeTar(tar, filePath, resultFolder)))));

And then also refactored the above to following form:

private def withResource[T: Resource : Manifest, X](t: T, block: T => X): X = managed(t).acquireAndGet(x => block(x))

def writeInFile(x: FileOutputStream): Try[Unit] = withResource(new BufferedOutputStream(x), writeInBuffer)    
def writeInBuffer(y: BufferedOutputStream): Try[Unit] = withResource(new GzipCompressorOutputStream(y), writeGzipStream)    
def writeGzipStream(z: GzipCompressorOutputStream): Try[Unit] = withResource(new TarArchiveOutputStream(z), writeTarStream)    
val file = new File(filePath, s"$resultFile.tar.gz")
withResource(new FileOutputStream(file), writeInFile);

The next day, a colleague mentioned this, which looks better than both of above: I am still exploring how to propagate result/error out of this block.

val file = new File(filePath, s"$resultFile.tar.gz")    
for {   
  stream <- managed(new FileOutputStream(file)) 
  buffer <- managed(new BufferedOutputStream(stream))   
  gzip <- managed(new GzipCompressorOutputStream(buffer))   
  tar <- managed(new TarArchiveOutputStream(gzip))  
} {
  writeTar(tar)
}

Similar to what @cchantep suggested, I ended up doing this:

  val tarOutputStream: ManagedResource[TarArchiveOutputStream] = (for {
    stream <- managed(new FileOutputStream(file))
    buffer <- managed(new BufferedOutputStream(stream))
    gzip <- managed(new GzipCompressorOutputStream(buffer))
    tar <- managed(new TarArchiveOutputStream(gzip))
  } yield tar)

  Try (tarOutputStream.acquireAndGet(writeTarStream(_))) match {
    case Failure(e) => Failure(e)
    case Success(_) => Success(new File(s"$filePath/$runLabel.tar.gz"))
  }
Asad Iqbal
  • 3,241
  • 4
  • 32
  • 52