summaryrefslogtreecommitdiff
path: root/examples/scala-js/javalib/src/main/scala/java/nio/StringCharBuffer.scala
blob: 25bc594f108b939438a7b88d5a4de43d6d15cdb9 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package java.nio

private[nio] final class StringCharBuffer private (
    _capacity: Int, private[this] var _csq: CharSequence,
    private[this] var _csqOffset: Int,
    _initialPosition: Int, _initialLimit: Int)
    extends CharBuffer(_capacity) {

  position(_initialPosition)
  limit(_initialLimit)

  def isReadOnly(): Boolean = true

  def isDirect(): Boolean = false

  def slice(): CharBuffer = {
    val cap = remaining
    new StringCharBuffer(cap, _csq, _csqOffset + position, 0, cap)
  }

  def duplicate(): CharBuffer = {
    val result = new StringCharBuffer(capacity, _csq, _csqOffset,
        position, limit)
    result._mark = this._mark
    result
  }

  def asReadOnlyBuffer(): CharBuffer = duplicate()

  def subSequence(start: Int, end: Int): CharBuffer = {
    if (start < 0 || end < start || end > remaining)
      throw new IndexOutOfBoundsException
    new StringCharBuffer(capacity, _csq, _csqOffset,
        position + start, position + end)
  }

  def get(): Char = {
    if (!hasRemaining)
      throw new BufferUnderflowException
    val p = position
    position(p + 1)
    _csq.charAt(_csqOffset + p)
  }

  def put(c: Char): CharBuffer =
    throw new ReadOnlyBufferException

  def get(index: Int): Char = {
    if (index < 0 || index >= limit)
      throw new IndexOutOfBoundsException
    _csq.charAt(_csqOffset + index)
  }

  def put(index: Int, b: Char): CharBuffer =
    throw new ReadOnlyBufferException

  override def get(dst: Array[Char], offset: Int, length: Int): CharBuffer = {
    val end = offset + length

    if (offset < 0 || length < 0 || end > dst.length)
      throw new IndexOutOfBoundsException

    val startPos = position
    val endPos = startPos + length
    if (endPos > limit)
      throw new BufferUnderflowException

    var i = offset
    var j = startPos + _csqOffset
    while (i != end) {
      dst(i) = _csq.charAt(j)
      i += 1
      j += 1
    }
    position(endPos)

    this
  }

  def compact(): CharBuffer =
    throw new ReadOnlyBufferException

  override def toString(): String = {
    val offset = _csqOffset
    _csq.subSequence(position + offset, limit + offset).toString()
  }

  def order(): ByteOrder = ByteOrder.nativeOrder()
}

private[nio] object StringCharBuffer {
  private[nio] def wrap(csq: CharSequence, csqOffset: Int, capacity: Int,
      initialPosition: Int, initialLength: Int): CharBuffer = {
    if (csqOffset < 0 || capacity < 0 || csqOffset+capacity > csq.length)
      throw new IndexOutOfBoundsException
    val initialLimit = initialPosition + initialLength
    if (initialPosition < 0 || initialLength < 0 || initialLimit > capacity)
      throw new IndexOutOfBoundsException
    new StringCharBuffer(capacity, csq, csqOffset,
        initialPosition, initialLimit)
  }
}