scala/ScalaBook/chapter-10/src/main/scala/scalabook/file/FileUtil.scala

package scalabook.file

import java.io.{File => JavaFile}

object FileUtil {
  import VFS.AnyFile
  
  def createTmpFile =
    NativeFS.newTempFile("scalabook-vfs", "tmp")

  def copy[T <: VFile[T]](from: AnyFile, to: T): T = {
    IOUtil.copy(
      from.inputStream.get,
      to.outputStream.get,
      true, true)
    to
  }

  def materializeToNative(file: AnyFile): NativeFile =
    if(file.isNative)
      file.asNative.get
    else if(!file.exists)
      error("File %s does not exist".format(file))
    else if(file.isFolder)
      error("File %s is a folder".format(file))
    else
      copy(file, createTmpFile)
}