Further organization of File/Path/Directory. Adds a
copyFile method. git-svn-id: http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk@18628 5e8d7ff9-d8ef-0310-90f0-a4852d11357a
This commit is contained in:
parent
6fb7542f20
commit
6bffe6faaf
|
@ -33,7 +33,6 @@ class Directory(jfile: JFile) extends Path(jfile)
|
|||
{
|
||||
override def toDirectory: Directory = this
|
||||
override def toFile: File = new File(jfile)
|
||||
override def create(): Boolean = jfile.mkdirs()
|
||||
override def isValid = jfile.isDirectory() || !jfile.exists()
|
||||
|
||||
/** An iterator over the contents of this directory.
|
||||
|
|
|
@ -12,7 +12,7 @@ package scala.io
|
|||
|
||||
import java.io.{
|
||||
FileInputStream, FileOutputStream, BufferedReader, BufferedWriter, InputStreamReader, OutputStreamWriter,
|
||||
BufferedInputStream, BufferedOutputStream, File => JFile }
|
||||
BufferedInputStream, BufferedOutputStream, IOException, File => JFile }
|
||||
import java.nio.channels.FileChannel
|
||||
import collection.Traversable
|
||||
|
||||
|
@ -25,7 +25,14 @@ object File
|
|||
// Create a temporary file
|
||||
def makeTemp(prefix: String = Path.randomPrefix, suffix: String = null, dir: JFile = null) =
|
||||
apply(JFile.createTempFile(prefix, suffix, dir))
|
||||
|
||||
import java.nio.channels.Channel
|
||||
type Closeable = { def close(): Unit }
|
||||
def closeQuietly(target: Closeable) {
|
||||
try target.close() catch { case e: IOException => }
|
||||
}
|
||||
}
|
||||
import File._
|
||||
import Path._
|
||||
|
||||
/** An abstraction for files. For character data, a Codec
|
||||
|
@ -45,7 +52,6 @@ with Streamable.Chars
|
|||
override def toDirectory: Directory = new Directory(jfile)
|
||||
override def toFile: File = this
|
||||
|
||||
override def create(): Boolean = jfile.createNewFile()
|
||||
override def isValid = jfile.isFile() || !jfile.exists()
|
||||
override def length = super[Path].length
|
||||
|
||||
|
@ -75,5 +81,38 @@ with Streamable.Chars
|
|||
finally out close
|
||||
}
|
||||
|
||||
def copyFile(destPath: Path, preserveFileDate: Boolean = false) = {
|
||||
val FIFTY_MB = 1024 * 1024 * 50
|
||||
val dest = destPath.toFile
|
||||
if (!isValid) fail("Source %s is not a valid file." format name)
|
||||
if (this.normalize == dest.normalize) fail("Source and destination are the same.")
|
||||
if (!dest.parent.map(_.exists).getOrElse(false)) fail("Destination cannot be created.")
|
||||
if (dest.exists && !dest.canWrite) fail("Destination exists but is not writable.")
|
||||
if (dest.isDirectory) fail("Destination exists but is a directory.")
|
||||
|
||||
lazy val in_s = inputStream()
|
||||
lazy val out_s = dest.outputStream()
|
||||
lazy val in = in_s.getChannel()
|
||||
lazy val out = out_s.getChannel()
|
||||
|
||||
try {
|
||||
val size = in.size()
|
||||
var pos, count = 0L
|
||||
while (pos < size) {
|
||||
count = (size - pos) min FIFTY_MB
|
||||
pos += out.transferFrom(in, pos, count)
|
||||
}
|
||||
}
|
||||
finally List[Closeable](out, out_s, in, in_s) foreach closeQuietly
|
||||
|
||||
if (this.length != dest.length)
|
||||
fail("Failed to completely copy %s to %s".format(name, dest.name))
|
||||
|
||||
if (preserveFileDate)
|
||||
dest.lastModified = this.lastModified
|
||||
|
||||
()
|
||||
}
|
||||
|
||||
override def toString() = "File(%s)".format(path)
|
||||
}
|
||||
|
|
|
@ -125,9 +125,14 @@ class Path private[io] (val jfile: JFile)
|
|||
def isDirectory = jfile.isDirectory()
|
||||
def isAbsolute = jfile.isAbsolute()
|
||||
def isHidden = jfile.isHidden()
|
||||
def isSymlink = parent.isDefined && {
|
||||
val x = parent.get / name
|
||||
x.normalize != x.toAbsolute
|
||||
}
|
||||
|
||||
// Information
|
||||
def lastModified = jfile.lastModified()
|
||||
def lastModified_=(time: Long) = jfile setLastModified time // should use setXXX function?
|
||||
def length = jfile.length()
|
||||
|
||||
// Boolean path comparisons
|
||||
|
@ -137,7 +142,6 @@ class Path private[io] (val jfile: JFile)
|
|||
def isFresher(other: Path) = lastModified > other.lastModified
|
||||
|
||||
// creations
|
||||
def create(): Boolean = true
|
||||
def createDirectory(force: Boolean = true, failIfExists: Boolean = false): Directory = {
|
||||
val res = if (force) jfile.mkdirs() else jfile.mkdir()
|
||||
if (!res && exists && failIfExists) fail("Directory '%s' already exists." format name)
|
||||
|
|
Loading…
Reference in New Issue