final case class Bytes(array: Array[Byte]) extends Serializable with Product
A wrapper for Array[Byte]
that provides sane implementations of hashCode
, equals
, and toString
. The
wrapped array of bytes is assumed to be never modified.
Note: Unfortunately we cannot make Bytes a value class because a value class may not override the
hashCode
and equals
methods (cf. SIP-15, criterion 4).
Alternatives
Instead of wrapping an Array[Byte]
with this class you can also convert an Array[Byte]
to a Seq[Byte]
via Scala's toSeq
method:
val arrayByte: Array[Byte] = Array(1.toByte) val seqByte: Seq[Byte] = arrayByte.toSeq
Like Bytes, a Seq[Byte]
has sane hashCode
, equals
, and toString
implementations.
Performance-wise we found that a Seq[Byte]
is comparable to Bytes. For example, a CMS[Seq[Byte]]
was measured to be only slightly slower than CMS[Bytes]
(think: single-digit percentages).
- array
the wrapped array of bytes
- See also
- Alphabetic
- By Inheritance
- Bytes
- Product
- Equals
- Serializable
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Instance Constructors
- new Bytes(array: Array[Byte])
- array
the wrapped array of bytes
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- def apply(idx: Int): Byte
- val array: Array[Byte]
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(that: Any): Boolean
Implementation detail: This
equals
method is defined in terms of the wrapped array, which is a mutable field.Implementation detail: This
equals
method is defined in terms of the wrapped array, which is a mutable field. In general such a definition ofequals
is considered bad practice, but in our case we justify the use of a mutable field because the contract of Bytes requires that the wrapped array must never be modified (and we intentionally do not create a defensive, immutable copy because of performance considerations).- Definition Classes
- Bytes → Equals → AnyRef → Any
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def hashCode(): Int
- Definition Classes
- Bytes → AnyRef → Any
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- def productElementNames: Iterator[String]
- Definition Classes
- Product
- def size: Int
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- Bytes → AnyRef → Any
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()