2011-05-22 11:00:51 -06:00
|
|
|
package akka.util
|
|
|
|
|
|
|
|
|
|
import java.nio.ByteBuffer
|
2011-12-31 09:18:37 -07:00
|
|
|
import java.nio.charset.Charset
|
2011-05-22 11:00:51 -06:00
|
|
|
|
2011-12-31 09:18:37 -07:00
|
|
|
import scala.collection.{ IndexedSeqOptimized, LinearSeq }
|
|
|
|
|
import scala.collection.mutable.{ Builder, ArrayBuilder, WrappedArray }
|
|
|
|
|
import scala.collection.immutable.{ IndexedSeq, VectorBuilder }
|
2011-05-22 11:00:51 -06:00
|
|
|
import scala.collection.generic.{ CanBuildFrom, GenericCompanion }
|
|
|
|
|
|
|
|
|
|
object ByteString {
|
|
|
|
|
|
2011-06-05 14:04:18 -06:00
|
|
|
def apply(bytes: Array[Byte]): ByteString = ByteString1(bytes.clone)
|
2011-05-22 11:00:51 -06:00
|
|
|
|
2011-05-22 13:53:58 -06:00
|
|
|
def apply(bytes: Byte*): ByteString = {
|
|
|
|
|
val ar = new Array[Byte](bytes.size)
|
|
|
|
|
bytes.copyToArray(ar)
|
2011-06-05 14:04:18 -06:00
|
|
|
ByteString1(ar)
|
2011-05-22 13:53:58 -06:00
|
|
|
}
|
|
|
|
|
|
2011-05-22 11:00:51 -06:00
|
|
|
def apply[T](bytes: T*)(implicit num: Integral[T]): ByteString =
|
2011-06-05 14:04:18 -06:00
|
|
|
ByteString1(bytes.map(x ⇒ num.toInt(x).toByte)(collection.breakOut))
|
2011-05-22 11:00:51 -06:00
|
|
|
|
|
|
|
|
def apply(bytes: ByteBuffer): ByteString = {
|
|
|
|
|
val ar = new Array[Byte](bytes.remaining)
|
|
|
|
|
bytes.get(ar)
|
2011-06-05 14:04:18 -06:00
|
|
|
ByteString1(ar)
|
2011-05-22 11:00:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def apply(string: String): ByteString = apply(string, "UTF-8")
|
|
|
|
|
|
2011-06-05 14:04:18 -06:00
|
|
|
def apply(string: String, charset: String): ByteString = ByteString1(string.getBytes(charset))
|
2011-05-22 11:00:51 -06:00
|
|
|
|
2011-12-31 09:18:37 -07:00
|
|
|
def fromArray(array: Array[Byte], offset: Int, length: Int): ByteString = {
|
|
|
|
|
val copyOffset = math.max(offset, 0)
|
|
|
|
|
val copyLength = math.max(math.min(array.length - copyOffset, length), 0)
|
|
|
|
|
if (copyLength == 0) empty
|
|
|
|
|
else {
|
|
|
|
|
val copyArray = new Array[Byte](copyLength)
|
|
|
|
|
Array.copy(array, copyOffset, copyArray, 0, copyLength)
|
|
|
|
|
ByteString1(copyArray)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-05 14:04:18 -06:00
|
|
|
val empty: ByteString = ByteString1(Array.empty[Byte])
|
2011-05-22 11:00:51 -06:00
|
|
|
|
2011-12-31 09:18:37 -07:00
|
|
|
def newBuilder = new ByteStringBuilder
|
2011-05-22 11:00:51 -06:00
|
|
|
|
|
|
|
|
implicit def canBuildFrom = new CanBuildFrom[TraversableOnce[Byte], Byte, ByteString] {
|
|
|
|
|
def apply(from: TraversableOnce[Byte]) = newBuilder
|
|
|
|
|
def apply() = newBuilder
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-31 09:18:37 -07:00
|
|
|
private[akka] object ByteString1 {
|
2011-06-05 14:04:18 -06:00
|
|
|
def apply(bytes: Array[Byte]) = new ByteString1(bytes)
|
2011-05-24 10:55:38 -06:00
|
|
|
}
|
2011-05-22 11:00:51 -06:00
|
|
|
|
2011-12-31 09:18:37 -07:00
|
|
|
final class ByteString1 private (private val bytes: Array[Byte], private val startIndex: Int, val length: Int) extends ByteString {
|
2011-05-22 11:00:51 -06:00
|
|
|
|
2011-06-05 14:04:18 -06:00
|
|
|
private def this(bytes: Array[Byte]) = this(bytes, 0, bytes.length)
|
2011-05-22 15:16:38 -06:00
|
|
|
|
2011-06-05 14:04:18 -06:00
|
|
|
def apply(idx: Int): Byte = bytes(checkRangeConvert(idx))
|
2011-05-22 11:00:51 -06:00
|
|
|
|
2011-06-05 14:04:18 -06:00
|
|
|
private def checkRangeConvert(index: Int) = {
|
2011-12-31 09:18:37 -07:00
|
|
|
if (0 <= index && length > index)
|
|
|
|
|
index + startIndex
|
2011-06-05 14:04:18 -06:00
|
|
|
else
|
|
|
|
|
throw new IndexOutOfBoundsException(index.toString)
|
|
|
|
|
}
|
2011-05-22 11:00:51 -06:00
|
|
|
|
2011-06-05 14:04:18 -06:00
|
|
|
def toArray: Array[Byte] = {
|
|
|
|
|
val ar = new Array[Byte](length)
|
|
|
|
|
Array.copy(bytes, startIndex, ar, 0, length)
|
|
|
|
|
ar
|
|
|
|
|
}
|
2011-05-27 22:52:06 -06:00
|
|
|
|
2011-06-05 14:04:18 -06:00
|
|
|
override def clone: ByteString = new ByteString1(toArray)
|
2011-05-22 15:16:38 -06:00
|
|
|
|
2011-06-05 14:04:18 -06:00
|
|
|
def compact: ByteString =
|
2011-12-31 09:18:37 -07:00
|
|
|
if (length == bytes.length) this else clone
|
2011-05-24 10:55:38 -06:00
|
|
|
|
2011-06-05 14:04:18 -06:00
|
|
|
def asByteBuffer: ByteBuffer = {
|
|
|
|
|
val buffer = ByteBuffer.wrap(bytes, startIndex, length).asReadOnlyBuffer
|
|
|
|
|
if (buffer.remaining < bytes.length) buffer.slice
|
|
|
|
|
else buffer
|
|
|
|
|
}
|
2011-05-24 10:55:38 -06:00
|
|
|
|
2011-12-31 09:18:37 -07:00
|
|
|
def decodeString(charset: String): String =
|
|
|
|
|
new String(if (length == bytes.length) bytes else toArray, charset)
|
2011-05-27 18:53:18 -06:00
|
|
|
|
2011-06-05 14:04:18 -06:00
|
|
|
def ++(that: ByteString): ByteString = that match {
|
|
|
|
|
case b: ByteString1 ⇒ ByteStrings(this, b)
|
|
|
|
|
case bs: ByteStrings ⇒ ByteStrings(this, bs)
|
|
|
|
|
}
|
2011-05-27 18:53:18 -06:00
|
|
|
|
2011-06-05 14:04:18 -06:00
|
|
|
override def slice(from: Int, until: Int): ByteString = {
|
|
|
|
|
val newStartIndex = math.max(from, 0) + startIndex
|
2011-12-31 09:18:37 -07:00
|
|
|
val newLength = math.min(until, length) - from
|
|
|
|
|
if (newLength <= 0) ByteString.empty
|
|
|
|
|
else new ByteString1(bytes, newStartIndex, newLength)
|
2011-06-05 14:04:18 -06:00
|
|
|
}
|
2011-05-27 18:53:18 -06:00
|
|
|
|
2011-06-05 14:04:18 -06:00
|
|
|
override def copyToArray[A >: Byte](xs: Array[A], start: Int, len: Int): Unit =
|
|
|
|
|
Array.copy(bytes, startIndex, xs, start, math.min(math.min(length, len), xs.length - start))
|
2011-05-27 18:53:18 -06:00
|
|
|
|
2011-12-31 09:18:37 -07:00
|
|
|
def copyToBuffer(buffer: ByteBuffer): Int = {
|
|
|
|
|
val copyLength = math.min(buffer.remaining, length)
|
|
|
|
|
if (copyLength > 0) buffer.put(bytes, startIndex, copyLength)
|
|
|
|
|
copyLength
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-05 14:04:18 -06:00
|
|
|
}
|
2011-05-27 18:53:18 -06:00
|
|
|
|
2011-12-31 09:18:37 -07:00
|
|
|
private[akka] object ByteStrings {
|
|
|
|
|
def apply(bytestrings: Vector[ByteString1]): ByteString = new ByteStrings(bytestrings, (0 /: bytestrings)(_ + _.length))
|
|
|
|
|
|
|
|
|
|
def apply(bytestrings: Vector[ByteString1], length: Int): ByteString = new ByteStrings(bytestrings, length)
|
2011-05-27 18:53:18 -06:00
|
|
|
|
2011-06-05 14:04:18 -06:00
|
|
|
def apply(b1: ByteString1, b2: ByteString1): ByteString = compare(b1, b2) match {
|
2011-12-31 09:18:37 -07:00
|
|
|
case 3 ⇒ new ByteStrings(Vector(b1, b2), b1.length + b2.length)
|
2011-06-05 14:04:18 -06:00
|
|
|
case 2 ⇒ b2
|
|
|
|
|
case 1 ⇒ b1
|
|
|
|
|
case 0 ⇒ ByteString.empty
|
|
|
|
|
}
|
2011-05-27 18:53:18 -06:00
|
|
|
|
2011-06-05 14:04:18 -06:00
|
|
|
def apply(b: ByteString1, bs: ByteStrings): ByteString = compare(b, bs) match {
|
2011-12-31 09:18:37 -07:00
|
|
|
case 3 ⇒ new ByteStrings(b +: bs.bytestrings, bs.length + b.length)
|
2011-06-05 14:04:18 -06:00
|
|
|
case 2 ⇒ bs
|
|
|
|
|
case 1 ⇒ b
|
|
|
|
|
case 0 ⇒ ByteString.empty
|
|
|
|
|
}
|
2011-05-27 18:53:18 -06:00
|
|
|
|
2011-06-05 14:04:18 -06:00
|
|
|
def apply(bs: ByteStrings, b: ByteString1): ByteString = compare(bs, b) match {
|
2011-12-31 09:18:37 -07:00
|
|
|
case 3 ⇒ new ByteStrings(bs.bytestrings :+ b, bs.length + b.length)
|
2011-06-05 14:04:18 -06:00
|
|
|
case 2 ⇒ b
|
|
|
|
|
case 1 ⇒ bs
|
|
|
|
|
case 0 ⇒ ByteString.empty
|
|
|
|
|
}
|
2011-05-27 18:53:18 -06:00
|
|
|
|
2011-06-05 14:04:18 -06:00
|
|
|
def apply(bs1: ByteStrings, bs2: ByteStrings): ByteString = compare(bs1, bs2) match {
|
2011-12-31 09:18:37 -07:00
|
|
|
case 3 ⇒ new ByteStrings(bs1.bytestrings ++ bs2.bytestrings, bs1.length + bs2.length)
|
2011-06-05 14:04:18 -06:00
|
|
|
case 2 ⇒ bs2
|
|
|
|
|
case 1 ⇒ bs1
|
|
|
|
|
case 0 ⇒ ByteString.empty
|
|
|
|
|
}
|
2011-05-27 18:53:18 -06:00
|
|
|
|
2011-06-05 14:04:18 -06:00
|
|
|
// 0: both empty, 1: 2nd empty, 2: 1st empty, 3: neither empty
|
|
|
|
|
def compare(b1: ByteString, b2: ByteString): Int =
|
|
|
|
|
if (b1.length == 0)
|
|
|
|
|
if (b2.length == 0) 0 else 2
|
|
|
|
|
else if (b2.length == 0) 1 else 3
|
2011-12-31 09:18:37 -07:00
|
|
|
|
2011-05-27 18:53:18 -06:00
|
|
|
}
|
|
|
|
|
|
2011-12-31 09:18:37 -07:00
|
|
|
final class ByteStrings private (val bytestrings: Vector[ByteString1], val length: Int) extends ByteString {
|
2011-06-05 14:04:18 -06:00
|
|
|
|
|
|
|
|
def apply(idx: Int): Byte =
|
|
|
|
|
if (0 <= idx && idx < length) {
|
|
|
|
|
var pos = 0
|
|
|
|
|
var seen = 0
|
|
|
|
|
while (idx >= seen + bytestrings(pos).length) {
|
|
|
|
|
seen += bytestrings(pos).length
|
|
|
|
|
pos += 1
|
|
|
|
|
}
|
|
|
|
|
bytestrings(pos)(idx - seen)
|
|
|
|
|
} else throw new IndexOutOfBoundsException(idx.toString)
|
|
|
|
|
|
|
|
|
|
override def slice(from: Int, until: Int): ByteString = {
|
|
|
|
|
val start = math.max(from, 0)
|
|
|
|
|
val end = math.min(until, length)
|
|
|
|
|
if (end <= start)
|
|
|
|
|
ByteString.empty
|
|
|
|
|
else {
|
2011-12-31 09:18:37 -07:00
|
|
|
val iter = bytestrings.iterator
|
|
|
|
|
var cur = iter.next
|
2011-06-05 14:04:18 -06:00
|
|
|
var pos = 0
|
|
|
|
|
var seen = 0
|
2011-12-31 09:18:37 -07:00
|
|
|
while (from >= seen + cur.length) {
|
|
|
|
|
seen += cur.length
|
2011-06-05 14:04:18 -06:00
|
|
|
pos += 1
|
2011-12-31 09:18:37 -07:00
|
|
|
cur = iter.next
|
2011-06-05 14:04:18 -06:00
|
|
|
}
|
|
|
|
|
val startpos = pos
|
|
|
|
|
val startidx = start - seen
|
2011-12-31 09:18:37 -07:00
|
|
|
while (until > seen + cur.length) {
|
|
|
|
|
seen += cur.length
|
2011-06-05 14:04:18 -06:00
|
|
|
pos += 1
|
2011-12-31 09:18:37 -07:00
|
|
|
cur = iter.next
|
2011-06-05 14:04:18 -06:00
|
|
|
}
|
|
|
|
|
val endpos = pos
|
|
|
|
|
val endidx = end - seen
|
|
|
|
|
if (startpos == endpos)
|
2011-12-31 09:18:37 -07:00
|
|
|
cur.slice(startidx, endidx)
|
2011-06-05 14:04:18 -06:00
|
|
|
else {
|
|
|
|
|
val first = bytestrings(startpos).drop(startidx).asInstanceOf[ByteString1]
|
2011-12-31 09:18:37 -07:00
|
|
|
val last = cur.take(endidx).asInstanceOf[ByteString1]
|
2011-06-05 14:04:18 -06:00
|
|
|
if ((endpos - startpos) == 1)
|
2011-12-31 09:18:37 -07:00
|
|
|
new ByteStrings(Vector(first, last), until - from)
|
2011-06-05 14:04:18 -06:00
|
|
|
else
|
2011-12-31 09:18:37 -07:00
|
|
|
new ByteStrings(first +: bytestrings.slice(startpos + 1, endpos) :+ last, until - from)
|
2011-06-05 14:04:18 -06:00
|
|
|
}
|
2011-05-27 18:53:18 -06:00
|
|
|
}
|
2011-06-05 14:04:18 -06:00
|
|
|
}
|
2011-05-27 18:53:18 -06:00
|
|
|
|
2011-06-05 14:04:18 -06:00
|
|
|
def ++(that: ByteString): ByteString = that match {
|
|
|
|
|
case b: ByteString1 ⇒ ByteStrings(this, b)
|
|
|
|
|
case bs: ByteStrings ⇒ ByteStrings(this, bs)
|
|
|
|
|
}
|
2011-05-27 18:53:18 -06:00
|
|
|
|
2011-06-05 14:04:18 -06:00
|
|
|
def compact: ByteString = {
|
|
|
|
|
val ar = new Array[Byte](length)
|
2011-05-27 18:53:18 -06:00
|
|
|
var pos = 0
|
2011-06-05 14:04:18 -06:00
|
|
|
bytestrings foreach { b ⇒
|
|
|
|
|
b.copyToArray(ar, pos, b.length)
|
|
|
|
|
pos += b.length
|
2011-05-27 18:53:18 -06:00
|
|
|
}
|
2011-06-05 14:04:18 -06:00
|
|
|
ByteString1(ar)
|
2011-05-27 18:53:18 -06:00
|
|
|
}
|
2011-06-05 14:04:18 -06:00
|
|
|
|
|
|
|
|
def asByteBuffer: ByteBuffer = compact.asByteBuffer
|
|
|
|
|
|
2011-12-31 09:18:37 -07:00
|
|
|
def decodeString(charset: String): String = compact.decodeString(charset)
|
|
|
|
|
|
|
|
|
|
def copyToBuffer(buffer: ByteBuffer): Int = {
|
|
|
|
|
val copyLength = math.min(buffer.remaining, length)
|
|
|
|
|
val iter = bytestrings.iterator
|
|
|
|
|
while (iter.hasNext && buffer.hasRemaining) {
|
|
|
|
|
iter.next.copyToBuffer(buffer)
|
|
|
|
|
}
|
|
|
|
|
copyLength
|
|
|
|
|
}
|
2011-05-27 18:53:18 -06:00
|
|
|
}
|
|
|
|
|
|
2011-06-05 14:04:18 -06:00
|
|
|
}
|
2011-05-27 18:53:18 -06:00
|
|
|
|
2011-06-05 14:04:18 -06:00
|
|
|
sealed trait ByteString extends IndexedSeq[Byte] with IndexedSeqOptimized[Byte, ByteString] {
|
|
|
|
|
override protected[this] def newBuilder = ByteString.newBuilder
|
|
|
|
|
def ++(that: ByteString): ByteString
|
2011-12-31 09:18:37 -07:00
|
|
|
def copyToBuffer(buffer: ByteBuffer): Int
|
2011-06-05 14:04:18 -06:00
|
|
|
def compact: ByteString
|
|
|
|
|
def asByteBuffer: ByteBuffer
|
2011-12-31 09:18:37 -07:00
|
|
|
final def toByteBuffer: ByteBuffer = ByteBuffer.wrap(toArray)
|
|
|
|
|
final def utf8String: String = decodeString("UTF-8")
|
|
|
|
|
def decodeString(charset: String): String
|
|
|
|
|
final def mapI(f: Byte ⇒ Int): ByteString = map(f andThen (_.toByte))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final class ByteStringBuilder extends Builder[Byte, ByteString] {
|
|
|
|
|
import ByteString.{ ByteString1, ByteStrings }
|
|
|
|
|
private var _length = 0
|
|
|
|
|
private val _builder = new VectorBuilder[ByteString1]()
|
|
|
|
|
private var _temp: Array[Byte] = _
|
|
|
|
|
private var _tempLength = 0
|
|
|
|
|
private var _tempCapacity = 0
|
|
|
|
|
|
|
|
|
|
private def clearTemp() {
|
|
|
|
|
if (_tempLength > 0) {
|
|
|
|
|
val arr = new Array[Byte](_tempLength)
|
|
|
|
|
Array.copy(_temp, 0, arr, 0, _tempLength)
|
|
|
|
|
_builder += ByteString1(arr)
|
|
|
|
|
_tempLength = 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private def resizeTemp(size: Int) {
|
|
|
|
|
val newtemp = new Array[Byte](size)
|
|
|
|
|
if (_tempLength > 0) Array.copy(_temp, 0, newtemp, 0, _tempLength)
|
|
|
|
|
_temp = newtemp
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private def ensureTempSize(size: Int) {
|
|
|
|
|
if (_tempCapacity < size || _tempCapacity == 0) {
|
|
|
|
|
var newSize = if (_tempCapacity == 0) 16 else _tempCapacity * 2
|
|
|
|
|
while (newSize < size) newSize *= 2
|
|
|
|
|
resizeTemp(newSize)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def +=(elem: Byte): this.type = {
|
|
|
|
|
ensureTempSize(_tempLength + 1)
|
|
|
|
|
_temp(_tempLength) = elem
|
|
|
|
|
_tempLength += 1
|
|
|
|
|
_length += 1
|
|
|
|
|
this
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def ++=(xs: TraversableOnce[Byte]): this.type = {
|
|
|
|
|
xs match {
|
|
|
|
|
case b: ByteString1 ⇒
|
|
|
|
|
clearTemp()
|
|
|
|
|
_builder += b
|
|
|
|
|
_length += b.length
|
|
|
|
|
case bs: ByteStrings ⇒
|
|
|
|
|
clearTemp()
|
|
|
|
|
_builder ++= bs.bytestrings
|
|
|
|
|
_length += bs.length
|
|
|
|
|
case xs: WrappedArray.ofByte ⇒
|
|
|
|
|
clearTemp()
|
|
|
|
|
_builder += ByteString1(xs.array.clone)
|
|
|
|
|
_length += xs.length
|
|
|
|
|
case _: collection.IndexedSeq[_] ⇒
|
|
|
|
|
ensureTempSize(_tempLength + xs.size)
|
|
|
|
|
xs.copyToArray(_temp, _tempLength)
|
|
|
|
|
case _ ⇒
|
|
|
|
|
super.++=(xs)
|
|
|
|
|
}
|
|
|
|
|
this
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def clear() {
|
|
|
|
|
_builder.clear
|
|
|
|
|
_length = 0
|
|
|
|
|
_tempLength = 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def result: ByteString =
|
|
|
|
|
if (_length == 0) ByteString.empty
|
|
|
|
|
else {
|
|
|
|
|
clearTemp()
|
|
|
|
|
val bytestrings = _builder.result
|
|
|
|
|
if (bytestrings.size == 1)
|
|
|
|
|
bytestrings.head
|
|
|
|
|
else
|
|
|
|
|
ByteStrings(bytestrings, _length)
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-27 18:53:18 -06:00
|
|
|
}
|