Skip to content

Commit

Permalink
util-core: Enable ByteReader to Read Unsigned Longs
Browse files Browse the repository at this point in the history
Problem

readUnsignedLongBE and readUnsignedLongLE are missing in ByteReader
implementation.

Solution

Added readUnsignedLongBE and readUnsignedLongLE to ByteReader and
implemented in ByteReaderImpl.

Signed-off-by: Ryan O'Neill <ryano@twitter.com>

RB_ID=917289
  • Loading branch information
mehmetgunturkun authored and jenkins committed May 24, 2017
1 parent 9b0ebc1 commit e62c326
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,42 @@ private[netty4] abstract class AbstractByteBufByteReader(bb: ByteBuf) extends By
bb.readLongLE()
}

final def readUnsignedLongBE(): BigInt = {
checkRemaining(8)

// 9 (8+1) so sign bit is always positive
val bytes: Array[Byte] = new Array[Byte](9)
bytes(0) = 0
bytes(1) = bb.readByte()
bytes(2) = bb.readByte()
bytes(3) = bb.readByte()
bytes(4) = bb.readByte()
bytes(5) = bb.readByte()
bytes(6) = bb.readByte()
bytes(7) = bb.readByte()
bytes(8) = bb.readByte()

BigInt(bytes)
}

final def readUnsignedLongLE(): BigInt = {
checkRemaining(8)

// 9 (8+1) so sign bit is always positive
val bytes: Array[Byte] = new Array[Byte](9)
bytes(8) = bb.readByte()
bytes(7) = bb.readByte()
bytes(6) = bb.readByte()
bytes(5) = bb.readByte()
bytes(4) = bb.readByte()
bytes(3) = bb.readByte()
bytes(2) = bb.readByte()
bytes(1) = bb.readByte()
bytes(0) = 0

BigInt(bytes)
}

final def readFloatBE(): Float = {
JFloat.intBitsToFloat(readIntBE())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.twitter.io.ByteReader.UnderflowException
import com.twitter.io.{Buf, ByteReader}
import io.netty.buffer.{ByteBuf, UnpooledByteBufAllocator}
import java.lang.{Double => JDouble, Float => JFloat}
import org.scalacheck.Gen
import org.scalatest.FunSuite
import org.scalatest.prop.GeneratorDrivenPropertyChecks

Expand Down Expand Up @@ -184,6 +185,38 @@ abstract class AbstractByteBufByteReaderTest extends FunSuite with GeneratorDriv
assert(br.readUnsignedIntLE() == (i & 0xffffffffl))
})

val uInt64s: Gen[BigInt] = Gen.chooseNum(Long.MinValue, Long.MaxValue)
.map(x => BigInt(x) + BigInt(2).pow(63))

test("readUnsignedLongBE") (forAll(uInt64s) { bi: BigInt =>
val br = readerWith(
((bi >> 56) & 0xff).toByte,
((bi >> 48) & 0xff).toByte,
((bi >> 40) & 0xff).toByte,
((bi >> 32) & 0xff).toByte,
((bi >> 24) & 0xff).toByte,
((bi >> 16) & 0xff).toByte,
((bi >> 8) & 0xff).toByte,
((bi ) & 0xff).toByte)
assert(br.readUnsignedLongBE() == bi)
val exc = intercept[UnderflowException] { br.readByte() }
})

test("readUnsignedLongLE") (forAll(uInt64s) { bi1: BigInt =>
val bi = bi1.abs
val br = readerWith(
((bi ) & 0xff).toByte,
((bi >> 8) & 0xff).toByte,
((bi >> 16) & 0xff).toByte,
((bi >> 24) & 0xff).toByte,
((bi >> 32) & 0xff).toByte,
((bi >> 40) & 0xff).toByte,
((bi >> 48) & 0xff).toByte,
((bi >> 56) & 0xff).toByte)
assert(br.readUnsignedLongLE() == bi)
val exc = intercept[UnderflowException] { br.readByte() }
})

// .equals is required to handle NaN
test("readFloatBE") (forAll { i: Int =>
val br = newReader(_.writeInt(i))
Expand Down

0 comments on commit e62c326

Please sign in to comment.