add zero-copy constructor to create ByteString from external array

This commit is contained in:
Hawstein 2017-07-20 23:34:33 +08:00 committed by Konrad `ktoso` Malawski
parent c60d20af32
commit 4684e19828

View file

@ -60,6 +60,16 @@ object ByteString {
*/
def fromArray(array: Array[Byte]): ByteString = apply(array)
/**
* Creates a ByteString without allocation.
*
* Use with caution:
* Since the ByteString is created without copying the byte array,
* this makes it unsafe if an array is passed in and mutated afterwards.
* DO NOT USE if you are not sure whether the passed-in array will be changed outside this method.
*/
def fromArrayUnsafe(array: Array[Byte]): ByteString = ByteString1C(array)
/**
* Creates a new ByteString by copying length bytes starting at offset from
* an Array.
@ -67,6 +77,16 @@ object ByteString {
def fromArray(array: Array[Byte], offset: Int, length: Int): ByteString =
CompactByteString.fromArray(array, offset, length)
/**
* Creates a ByteString without allocation, using length bytes starting at offset from an Array.
*
* Use with caution:
* Since the ByteString is created without copying the byte array,
* this makes it unsafe if an array is passed in and mutated afterwards.
* DO NOT USE if you are not sure whether the passed-in array will be changed outside this method.
*/
def fromArrayUnsafe(array: Array[Byte], offset: Int, length: Int): ByteString = ByteString1(array, offset, length)
/**
* JAVA API
* Creates a new ByteString by copying an int array by converting from integral numbers to bytes.