src/Pure/General/file.scala
changeset 52671 9a360530eac8
parent 51982 fb4352e89022
child 53336 b3bf6d72fea5
equal deleted inserted replaced
52670:57a00f274130 52671:9a360530eac8
    11   OutputStream, InputStream, FileInputStream, BufferedInputStream, BufferedReader,
    11   OutputStream, InputStream, FileInputStream, BufferedInputStream, BufferedReader,
    12   InputStreamReader, File => JFile, IOException}
    12   InputStreamReader, File => JFile, IOException}
    13 import java.util.zip.{GZIPInputStream, GZIPOutputStream}
    13 import java.util.zip.{GZIPInputStream, GZIPOutputStream}
    14 
    14 
    15 import scala.collection.mutable
    15 import scala.collection.mutable
    16 
       
    17 import org.tukaani.xz.{LZMA2Options, XZInputStream, XZOutputStream}
       
    18 
    16 
    19 
    17 
    20 object File
    18 object File
    21 {
    19 {
    22   /* directory content */
    20   /* directory content */
    75   def read_gzip(file: JFile): String =
    73   def read_gzip(file: JFile): String =
    76     read_stream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(file))))
    74     read_stream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(file))))
    77 
    75 
    78   def read_gzip(path: Path): String = read_gzip(path.file)
    76   def read_gzip(path: Path): String = read_gzip(path.file)
    79 
    77 
    80   def read_xz(file: JFile): String =
       
    81     read_stream(new XZInputStream(new BufferedInputStream(new FileInputStream(file))))
       
    82 
       
    83   def read_xz(path: Path): String = read_xz(path.file)
       
    84 
       
    85 
    78 
    86   /* read lines */
    79   /* read lines */
    87 
    80 
    88   def read_lines(reader: BufferedReader, progress: String => Unit): List[String] =
    81   def read_lines(reader: BufferedReader, progress: String => Unit): List[String] =
    89   {
    82   {
   111   }
   104   }
   112 
   105 
   113 
   106 
   114   /* write */
   107   /* write */
   115 
   108 
   116   private def write_file(file: JFile, text: Iterable[CharSequence],
   109   def write_file(file: JFile, text: Iterable[CharSequence],
   117     make_stream: OutputStream => OutputStream)
   110     make_stream: OutputStream => OutputStream)
   118   {
   111   {
   119     val stream = make_stream(new FileOutputStream(file))
   112     val stream = make_stream(new FileOutputStream(file))
   120     val writer = new BufferedWriter(new OutputStreamWriter(stream, UTF8.charset))
   113     val writer = new BufferedWriter(new OutputStreamWriter(stream, UTF8.charset))
   121     try { text.iterator.foreach(writer.append(_)) }
   114     try { text.iterator.foreach(writer.append(_)) }
   130   def write_gzip(file: JFile, text: Iterable[CharSequence]): Unit =
   123   def write_gzip(file: JFile, text: Iterable[CharSequence]): Unit =
   131     write_file(file, text, (s: OutputStream) => new GZIPOutputStream(new BufferedOutputStream(s)))
   124     write_file(file, text, (s: OutputStream) => new GZIPOutputStream(new BufferedOutputStream(s)))
   132   def write_gzip(file: JFile, text: CharSequence): Unit = write_gzip(file, List(text))
   125   def write_gzip(file: JFile, text: CharSequence): Unit = write_gzip(file, List(text))
   133   def write_gzip(path: Path, text: Iterable[CharSequence]): Unit = write_gzip(path.file, text)
   126   def write_gzip(path: Path, text: Iterable[CharSequence]): Unit = write_gzip(path.file, text)
   134   def write_gzip(path: Path, text: CharSequence): Unit = write_gzip(path.file, text)
   127   def write_gzip(path: Path, text: CharSequence): Unit = write_gzip(path.file, text)
   135 
       
   136   def write_xz(file: JFile, text: Iterable[CharSequence], preset: Int = 3)
       
   137   {
       
   138     val options = new LZMA2Options
       
   139     options.setPreset(preset)
       
   140     write_file(file, text, (s: OutputStream) =>
       
   141       new XZOutputStream(new BufferedOutputStream(s), options))
       
   142   }
       
   143 
   128 
   144 
   129 
   145   /* copy */
   130   /* copy */
   146 
   131 
   147   def eq(file1: JFile, file2: JFile): Boolean =
   132   def eq(file1: JFile, file2: JFile): Boolean =