All Downloads are FREE. Search and download functionalities are using the official Maven repository.

sttp.client4.internal.FileHelpers.scala Maven / Gradle / Ivy

There is a newer version: 4.0.0-M19
Show newest version
package sttp.client4.internal

import java.io.{BufferedInputStream, File, FileInputStream, FileOutputStream, InputStream}

import scala.util.control.NonFatal

private[client4] object FileHelpers {
  def saveFile(file: File, is: InputStream): Unit = {
    if (!file.exists()) {
      if (file.getParentFile != null) {
        file.getParentFile.mkdirs()
      }
      file.createNewFile()
    }

    withResources(new FileOutputStream(file))(os => transfer(is, os))
  }

  def readFile(file: File): Array[Byte] =
    withResources(new BufferedInputStream(new FileInputStream(file)))(toByteArray)

  // based on https://medium.com/@dkomanov/scala-try-with-resources-735baad0fd7d
  private def withResources[T <: AutoCloseable, V](r: => T)(f: T => V): V = {
    val resource: T = r
    require(resource != null, "resource is null")
    var exception: Throwable = null
    try
      f(resource)
    catch {
      case NonFatal(e) =>
        exception = e
        throw e
    } finally
      closeAndAddSuppressed(exception, resource)
  }

  private def closeAndAddSuppressed(e: Throwable, resource: AutoCloseable): Unit =
    if (e != null) {
      try
        resource.close()
      catch {
        case NonFatal(suppressed) =>
          e.addSuppressed(suppressed)
      }
    } else {
      resource.close()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy