root/lang/scala/sandbox/src/jp/gr/java_conf/mizu/io/IO.scala @ 6510

Revision 6510, 4.0 kB (checked in by kmizu, 7 years ago)

add implicit conversions

Line 
1package jp.gr.java_conf.mizu.io
2
3import scala.collection.mutable._
4import java.io._
5import java.net._
6
7object IO {
8  private def each[T](in :{ def read() :int })(proc : int => T) {
9    while(true) {
10      val i = in.read
11      if(i < 0) return
12      proc(i)
13    }
14  }
15
16  private def using[T <: Closeable, V](x :T)(proc : => V) = {
17    try {
18      proc
19    } finally {
20      x.close
21    }
22  }
23
24  final class RichInputStream(in :InputStream)
25    extends PushbackInputStream(in) with Iterator[byte] {
26
27    override def hasNext :Boolean = {
28      val i = read
29      if(i < 0) false else { unread(i); true }
30    }
31
32    override def next :byte = read.asInstanceOf[byte]
33
34    def >>(out :OutputStream) :unit = for(b <- this) out.write(b)
35
36    def readAll :Array[byte] = {
37      var b = new ArrayBuffer[byte]
38      each(this)(i => b += i.asInstanceOf[byte])
39      b.toArray
40    }
41  }
42
43  final class RichReader(in :Reader)
44    extends PushbackReader(in) with Iterator[char] {
45
46    override def hasNext :Boolean = {
47      val i = read
48      if(i < 0) false else { unread(i); true }
49    }
50
51    override def next :char = read.asInstanceOf[char]
52
53    def >>(out :Writer) :unit = for(b <- this) out.write(b)
54
55    def readAll :String = {
56      var b = new ArrayBuffer[char]
57      each(this)(i => b += i.asInstanceOf[char])
58      new String(b.toArray)
59    }
60
61    def readLine() :String = {
62      val s = new StringBuffer
63      while(true) {
64        val i = read
65        if(i < 0) return if(s.length == 0) null else new String(s)
66        if(i == '\n') return new String(s)
67        if(i == '\r') {
68          val j = read
69          if(j == -1 || j == '\n') return new String(s)
70          unread(j)
71          return new String(s)
72        }
73        s append i.asInstanceOf[char]
74      }
75      null
76    }
77
78    def eachLine[T](proc :String => T) :unit = {
79      while(true) {
80        val line = readLine
81        if(line == null) return
82        proc(line)
83      }
84    }
85  }
86
87  implicit def toFilenameFilter(filter :(File, String) => Boolean) = {
88    new FilenameFilter {
89      def accept(dir :File, name :String) :Boolean = filter(dir, name)
90    }
91  }
92
93  implicit def toFileFilter(filter :File => Boolean) = {
94    new FileFilter {
95      def accept(pathname :File) :Boolean = filter(pathname)
96    }
97  }
98
99  def openIn[T](file :File)(proc: RichInputStream => T) = {
100    val in = new RichInputStream(
101      new BufferedInputStream(new FileInputStream(file))
102    )
103    using(in)(proc(in))
104  }
105
106  def openIn[T](file :String)(proc :RichInputStream => T) = {
107    val in = new RichInputStream(
108      new BufferedInputStream(new FileInputStream(file))
109    )
110    using(in)(proc(in))
111  }
112
113  def openIn[T](url :URL)(proc: RichInputStream => T) = {
114    val in = new RichInputStream(
115      new BufferedInputStream(url.openStream)
116    )
117    using(in)(proc(in))
118  }
119 
120  def openReader[T](file :String)(proc :RichReader => T) = {
121    val in = new RichReader(
122      new BufferedReader(new FileReader(file))
123    )
124    using(in)(proc(in))
125  }
126
127  def openReader[T](file :File)(proc: RichReader => T) = {
128    val in = new RichReader(
129      new BufferedReader(new FileReader(file))
130    )
131    using(in)(proc(in))
132  }
133
134  def openReader[T](url :URL)(proc :RichReader => T) = {
135    val in = new RichReader(
136      new BufferedReader(new InputStreamReader(url.openStream))
137    )
138    using(in)(proc(in))
139  }
140
141  def openOut[T](file :File)(proc :PrintStream => T) = {
142    val out = new PrintStream(
143      new BufferedOutputStream(new FileOutputStream(file))
144    )
145    using(out)(proc(out))
146  }
147
148  def openWriter[T](file :File)(proc :PrintWriter => T) = {
149    val out = new PrintWriter(
150      new BufferedWriter(new FileWriter(file))
151    )
152    using(out)(proc(out))
153  }
154
155  def eachLine[T](file :File)(proc :String => T) {
156    openReader(file)(in => in.eachLine(line => proc(line)))
157  }
158
159  //Usage
160  def main(args :Array[String]) {
161    IO.openReader(args(0)) {in =>
162      in.eachLine{line =>
163        println(line)
164      }
165    }
166  }
167}
Note: See TracBrowser for help on using the browser.