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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
package java.nio
private[nio] final class HeapByteBuffer private (
_capacity: Int, _array0: Array[Byte], _arrayOffset0: Int,
_initialPosition: Int, _initialLimit: Int, _readOnly: Boolean)
extends ByteBuffer(_capacity, _array0, _arrayOffset0) {
position(_initialPosition)
limit(_initialLimit)
def isReadOnly(): Boolean = _readOnly
def isDirect(): Boolean = false
def slice(): ByteBuffer = {
val cap = remaining
new HeapByteBuffer(cap, _array, _arrayOffset+position, 0, cap, isReadOnly)
}
def duplicate(): ByteBuffer = {
val result = new HeapByteBuffer(capacity, _array, _arrayOffset,
position, limit, isReadOnly)
result._mark = this._mark
result
}
def asReadOnlyBuffer(): ByteBuffer = {
val result = new HeapByteBuffer(capacity, _array, _arrayOffset,
position, limit, true)
result._mark = this._mark
result
}
def get(): Byte = {
if (!hasRemaining)
throw new BufferUnderflowException
val p = position
position(p + 1)
_array(_arrayOffset + p)
}
def put(b: Byte): ByteBuffer = {
if (isReadOnly)
throw new ReadOnlyBufferException
if (!hasRemaining)
throw new BufferOverflowException
val p = position
_array(_arrayOffset + p) = b
position(p + 1)
this
}
def get(index: Int): Byte = {
if (index < 0 || index >= limit)
throw new IndexOutOfBoundsException
_array(_arrayOffset + index)
}
def put(index: Int, b: Byte): ByteBuffer = {
if (isReadOnly)
throw new ReadOnlyBufferException
if (index < 0 || index >= limit)
throw new IndexOutOfBoundsException
_array(_arrayOffset + index) = b
this
}
override def get(dst: Array[Byte], offset: Int, length: Int): ByteBuffer = {
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
System.arraycopy(_array, startPos + _arrayOffset, dst, offset, length)
position(endPos)
this
}
override def put(src: Array[Byte], offset: Int, length: Int): ByteBuffer = {
val end = offset + length
if (offset < 0 || length < 0 || end > src.length)
throw new IndexOutOfBoundsException
if (isReadOnly)
throw new ReadOnlyBufferException
val startPos = position
val endPos = startPos + length
if (endPos > limit)
throw new BufferOverflowException
System.arraycopy(src, offset, _array, startPos + _arrayOffset, length)
position(endPos)
this
}
def compact(): ByteBuffer = {
if (isReadOnly)
throw new ReadOnlyBufferException
val offset = _arrayOffset
val len = remaining
System.arraycopy(_array, offset + position, _array, offset, len)
_mark = -1
limit(capacity)
position(len)
this
}
}
private[nio] object HeapByteBuffer {
private[nio] def wrap(array: Array[Byte], arrayOffset: Int, capacity: Int,
initialPosition: Int, initialLength: Int,
isReadOnly: Boolean): ByteBuffer = {
if (arrayOffset < 0 || capacity < 0 || arrayOffset+capacity > array.length)
throw new IndexOutOfBoundsException
val initialLimit = initialPosition + initialLength
if (initialPosition < 0 || initialLength < 0 || initialLimit > capacity)
throw new IndexOutOfBoundsException
new HeapByteBuffer(capacity, array, arrayOffset,
initialPosition, initialLimit, isReadOnly)
}
}
|