Moved ByteIterator implementations into object ByteIterator
This commit is contained in:
parent
7d3edcddbc
commit
0c76e0f0f6
2 changed files with 318 additions and 318 deletions
|
|
@ -36,238 +36,6 @@ object ByteIterator {
|
|||
nSkip
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
abstract class ByteIterator extends BufferedIterator[Byte] {
|
||||
def isIdenticalTo(that: Iterator[Byte]): Boolean
|
||||
|
||||
def len: Int
|
||||
|
||||
def head: Byte
|
||||
|
||||
def next(): Byte
|
||||
|
||||
protected def clear(): Unit
|
||||
|
||||
def ++(that: TraversableOnce[Byte]): ByteIterator = {
|
||||
if (that.isEmpty) this
|
||||
else ByteArrayIterator(that.toArray)
|
||||
}
|
||||
|
||||
// *must* be overridden by derived classes
|
||||
override def clone: ByteIterator = null
|
||||
|
||||
final override def duplicate = (this, clone)
|
||||
|
||||
// *must* be overridden by derived classes
|
||||
override def take(n: Int): this.type = null
|
||||
|
||||
// *must* be overridden by derived classes
|
||||
override def drop(n: Int): this.type = null
|
||||
|
||||
final override def slice(from: Int, until: Int): this.type =
|
||||
drop(from).take(until - from)
|
||||
|
||||
// *must* be overridden by derived classes
|
||||
override def takeWhile(p: Byte ⇒ Boolean): this.type = null
|
||||
|
||||
// *must* be overridden by derived classes
|
||||
override def dropWhile(p: Byte ⇒ Boolean): this.type = null
|
||||
|
||||
final override def span(p: Byte ⇒ Boolean): (ByteIterator, ByteIterator) = {
|
||||
val that = clone
|
||||
that.takeWhile(p)
|
||||
drop(that.len)
|
||||
(that, this)
|
||||
}
|
||||
|
||||
final override def indexWhere(p: Byte ⇒ Boolean): Int = {
|
||||
var index = 0
|
||||
var found = false
|
||||
while (!found && hasNext) if (p(next())) { found = true } else { index += 1 }
|
||||
if (found) index else -1
|
||||
}
|
||||
|
||||
final def indexOf(elem: Byte): Int = {
|
||||
var index = 0
|
||||
var found = false
|
||||
while (!found && hasNext) if (elem == next()) { found = true } else { index += 1 }
|
||||
if (found) index else -1
|
||||
}
|
||||
|
||||
final override def indexOf[B >: Byte](elem: B): Int = {
|
||||
var index = 0
|
||||
var found = false
|
||||
while (!found && hasNext) if (elem == next()) { found = true } else { index += 1 }
|
||||
if (found) index else -1
|
||||
}
|
||||
|
||||
def toByteString: ByteString
|
||||
|
||||
override def toSeq: ByteString = toByteString
|
||||
|
||||
@inline final override def foreach[@specialized U](f: Byte ⇒ U): Unit =
|
||||
while (hasNext) f(next())
|
||||
|
||||
final override def foldLeft[@specialized B](z: B)(op: (B, Byte) ⇒ B): B = {
|
||||
var acc = z
|
||||
while (hasNext) acc = op(acc, next())
|
||||
acc
|
||||
}
|
||||
|
||||
final override def toArray[B >: Byte](implicit arg0: ClassManifest[B]): Array[B] = {
|
||||
val target = Array.ofDim[B](len)
|
||||
copyToArray(target)
|
||||
target
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single Byte from this iterator. Identical to next().
|
||||
*/
|
||||
def getByte = next()
|
||||
|
||||
/**
|
||||
* Get a single Short from this iterator.
|
||||
*/
|
||||
def getShort(implicit byteOrder: ByteOrder): Short = {
|
||||
if (byteOrder == ByteOrder.BIG_ENDIAN)
|
||||
((next() & 0xff) << 8 | (next() & 0xff) << 0).toShort
|
||||
else if (byteOrder == ByteOrder.LITTLE_ENDIAN)
|
||||
((next() & 0xff) << 0 | (next() & 0xff) << 8).toShort
|
||||
else throw new IllegalArgumentException("Unknown byte order " + byteOrder)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single Int from this iterator.
|
||||
*/
|
||||
def getInt(implicit byteOrder: ByteOrder): Int = {
|
||||
if (byteOrder == ByteOrder.BIG_ENDIAN)
|
||||
((next() & 0xff) << 24
|
||||
| (next() & 0xff) << 16
|
||||
| (next() & 0xff) << 8
|
||||
| (next() & 0xff) << 0)
|
||||
else if (byteOrder == ByteOrder.LITTLE_ENDIAN)
|
||||
((next() & 0xff) << 0
|
||||
| (next() & 0xff) << 8
|
||||
| (next() & 0xff) << 16
|
||||
| (next() & 0xff) << 24)
|
||||
else throw new IllegalArgumentException("Unknown byte order " + byteOrder)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single Long from this iterator.
|
||||
*/
|
||||
def getLong(implicit byteOrder: ByteOrder): Long = {
|
||||
if (byteOrder == ByteOrder.BIG_ENDIAN)
|
||||
((next().toLong & 0xff) << 56
|
||||
| (next().toLong & 0xff) << 48
|
||||
| (next().toLong & 0xff) << 40
|
||||
| (next().toLong & 0xff) << 32
|
||||
| (next().toLong & 0xff) << 24
|
||||
| (next().toLong & 0xff) << 16
|
||||
| (next().toLong & 0xff) << 8
|
||||
| (next().toLong & 0xff) << 0)
|
||||
else if (byteOrder == ByteOrder.LITTLE_ENDIAN)
|
||||
((next().toLong & 0xff) << 0
|
||||
| (next().toLong & 0xff) << 8
|
||||
| (next().toLong & 0xff) << 16
|
||||
| (next().toLong & 0xff) << 24
|
||||
| (next().toLong & 0xff) << 32
|
||||
| (next().toLong & 0xff) << 40
|
||||
| (next().toLong & 0xff) << 48
|
||||
| (next().toLong & 0xff) << 56)
|
||||
else throw new IllegalArgumentException("Unknown byte order " + byteOrder)
|
||||
}
|
||||
|
||||
def getFloat(implicit byteOrder: ByteOrder): Float =
|
||||
java.lang.Float.intBitsToFloat(getInt(byteOrder))
|
||||
|
||||
def getDouble(implicit byteOrder: ByteOrder): Double =
|
||||
java.lang.Double.longBitsToDouble(getLong(byteOrder))
|
||||
|
||||
/**
|
||||
* Get a specific number of Bytes from this iterator. In contrast to
|
||||
* copyToArray, this method will fail if this.len < xs.length.
|
||||
*/
|
||||
def getBytes(xs: Array[Byte]): this.type = getBytes(xs, 0, xs.length)
|
||||
|
||||
/**
|
||||
* Get a specific number of Bytes from this iterator. In contrast to
|
||||
* copyToArray, this method will fail if length < n or if (xs.length - offset) < n.
|
||||
*/
|
||||
def getBytes(xs: Array[Byte], offset: Int, n: Int): this.type
|
||||
|
||||
/**
|
||||
* Get a number of Shorts from this iterator.
|
||||
*/
|
||||
def getShorts(xs: Array[Short])(implicit byteOrder: ByteOrder): this.type =
|
||||
getShorts(xs, 0, xs.length)(byteOrder)
|
||||
|
||||
/**
|
||||
* Get a number of Shorts from this iterator.
|
||||
*/
|
||||
def getShorts(xs: Array[Short], offset: Int, n: Int)(implicit byteOrder: ByteOrder): this.type
|
||||
|
||||
/**
|
||||
* Get a number of Ints from this iterator.
|
||||
*/
|
||||
def getInts(xs: Array[Int])(implicit byteOrder: ByteOrder): this.type =
|
||||
getInts(xs, 0, xs.length)(byteOrder)
|
||||
|
||||
/**
|
||||
* Get a number of Ints from this iterator.
|
||||
*/
|
||||
def getInts(xs: Array[Int], offset: Int, n: Int)(implicit byteOrder: ByteOrder): this.type
|
||||
|
||||
/**
|
||||
* Get a number of Longs from this iterator.
|
||||
*/
|
||||
def getLongs(xs: Array[Long])(implicit byteOrder: ByteOrder): this.type =
|
||||
getLongs(xs, 0, xs.length)(byteOrder)
|
||||
|
||||
/**
|
||||
* Get a number of Longs from this iterator.
|
||||
*/
|
||||
def getLongs(xs: Array[Long], offset: Int, n: Int)(implicit byteOrder: ByteOrder): this.type
|
||||
|
||||
/**
|
||||
* Get a number of Floats from this iterator.
|
||||
*/
|
||||
def getFloats(xs: Array[Float])(implicit byteOrder: ByteOrder): this.type =
|
||||
getFloats(xs, 0, xs.length)(byteOrder)
|
||||
|
||||
/**
|
||||
* Get a number of Floats from this iterator.
|
||||
*/
|
||||
def getFloats(xs: Array[Float], offset: Int, n: Int)(implicit byteOrder: ByteOrder): this.type
|
||||
|
||||
/**
|
||||
* Get a number of Doubles from this iterator.
|
||||
*/
|
||||
def getDoubles(xs: Array[Double])(implicit byteOrder: ByteOrder): this.type =
|
||||
getDoubles(xs, 0, xs.length)(byteOrder)
|
||||
|
||||
/**
|
||||
* Get a number of Doubles from this iterator.
|
||||
*/
|
||||
def getDoubles(xs: Array[Double], offset: Int, n: Int)(implicit byteOrder: ByteOrder): this.type
|
||||
|
||||
/**
|
||||
* Copy as many bytes as possible to a ByteBuffer, starting from it's
|
||||
* current position. This method will not overflow the buffer.
|
||||
*
|
||||
* @param buffer a ByteBuffer to copy bytes to
|
||||
* @return the number of bytes actually copied
|
||||
*/
|
||||
def copyToBuffer(buffer: ByteBuffer): Int
|
||||
|
||||
/**
|
||||
* Directly wraps this ByteIterator in an InputStream without copying.
|
||||
* Read and skip operations on the stream will advance the iterator
|
||||
* accordingly.
|
||||
*/
|
||||
def asInputStream: java.io.InputStream = new ByteIterator.InputStreamWrapper(this)
|
||||
}
|
||||
|
||||
object ByteArrayIterator {
|
||||
private val emptyArray = Array.ofDim[Byte](0)
|
||||
|
|
@ -582,3 +350,235 @@ class MultiByteArrayIterator private (private var iterators: List[ByteArrayItera
|
|||
n
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
abstract class ByteIterator extends BufferedIterator[Byte] {
|
||||
def isIdenticalTo(that: Iterator[Byte]): Boolean
|
||||
|
||||
def len: Int
|
||||
|
||||
def head: Byte
|
||||
|
||||
def next(): Byte
|
||||
|
||||
protected def clear(): Unit
|
||||
|
||||
def ++(that: TraversableOnce[Byte]): ByteIterator = {
|
||||
if (that.isEmpty) this
|
||||
else ByteIterator.ByteArrayIterator(that.toArray)
|
||||
}
|
||||
|
||||
// *must* be overridden by derived classes
|
||||
override def clone: ByteIterator = null
|
||||
|
||||
final override def duplicate = (this, clone)
|
||||
|
||||
// *must* be overridden by derived classes
|
||||
override def take(n: Int): this.type = null
|
||||
|
||||
// *must* be overridden by derived classes
|
||||
override def drop(n: Int): this.type = null
|
||||
|
||||
final override def slice(from: Int, until: Int): this.type =
|
||||
drop(from).take(until - from)
|
||||
|
||||
// *must* be overridden by derived classes
|
||||
override def takeWhile(p: Byte ⇒ Boolean): this.type = null
|
||||
|
||||
// *must* be overridden by derived classes
|
||||
override def dropWhile(p: Byte ⇒ Boolean): this.type = null
|
||||
|
||||
final override def span(p: Byte ⇒ Boolean): (ByteIterator, ByteIterator) = {
|
||||
val that = clone
|
||||
that.takeWhile(p)
|
||||
drop(that.len)
|
||||
(that, this)
|
||||
}
|
||||
|
||||
final override def indexWhere(p: Byte ⇒ Boolean): Int = {
|
||||
var index = 0
|
||||
var found = false
|
||||
while (!found && hasNext) if (p(next())) { found = true } else { index += 1 }
|
||||
if (found) index else -1
|
||||
}
|
||||
|
||||
final def indexOf(elem: Byte): Int = {
|
||||
var index = 0
|
||||
var found = false
|
||||
while (!found && hasNext) if (elem == next()) { found = true } else { index += 1 }
|
||||
if (found) index else -1
|
||||
}
|
||||
|
||||
final override def indexOf[B >: Byte](elem: B): Int = {
|
||||
var index = 0
|
||||
var found = false
|
||||
while (!found && hasNext) if (elem == next()) { found = true } else { index += 1 }
|
||||
if (found) index else -1
|
||||
}
|
||||
|
||||
def toByteString: ByteString
|
||||
|
||||
override def toSeq: ByteString = toByteString
|
||||
|
||||
@inline final override def foreach[@specialized U](f: Byte ⇒ U): Unit =
|
||||
while (hasNext) f(next())
|
||||
|
||||
final override def foldLeft[@specialized B](z: B)(op: (B, Byte) ⇒ B): B = {
|
||||
var acc = z
|
||||
while (hasNext) acc = op(acc, next())
|
||||
acc
|
||||
}
|
||||
|
||||
final override def toArray[B >: Byte](implicit arg0: ClassManifest[B]): Array[B] = {
|
||||
val target = Array.ofDim[B](len)
|
||||
copyToArray(target)
|
||||
target
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single Byte from this iterator. Identical to next().
|
||||
*/
|
||||
def getByte = next()
|
||||
|
||||
/**
|
||||
* Get a single Short from this iterator.
|
||||
*/
|
||||
def getShort(implicit byteOrder: ByteOrder): Short = {
|
||||
if (byteOrder == ByteOrder.BIG_ENDIAN)
|
||||
((next() & 0xff) << 8 | (next() & 0xff) << 0).toShort
|
||||
else if (byteOrder == ByteOrder.LITTLE_ENDIAN)
|
||||
((next() & 0xff) << 0 | (next() & 0xff) << 8).toShort
|
||||
else throw new IllegalArgumentException("Unknown byte order " + byteOrder)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single Int from this iterator.
|
||||
*/
|
||||
def getInt(implicit byteOrder: ByteOrder): Int = {
|
||||
if (byteOrder == ByteOrder.BIG_ENDIAN)
|
||||
((next() & 0xff) << 24
|
||||
| (next() & 0xff) << 16
|
||||
| (next() & 0xff) << 8
|
||||
| (next() & 0xff) << 0)
|
||||
else if (byteOrder == ByteOrder.LITTLE_ENDIAN)
|
||||
((next() & 0xff) << 0
|
||||
| (next() & 0xff) << 8
|
||||
| (next() & 0xff) << 16
|
||||
| (next() & 0xff) << 24)
|
||||
else throw new IllegalArgumentException("Unknown byte order " + byteOrder)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single Long from this iterator.
|
||||
*/
|
||||
def getLong(implicit byteOrder: ByteOrder): Long = {
|
||||
if (byteOrder == ByteOrder.BIG_ENDIAN)
|
||||
((next().toLong & 0xff) << 56
|
||||
| (next().toLong & 0xff) << 48
|
||||
| (next().toLong & 0xff) << 40
|
||||
| (next().toLong & 0xff) << 32
|
||||
| (next().toLong & 0xff) << 24
|
||||
| (next().toLong & 0xff) << 16
|
||||
| (next().toLong & 0xff) << 8
|
||||
| (next().toLong & 0xff) << 0)
|
||||
else if (byteOrder == ByteOrder.LITTLE_ENDIAN)
|
||||
((next().toLong & 0xff) << 0
|
||||
| (next().toLong & 0xff) << 8
|
||||
| (next().toLong & 0xff) << 16
|
||||
| (next().toLong & 0xff) << 24
|
||||
| (next().toLong & 0xff) << 32
|
||||
| (next().toLong & 0xff) << 40
|
||||
| (next().toLong & 0xff) << 48
|
||||
| (next().toLong & 0xff) << 56)
|
||||
else throw new IllegalArgumentException("Unknown byte order " + byteOrder)
|
||||
}
|
||||
|
||||
def getFloat(implicit byteOrder: ByteOrder): Float =
|
||||
java.lang.Float.intBitsToFloat(getInt(byteOrder))
|
||||
|
||||
def getDouble(implicit byteOrder: ByteOrder): Double =
|
||||
java.lang.Double.longBitsToDouble(getLong(byteOrder))
|
||||
|
||||
/**
|
||||
* Get a specific number of Bytes from this iterator. In contrast to
|
||||
* copyToArray, this method will fail if this.len < xs.length.
|
||||
*/
|
||||
def getBytes(xs: Array[Byte]): this.type = getBytes(xs, 0, xs.length)
|
||||
|
||||
/**
|
||||
* Get a specific number of Bytes from this iterator. In contrast to
|
||||
* copyToArray, this method will fail if length < n or if (xs.length - offset) < n.
|
||||
*/
|
||||
def getBytes(xs: Array[Byte], offset: Int, n: Int): this.type
|
||||
|
||||
/**
|
||||
* Get a number of Shorts from this iterator.
|
||||
*/
|
||||
def getShorts(xs: Array[Short])(implicit byteOrder: ByteOrder): this.type =
|
||||
getShorts(xs, 0, xs.length)(byteOrder)
|
||||
|
||||
/**
|
||||
* Get a number of Shorts from this iterator.
|
||||
*/
|
||||
def getShorts(xs: Array[Short], offset: Int, n: Int)(implicit byteOrder: ByteOrder): this.type
|
||||
|
||||
/**
|
||||
* Get a number of Ints from this iterator.
|
||||
*/
|
||||
def getInts(xs: Array[Int])(implicit byteOrder: ByteOrder): this.type =
|
||||
getInts(xs, 0, xs.length)(byteOrder)
|
||||
|
||||
/**
|
||||
* Get a number of Ints from this iterator.
|
||||
*/
|
||||
def getInts(xs: Array[Int], offset: Int, n: Int)(implicit byteOrder: ByteOrder): this.type
|
||||
|
||||
/**
|
||||
* Get a number of Longs from this iterator.
|
||||
*/
|
||||
def getLongs(xs: Array[Long])(implicit byteOrder: ByteOrder): this.type =
|
||||
getLongs(xs, 0, xs.length)(byteOrder)
|
||||
|
||||
/**
|
||||
* Get a number of Longs from this iterator.
|
||||
*/
|
||||
def getLongs(xs: Array[Long], offset: Int, n: Int)(implicit byteOrder: ByteOrder): this.type
|
||||
|
||||
/**
|
||||
* Get a number of Floats from this iterator.
|
||||
*/
|
||||
def getFloats(xs: Array[Float])(implicit byteOrder: ByteOrder): this.type =
|
||||
getFloats(xs, 0, xs.length)(byteOrder)
|
||||
|
||||
/**
|
||||
* Get a number of Floats from this iterator.
|
||||
*/
|
||||
def getFloats(xs: Array[Float], offset: Int, n: Int)(implicit byteOrder: ByteOrder): this.type
|
||||
|
||||
/**
|
||||
* Get a number of Doubles from this iterator.
|
||||
*/
|
||||
def getDoubles(xs: Array[Double])(implicit byteOrder: ByteOrder): this.type =
|
||||
getDoubles(xs, 0, xs.length)(byteOrder)
|
||||
|
||||
/**
|
||||
* Get a number of Doubles from this iterator.
|
||||
*/
|
||||
def getDoubles(xs: Array[Double], offset: Int, n: Int)(implicit byteOrder: ByteOrder): this.type
|
||||
|
||||
/**
|
||||
* Copy as many bytes as possible to a ByteBuffer, starting from it's
|
||||
* current position. This method will not overflow the buffer.
|
||||
*
|
||||
* @param buffer a ByteBuffer to copy bytes to
|
||||
* @return the number of bytes actually copied
|
||||
*/
|
||||
def copyToBuffer(buffer: ByteBuffer): Int
|
||||
|
||||
/**
|
||||
* Directly wraps this ByteIterator in an InputStream without copying.
|
||||
* Read and skip operations on the stream will advance the iterator
|
||||
* accordingly.
|
||||
*/
|
||||
def asInputStream: java.io.InputStream = new ByteIterator.InputStreamWrapper(this)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -73,7 +73,7 @@ object ByteString {
|
|||
|
||||
override def length = bytes.length
|
||||
|
||||
override def iterator = ByteArrayIterator(bytes, 0, bytes.length)
|
||||
override def iterator = ByteIterator.ByteArrayIterator(bytes, 0, bytes.length)
|
||||
|
||||
def toByteString1: ByteString1 = ByteString1(bytes)
|
||||
|
||||
|
|
@ -108,7 +108,7 @@ object ByteString {
|
|||
|
||||
def apply(idx: Int): Byte = bytes(checkRangeConvert(idx))
|
||||
|
||||
override def iterator = ByteArrayIterator(bytes, startIndex, startIndex + length)
|
||||
override def iterator = ByteIterator.ByteArrayIterator(bytes, startIndex, startIndex + length)
|
||||
|
||||
private def checkRangeConvert(index: Int) = {
|
||||
if (0 <= index && length > index)
|
||||
|
|
@ -206,7 +206,7 @@ object ByteString {
|
|||
bytestrings(pos)(idx - seen)
|
||||
} else throw new IndexOutOfBoundsException(idx.toString)
|
||||
|
||||
override def iterator = MultiByteArrayIterator(bytestrings.map(_.iterator)(collection.breakOut))
|
||||
override def iterator = ByteIterator.MultiByteArrayIterator(bytestrings.map(_.iterator)(collection.breakOut))
|
||||
|
||||
def ++(that: ByteString): ByteString = {
|
||||
if (that.isEmpty) this
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue