summaryrefslogtreecommitdiff
path: root/examples/scala-js/javalib/src/main/scala/java/io/Reader.scala
blob: 97be140884556ae2911645fa9bbe391860aeff4d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package java.io

import java.nio.CharBuffer

abstract class Reader private[this] (_lock: Option[Object])
    extends Readable with Closeable {

  protected val lock = _lock.getOrElse(this)

  protected def this(lock: Object) = this(Some(lock))
  protected def this() = this(None)

  def read(target: CharBuffer): Int = {
    if (!target.hasRemaining) 0
    else if (target.hasArray) {
      val charsRead = read(target.array,
          target.position + target.arrayOffset, target.remaining)
      if (charsRead != -1)
        target.position(target.position + charsRead)
      charsRead
    } else {
      val buf = new Array[Char](target.remaining)
      val charsRead = read(buf)
      if (charsRead != -1)
        target.put(buf, 0, charsRead)
      charsRead
    }
  }

  def read(): Int = {
    val buf = new Array[Char](1)
    if (read(buf) == -1) -1
    else buf(0).toInt
  }

  def read(cbuf: Array[Char]): Int =
    read(cbuf, 0, cbuf.length)

  def read(cbuf: Array[Char], off: Int, len: Int): Int

  def skip(n: Long): Long = {
    if (n < 0)
      throw new IllegalArgumentException("Cannot skip negative amount")
    else if (read() == -1) 0
    else 1
  }

  def ready(): Boolean = false

  def markSupported(): Boolean = false

  def mark(readAheadLimit: Int): Unit =
    throw new IOException("Mark not supported")

  def reset(): Unit =
    throw new IOException("Reset not supported")

  def close(): Unit

}