-
Notifications
You must be signed in to change notification settings - Fork 0
/
ByteBufferOutputStream.scala
47 lines (36 loc) · 1.21 KB
/
ByteBufferOutputStream.scala
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
package io.github.iostreamsinspark.out
import java.io.ByteArrayOutputStream
import java.nio.ByteBuffer
/**
* Provide a zero-copy way to convert data in ByteArrayOutputStream to ByteBuffer
*/
class ByteBufferOutputStream(capacity: Int) extends ByteArrayOutputStream(capacity) {
def this() = this(32)
def getCount(): Int = count
private[this] var closed: Boolean = false
// NOTE: this method is not to write a int number, but a byte(since a byte is
// belong to [0, 127]).
override def write(b: Int): Unit = {
require(!closed, "cannot write to a closed ByteBufferOutputStream")
super.write(b)
}
override def write(b: Array[Byte], off: Int, len: Int): Unit = {
require(!closed, "cannot write to a closed ByteBufferOutputStream")
super.write(b, off, len)
}
override def reset(): Unit = {
require(!closed, "cannot reset a closed ByteBufferOutputStream")
super.reset()
}
override def close(): Unit = {
if (!closed) {
super.close()
closed = true
}
}
// convert a byte array to a ByteBuffer
def toByteBuffer: ByteBuffer = {
require(closed, "can only call toByteBuffer() after ByteBufferOutputStream has been closed")
ByteBuffer.wrap(buf, 0, count)
}
}