You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pekko.apache.org by fa...@apache.org on 2025/06/20 08:31:01 UTC
(pekko-http) branch main updated: avoid unsafe in StringTools (#690)
This is an automated email from the ASF dual-hosted git repository.
fanningpj pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/pekko-http.git
The following commit(s) were added to refs/heads/main by this push:
new 7b82c0263 avoid unsafe in StringTools (#690)
7b82c0263 is described below
commit 7b82c0263a714c87478d4bd8d29205665ff6eb02
Author: PJ Fanning <pj...@users.noreply.github.com>
AuthorDate: Fri Jun 20 09:30:56 2025 +0100
avoid unsafe in StringTools (#690)
* avoid unsafe in StringTools
* Update StringTools.scala
---
.../org/apache/pekko/http/impl/util/StringTools.scala | 17 +++++++++++++----
1 file changed, 13 insertions(+), 4 deletions(-)
diff --git a/http-core/src/main/scala/org/apache/pekko/http/impl/util/StringTools.scala b/http-core/src/main/scala/org/apache/pekko/http/impl/util/StringTools.scala
index a1abd34db..1d371107d 100644
--- a/http-core/src/main/scala/org/apache/pekko/http/impl/util/StringTools.scala
+++ b/http-core/src/main/scala/org/apache/pekko/http/impl/util/StringTools.scala
@@ -15,7 +15,8 @@ package org.apache.pekko.http.impl.util
import org.apache.pekko
import pekko.annotation.InternalApi
-import pekko.util.Unsafe
+import pekko.util.{ JavaVersion, Unsafe }
+
import scala.annotation.nowarn
/**
@@ -23,6 +24,8 @@ import scala.annotation.nowarn
*/
@InternalApi
private[http] object StringTools {
+ private val avoidUnsafe = JavaVersion.majorVersion >= 17
+
@nowarn("msg=deprecated")
def asciiStringFromBytes(bytes: Array[Byte]): String =
// Deprecated constructor but also (unfortunately) the fastest way to convert a ASCII encoded byte array
@@ -30,8 +33,14 @@ private[http] object StringTools {
new String(bytes, 0)
def asciiStringBytes(string: String): Array[Byte] = {
- val bytes = new Array[Byte](string.length)
- Unsafe.copyUSAsciiStrToBytes(string, bytes)
- bytes
+ if (avoidUnsafe) {
+ // this is as fast as Unsafe.copyUSAsciiStrToBytes for recent JDK versions
+ // and avoids the use of deprecated Unsafe methods
+ string.getBytes(java.nio.charset.StandardCharsets.US_ASCII)
+ } else {
+ val bytes = new Array[Byte](string.length)
+ Unsafe.copyUSAsciiStrToBytes(string, bytes)
+ bytes
+ }
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pekko.apache.org
For additional commands, e-mail: commits-help@pekko.apache.org