Prefer use of charset over charset name (#22679)

* Use Charset instead of charset name

When using the reference to the charset directly, j.l.StringCoding will
run without doing a charset by name lookup.

* Use Charset instead of charset name

Add a method taking charset instance for every method with charset name.

* Use Charset instead of charset name

Rolled back change of parameter name as that would not be fully API compatible.
This commit is contained in:
Enno 2017-04-21 13:56:36 +02:00 committed by Patrik Nordwall
parent c8ff39e6c2
commit 4ddc10ced0

View file

@ -43,13 +43,18 @@ object ByteString {
/**
* Creates a new ByteString by encoding a String as UTF-8.
*/
def apply(string: String): ByteString = apply(string, UTF_8)
def apply(string: String): ByteString = apply(string, StandardCharsets.UTF_8)
/**
* Creates a new ByteString by encoding a String with a charset.
*/
def apply(string: String, charset: String): ByteString = CompactByteString(string, charset)
/**
* Creates a new ByteString by encoding a String with a charset.
*/
def apply(string: String, charset: Charset): ByteString = CompactByteString(string, charset)
/**
* Creates a new ByteString by copying a byte array.
*/
@ -80,6 +85,11 @@ object ByteString {
*/
def fromString(string: String, charset: String): ByteString = apply(string, charset)
/**
* Creates a new ByteString which will contain the representation of the given String in the given charset
*/
def fromString(string: String, charset: Charset): ByteString = apply(string, charset)
/**
* Standard "UTF-8" charset
*/
@ -459,8 +469,7 @@ object ByteString {
def decodeString(charset: String): String = compact.decodeString(charset)
def decodeString(charset: Charset): String =
compact.decodeString(charset)
def decodeString(charset: Charset): String = compact.decodeString(charset)
private[akka] def writeToOutputStream(os: ObjectOutputStream): Unit = {
os.writeInt(bytestrings.length)
@ -746,7 +755,7 @@ sealed abstract class ByteString extends IndexedSeq[Byte] with IndexedSeqOptimiz
/**
* Decodes this ByteString as a UTF-8 encoded String.
*/
final def utf8String: String = decodeString(ByteString.UTF_8)
final def utf8String: String = decodeString(StandardCharsets.UTF_8)
/**
* Decodes this ByteString using a charset to produce a String.
@ -808,7 +817,7 @@ object CompactByteString {
/**
* Creates a new CompactByteString by encoding a String as UTF-8.
*/
def apply(string: String): CompactByteString = apply(string, ByteString.UTF_8)
def apply(string: String): CompactByteString = apply(string, StandardCharsets.UTF_8)
/**
* Creates a new CompactByteString by encoding a String with a charset.
@ -816,6 +825,12 @@ object CompactByteString {
def apply(string: String, charset: String): CompactByteString =
if (string.isEmpty) empty else ByteString.ByteString1C(string.getBytes(charset))
/**
* Creates a new CompactByteString by encoding a String with a charset.
*/
def apply(string: String, charset: Charset): CompactByteString =
if (string.isEmpty) empty else ByteString.ByteString1C(string.getBytes(charset))
/**
* Creates a new CompactByteString by copying length bytes starting at offset from
* an Array.