diff --git a/akka-actor-tests/src/test/scala/akka/util/ByteStringSpec.scala b/akka-actor-tests/src/test/scala/akka/util/ByteStringSpec.scala index 78d482c3cf..bf17da3164 100644 --- a/akka-actor-tests/src/test/scala/akka/util/ByteStringSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/util/ByteStringSpec.scala @@ -80,6 +80,8 @@ class ByteStringSpec extends WordSpec with Matchers with Checkers { } yield (xs.slice(from, until), bytes) } + implicit val arbitraryByteStringBuilder: Arbitrary[ByteStringBuilder] = Arbitrary(ByteString.newBuilder) + def likeVector(bs: ByteString)(body: IndexedSeq[Byte] ⇒ Any): Boolean = { val vec = Vector(bs: _*) body(bs) == body(vec) @@ -552,5 +554,17 @@ class ByteStringSpec extends WordSpec with Matchers with Checkers { "encoding Double in big-endian" in { check { slice: ArraySlice[Double] ⇒ testDoubleEncoding(slice, BIG_ENDIAN) } } "encoding Double in little-endian" in { check { slice: ArraySlice[Double] ⇒ testDoubleEncoding(slice, LITTLE_ENDIAN) } } } + + "have correct empty info" when { + "is empty" in { + check { a: ByteStringBuilder ⇒ a.isEmpty } + } + "is nonEmpty" in { + check { a: ByteStringBuilder ⇒ + a.putByte(1.toByte) + a.nonEmpty + } + } + } } } diff --git a/akka-actor/src/main/scala/akka/util/ByteString.scala b/akka-actor/src/main/scala/akka/util/ByteString.scala index 5a1bbb3e74..832f6d272b 100644 --- a/akka-actor/src/main/scala/akka/util/ByteString.scala +++ b/akka-actor/src/main/scala/akka/util/ByteString.scala @@ -798,4 +798,14 @@ final class ByteStringBuilder extends Builder[Byte, ByteString] { override def write(b: Array[Byte], off: Int, len: Int): Unit = { builder.putBytes(b, off, len) } } + + /** + * Tests whether this ByteStringBuilder is empty. + */ + def isEmpty: Boolean = _length == 0 + + /** + * Tests whether this ByteStringBuilder is not empty. + */ + def nonEmpty: Boolean = _length > 0 }