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

package scalabook.file

import path.Path

object NativeFile {
  def apply(nfile: java.io.File) =
    new NativeFile(NativeFS.mkpath(nfile.getPath))

  def apply(path: Path) = new NativeFile(path)

  def apply(path: String) =
    new NativeFile(NativeFS.mkpath(path))
}

class NativeFile(val path: Path) extends VFile[NativeFile] {
  private[this] lazy val nfile =
    new java.io.File(path.fullName)
  
  override def asNative = Some(this)

  def /(that: Path) = new NativeFile(this.path / that)
  def /(that: String) = new NativeFile(this.path / that)

  def exists = nfile.exists
  def isFile = nfile.isFile
  def isFolder = nfile.isDirectory

  def inputStream = if(isFile)
       Some(new java.io.FileInputStream(path.fullName))
  else None

  def outputStream = if(isFile)
       Some(new java.io.FileOutputStream(path.fullName))
  else None

  def children = nfile.listFiles match {
    case null => Array()
    case array => array.map { file =>
      NativeFile(file.getPath)
    } //toList
  }

  def childrenNames = nfile.list match {
    case null => Array()
    case array => array
  }

  def nativeJavaFile = nfile
}