src/Pure/General/file.scala
changeset 64002 08f89f0e8a62
parent 64000 445b3deced8f
child 64003 73af1d36aeff
equal deleted inserted replaced
64001:7ecb22be8f03 64002:08f89f0e8a62
    14   Files, SimpleFileVisitor, FileVisitResult}
    14   Files, SimpleFileVisitor, FileVisitResult}
    15 import java.nio.file.attribute.BasicFileAttributes
    15 import java.nio.file.attribute.BasicFileAttributes
    16 import java.net.{URL, URLDecoder, MalformedURLException}
    16 import java.net.{URL, URLDecoder, MalformedURLException}
    17 import java.util.zip.{GZIPInputStream, GZIPOutputStream}
    17 import java.util.zip.{GZIPInputStream, GZIPOutputStream}
    18 import java.util.regex.Pattern
    18 import java.util.regex.Pattern
       
    19 
       
    20 import org.tukaani.xz.{XZInputStream, XZOutputStream}
    19 
    21 
    20 import scala.collection.mutable
    22 import scala.collection.mutable
    21 import scala.util.matching.Regex
    23 import scala.util.matching.Regex
    22 
    24 
    23 
    25 
   194 
   196 
   195   def read_gzip(file: JFile): String =
   197   def read_gzip(file: JFile): String =
   196     read_stream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(file))))
   198     read_stream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(file))))
   197   def read_gzip(path: Path): String = read_gzip(path.file)
   199   def read_gzip(path: Path): String = read_gzip(path.file)
   198 
   200 
   199   def read_xz(file: JFile): String = read_stream(XZ.uncompress(new FileInputStream(file)))
   201   def read_xz(file: JFile): String =
       
   202     read_stream(new XZInputStream(new BufferedInputStream(new FileInputStream(file))))
   200   def read_xz(path: Path): String = read_xz(path.file)
   203   def read_xz(path: Path): String = read_xz(path.file)
   201 
   204 
   202 
   205 
   203   /* read lines */
   206   /* read lines */
   204 
   207 
   215   }
   218   }
   216 
   219 
   217 
   220 
   218   /* write */
   221   /* write */
   219 
   222 
   220   def write_file(file: JFile, text: Iterable[CharSequence],
   223   def write_file(file: JFile, text: CharSequence, make_stream: OutputStream => OutputStream)
   221     make_stream: OutputStream => OutputStream)
       
   222   {
   224   {
   223     val stream = make_stream(new FileOutputStream(file))
   225     val stream = make_stream(new FileOutputStream(file))
   224     val writer = new BufferedWriter(new OutputStreamWriter(stream, UTF8.charset))
   226     val writer = new BufferedWriter(new OutputStreamWriter(stream, UTF8.charset))
   225     try { text.iterator.foreach(writer.append(_)) }
   227     try { writer.append(text) } finally { writer.close }
   226     finally { writer.close }
   228   }
   227   }
   229 
   228 
   230   def write(file: JFile, text: CharSequence): Unit = write_file(file, text, (s) => s)
   229   def write(file: JFile, text: Iterable[CharSequence]): Unit = write_file(file, text, (s) => s)
       
   230   def write(file: JFile, text: CharSequence): Unit = write(file, List(text))
       
   231   def write(path: Path, text: Iterable[CharSequence]): Unit = write(path.file, text)
       
   232   def write(path: Path, text: CharSequence): Unit = write(path.file, text)
   231   def write(path: Path, text: CharSequence): Unit = write(path.file, text)
   233 
   232 
   234   def write_gzip(file: JFile, text: Iterable[CharSequence]): Unit =
   233   def write_gzip(file: JFile, text: CharSequence): Unit =
   235     write_file(file, text, (s: OutputStream) => new GZIPOutputStream(new BufferedOutputStream(s)))
   234     write_file(file, text, (s: OutputStream) => new GZIPOutputStream(new BufferedOutputStream(s)))
   236   def write_gzip(file: JFile, text: CharSequence): Unit = write_gzip(file, List(text))
       
   237   def write_gzip(path: Path, text: Iterable[CharSequence]): Unit = write_gzip(path.file, text)
       
   238   def write_gzip(path: Path, text: CharSequence): Unit = write_gzip(path.file, text)
   235   def write_gzip(path: Path, text: CharSequence): Unit = write_gzip(path.file, text)
   239 
   236 
   240   def write_xz(file: JFile, text: Iterable[CharSequence]): Unit = XZ.write_file(file, text)
   237   def write_xz(file: JFile, text: CharSequence, options: XZ.Options): Unit =
   241   def write_xz(file: JFile, text: CharSequence): Unit = XZ.write_file(file, List(text))
   238     File.write_file(file, text, (s) => new XZOutputStream(new BufferedOutputStream(s), options))
   242   def write_xz(path: Path, text: Iterable[CharSequence]): Unit = XZ.write_file(path.file, text)
   239   def write_xz(path: Path, text: CharSequence, options: XZ.Options): Unit =
   243   def write_xz(path: Path, text: CharSequence): Unit = XZ.write_file(path.file, List(text))
   240     write_xz(path.file, text, options)
   244 
   241 
   245   def write_backup(path: Path, text: CharSequence)
   242   def write_backup(path: Path, text: CharSequence)
   246   {
   243   {
   247     path.file renameTo path.backup.file
   244     path.file renameTo path.backup.file
   248     write(path, text)
   245     write(path, text)