在scala中管理(ARM)嵌套资源

时间:2017-04-04 23:17:24

标签: scala scala-arm

所以,我在scala中有这个代码,我转换为托管。

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();
}

首次尝试是:

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()
                                  }
                                }))))

然而,它看起来并不可读。有没有更好的方法来使它managed但也可读?

2 个答案:

答案 0 :(得分:0)

您是否考虑过加载模式?

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

然后你会像以下一样使用它:

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

如果你为每个文件使用单独的加载,你最终会得到嵌套的块......(在某些情况下这可能是最合理的选择),但在这里我想它就足够了:

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()
  }
}

用过:

withTarStream(filePath, s"$resultFile.tar.gz") { tarStream =>
  addFileToTarGz(tarStream, filePath + "/" + resultFolder, "")
}

答案 1 :(得分:0)

基于@Mateusz Kubuszok的建议,我尝试了这些变化:

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)))));

然后还将以上形式重构为以下形式:

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);

第二天,一位同事提到了这一点,它看起来比上述两种方式都好:我还在探索如何将结果/错误传播出来。

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)
}

与@cchantep建议的相似,我最终做到了这一点:

  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"))
  }