use more of StandardCharsets (#952)

* use more of StandardCharsets

* scalafmt
This commit is contained in:
PJ Fanning 2024-01-14 23:50:50 +01:00 committed by GitHub
parent 485639ef82
commit 8c688dad2e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 68 additions and 46 deletions

View file

@ -14,16 +14,16 @@
package org.apache.pekko.serialization
import java.io._
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.{ ByteBuffer, ByteOrder }
import java.nio.charset.StandardCharsets
import scala.annotation.nowarn
import scala.concurrent.Await
import scala.concurrent.duration._
import language.postfixOps
import SerializationTests._
import scala.annotation.nowarn
import com.typesafe.config._
import language.postfixOps
import test.org.apache.pekko.serialization.NoVerification
import org.apache.pekko
@ -247,7 +247,7 @@ class SerializeSpec extends PekkoSpec(SerializationTests.serializeConf) {
val byteSerializer = ser.serializerFor(classOf[Array[Byte]])
(byteSerializer.getClass should be).theSameInstanceAs(classOf[ByteArraySerializer])
for (a <- Seq("foo".getBytes("UTF-8"), null: Array[Byte], Array[Byte]()))
for (a <- Seq("foo".getBytes(StandardCharsets.UTF_8), null: Array[Byte], Array[Byte]()))
(byteSerializer.fromBinary(byteSerializer.toBinary(a)) should be).theSameInstanceAs(a)
intercept[IllegalArgumentException] {
@ -261,13 +261,13 @@ class SerializeSpec extends PekkoSpec(SerializationTests.serializeConf) {
val byteBuffer = ByteBuffer.allocate(128).order(ByteOrder.LITTLE_ENDIAN)
val str = "abcdef"
val payload = str.getBytes("UTF-8")
val payload = str.getBytes(StandardCharsets.UTF_8)
byteSerializer.toBinary(payload, byteBuffer)
byteBuffer.position() should ===(payload.length)
byteBuffer.flip()
val deserialized = byteSerializer.fromBinary(byteBuffer, "").asInstanceOf[Array[Byte]]
byteBuffer.remaining() should ===(0)
new String(deserialized, "UTF-8") should ===(str)
new String(deserialized, StandardCharsets.UTF_8) should ===(str)
intercept[IllegalArgumentException] {
byteSerializer.toBinary("pigdog", byteBuffer)

View file

@ -18,10 +18,11 @@ import java.lang.Double.doubleToRawLongBits
import java.lang.Float.floatToRawIntBits
import java.nio.{ ByteBuffer, ByteOrder }
import java.nio.ByteOrder.{ BIG_ENDIAN, LITTLE_ENDIAN }
import scala.collection.mutable.Builder
import java.nio.charset.StandardCharsets
import scala.annotation.nowarn
import scala.collection.mutable.Builder
import org.apache.commons.codec.binary.Hex.encodeHex
import org.scalacheck.{ Arbitrary, Gen }
import org.scalacheck.Arbitrary.arbitrary
@ -796,7 +797,7 @@ class ByteStringSpec extends AnyWordSpec with Matchers with Checkers {
"behave as expected" when {
"created from and decoding to String" in {
check { (s: String) =>
ByteString(s, "UTF-8").decodeString("UTF-8") == s
ByteString(s, StandardCharsets.UTF_8).decodeString(StandardCharsets.UTF_8) == s
}
}

View file

@ -14,6 +14,7 @@
package org.apache.pekko.serialization
import java.nio.{ BufferOverflowException, ByteBuffer }
import java.nio.charset.StandardCharsets
import org.apache.pekko
import pekko.actor.ExtendedActorSystem
@ -115,12 +116,13 @@ import pekko.util.ByteString
override def fromBinary(buf: ByteBuffer, manifest: String): AnyRef = {
val bytes = new Array[Byte](buf.remaining())
buf.get(bytes)
new String(bytes, "UTF-8")
new String(bytes, StandardCharsets.UTF_8)
}
override def toBinary(o: AnyRef): Array[Byte] = o.asInstanceOf[String].getBytes("UTF-8")
override def toBinary(o: AnyRef): Array[Byte] = o.asInstanceOf[String].getBytes(StandardCharsets.UTF_8)
override def fromBinary(bytes: Array[Byte], manifest: Option[Class[_]]): AnyRef = new String(bytes, "UTF-8")
override def fromBinary(bytes: Array[Byte], manifest: Option[Class[_]]): AnyRef =
new String(bytes, StandardCharsets.UTF_8)
}

View file

@ -13,7 +13,7 @@
package org.apache.pekko.util
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations._
@ -31,8 +31,8 @@ class ByteString_decode_Benchmark {
val bss_large = ByteStrings(Vector.fill(4)(bs_large.asInstanceOf[ByteString1C].toByteString1), 4 * bs_large.length)
val bc_large = bss_large.compact // compacted
val utf8String = "utf-8"
val utf8 = Charset.forName(utf8String)
val utf8 = StandardCharsets.UTF_8
val utf8String = utf8.name
/*
Using Charset helps a bit, but nothing impressive:

View file

@ -13,6 +13,7 @@
package org.apache.pekko.cluster
import java.nio.charset.StandardCharsets
import java.security.MessageDigest
import scala.annotation.tailrec
@ -38,7 +39,7 @@ private[cluster] object VectorClock {
private def hash(name: String): String = {
val digester = MessageDigest.getInstance("MD5")
digester.update(name.getBytes("UTF-8"))
digester.update(name.getBytes(StandardCharsets.UTF_8))
digester.digest.map { h =>
"%02x".format(0xFF & h)
}.mkString

View file

@ -13,7 +13,11 @@
package org.apache.pekko.cluster
import java.io.NotSerializableException
import java.nio.charset.StandardCharsets
import scala.concurrent.duration._
import com.typesafe.config.ConfigFactory
import org.apache.pekko
import pekko.actor.ActorIdentity
@ -29,8 +33,6 @@ import pekko.serialization.SerializerWithStringManifest
import pekko.testkit._
import pekko.util.unused
import java.io.NotSerializableException
object LargeMessageClusterMultiJvmSpec extends MultiNodeConfig {
val first = role("first")
val second = role("second")
@ -140,7 +142,7 @@ abstract class LargeMessageClusterSpec
val largeEchoProbe = TestProbe(name = "largeEchoProbe")
val largeMsgSize = 2 * 1000 * 1000
val largeMsg = ("0" * largeMsgSize).getBytes("utf-8")
val largeMsg = ("0" * largeMsgSize).getBytes(StandardCharsets.UTF_8)
val largeMsgBurst = 3
val repeat = 15
for (n <- 1 to repeat) {
@ -150,7 +152,7 @@ abstract class LargeMessageClusterSpec
}
val ordinaryProbe = TestProbe()
echo3.tell(("0" * 1000).getBytes("utf-8"), ordinaryProbe.ref)
echo3.tell(("0" * 1000).getBytes(StandardCharsets.UTF_8), ordinaryProbe.ref)
ordinaryProbe.expectMsgType[Array[Byte]]
val ordinaryDurationMs = (System.nanoTime() - startTime) / 1000 / 1000
@ -177,7 +179,7 @@ abstract class LargeMessageClusterSpec
val largeEcho3 = identify(third, "largeEcho")
val largeMsgSize = 1 * 1000 * 1000
val payload = ("0" * largeMsgSize).getBytes("utf-8")
val payload = ("0" * largeMsgSize).getBytes(StandardCharsets.UTF_8)
val largeMsg = if (aeronUdpEnabled) payload else Slow(payload)
(1 to 3).foreach { _ =>
// this will ping-pong between second and third

View file

@ -14,6 +14,7 @@
package org.apache.pekko.persistence
import java.io.IOException
import java.nio.charset.StandardCharsets
import java.util.UUID
import scala.concurrent.Future
@ -87,7 +88,7 @@ object SnapshotFailureRobustnessSpec {
class FailingLocalSnapshotStore(config: Config) extends LocalSnapshotStore(config) {
override def save(metadata: SnapshotMetadata, snapshot: Any): Unit = {
if (metadata.sequenceNr == 2 || snapshot.toString.startsWith("boom")) {
val bytes = "b0rkb0rk".getBytes("UTF-8") // length >= 8 to prevent EOF exception
val bytes = "b0rkb0rk".getBytes(StandardCharsets.UTF_8) // length >= 8 to prevent EOF exception
val tmpFile = withOutputStream(metadata)(_.write(bytes))
tmpFile.renameTo(snapshotFileForWrite(metadata))
} else super.save(metadata, snapshot)

View file

@ -14,7 +14,7 @@
package org.apache.pekko.pki.pem
import java.io.File
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.security.PrivateKey
@ -55,7 +55,7 @@ class DERPrivateKeyLoaderSpec extends AnyWordSpec with Matchers with EitherValue
resourceUrl.getProtocol should ===("file")
val path = new File(resourceUrl.toURI).toPath
val bytes = Files.readAllBytes(path)
val str = new String(bytes, Charset.forName("UTF-8"))
val str = new String(bytes, StandardCharsets.UTF_8)
val derData = PEMDecoder.decode(str)
derData
}

View file

@ -14,6 +14,7 @@
package org.apache.pekko.remote.artery
import java.io.File
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.time.Instant
import java.time.LocalDateTime
@ -84,7 +85,7 @@ object BenchmarkFileReporter {
def reportResults(result: String): Unit = synchronized {
println(result)
fos.write(result.getBytes("utf8"))
fos.write(result.getBytes(StandardCharsets.UTF_8))
fos.write('\n')
fos.flush()
}

View file

@ -13,6 +13,7 @@
package org.apache.pekko.remote.artery
import java.nio.charset.StandardCharsets
import java.util.concurrent.Executors
import java.util.concurrent.atomic.AtomicLongArray
import java.util.concurrent.locks.LockSupport
@ -261,7 +262,7 @@ abstract class LatencySpec extends RemotingMultiNodeSpec(LatencySpec) {
import testSettings._
runOn(first) {
val payload = ("0" * payloadSize).getBytes("utf-8")
val payload = ("0" * payloadSize).getBytes(StandardCharsets.UTF_8)
// by default run for 2 seconds, but can be adjusted with the totalMessagesFactor
val totalMessages = (2 * messageRate * totalMessagesFactor).toInt
val sendTimes = new AtomicLongArray(totalMessages)

View file

@ -13,10 +13,14 @@
package org.apache.pekko.remote.artery
import java.io.NotSerializableException
import java.nio.ByteBuffer
import java.nio.charset.StandardCharsets
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit.NANOSECONDS
import scala.concurrent.duration._
import com.typesafe.config.ConfigFactory
import org.apache.pekko
import pekko.actor._
@ -28,8 +32,6 @@ import pekko.serialization.{ ByteBufferSerializer, SerializerWithStringManifest
import pekko.serialization.jackson.CborSerializable
import pekko.testkit._
import java.io.NotSerializableException
object MaxThroughputSpec extends MultiNodeConfig {
val first = role("first")
val second = role("second")
@ -171,7 +173,7 @@ object MaxThroughputSpec extends MultiNodeConfig {
val numTargets = targets.size
import testSettings._
val payload = ("0" * testSettings.payloadSize).getBytes("utf-8")
val payload = ("0" * testSettings.payloadSize).getBytes(StandardCharsets.UTF_8)
var startTime = 0L
var remaining = totalMessages
var maxRoundTripMillis = 0L

View file

@ -15,6 +15,7 @@ package org.apache.pekko.remote.artery
package aeron
import java.io.File
import java.nio.charset.StandardCharsets
import java.util.concurrent.atomic.AtomicInteger
import scala.concurrent.Await
@ -127,7 +128,7 @@ abstract class AeronStreamConsistencySpec
val done = TestLatch(1)
val killSwitch = KillSwitches.shared("test")
val started = TestProbe()
val startMsg = "0".getBytes("utf-8")
val startMsg = "0".getBytes(StandardCharsets.UTF_8)
Source
.fromGraph(new AeronSource(channel(first), streamId, aeron, taskRunner, pool, NoOpRemotingFlightRecorder, 0))
.via(killSwitch.flow)
@ -137,7 +138,7 @@ abstract class AeronStreamConsistencySpec
started.ref ! Done
else {
val c = count.incrementAndGet()
val x = new String(bytes.toArray, "utf-8").toInt
val x = new String(bytes.toArray, StandardCharsets.UTF_8).toInt
if (x != c) {
throw new IllegalArgumentException(s"# wrong message $x expected $c")
}
@ -174,7 +175,7 @@ abstract class AeronStreamConsistencySpec
.throttle(10000, 1.second, 1000, ThrottleMode.Shaping)
.map { n =>
val envelope = pool.acquire()
envelope.byteBuffer.put(n.toString.getBytes("utf-8"))
envelope.byteBuffer.put(n.toString.getBytes(StandardCharsets.UTF_8))
envelope.byteBuffer.flip()
envelope
}

View file

@ -15,6 +15,7 @@ package org.apache.pekko.remote.artery
package aeron
import java.io.File
import java.nio.charset.StandardCharsets
import java.util.concurrent.CyclicBarrier
import java.util.concurrent.Executors
import java.util.concurrent.atomic.AtomicBoolean
@ -180,7 +181,7 @@ abstract class AeronStreamLatencySpec
import testSettings._
runOn(first) {
val payload = ("1" * payloadSize).getBytes("utf-8")
val payload = ("1" * payloadSize).getBytes(StandardCharsets.UTF_8)
// by default run for 2 seconds, but can be adjusted with the totalMessagesFactor
val totalMessages = (2 * messageRate * totalMessagesFactor).toInt
val sendTimes = new AtomicLongArray(totalMessages)
@ -193,7 +194,7 @@ abstract class AeronStreamLatencySpec
val lastRepeat = new AtomicBoolean(false)
val killSwitch = KillSwitches.shared(testName)
val started = TestProbe()
val startMsg = "0".getBytes("utf-8")
val startMsg = "0".getBytes(StandardCharsets.UTF_8)
Source
.fromGraph(new AeronSource(channel(first), streamId, aeron, taskRunner, pool, NoOpRemotingFlightRecorder, 0))
.via(killSwitch.flow)

View file

@ -15,6 +15,7 @@ package org.apache.pekko.remote.artery
package aeron
import java.io.File
import java.nio.charset.StandardCharsets
import java.util.concurrent.Executors
import scala.collection.AbstractIterator
@ -196,7 +197,7 @@ abstract class AeronStreamMaxThroughputSpec
runOn(first) {
enterBarrier(receiverName + "-started")
val payload = ("0" * payloadSize).getBytes("utf-8")
val payload = ("0" * payloadSize).getBytes(StandardCharsets.UTF_8)
Source
.fromIterator(() => iterate(1, totalMessages))
.map { _ =>

View file

@ -13,6 +13,8 @@
package org.apache.pekko.remote.artery
import java.nio.charset.StandardCharsets
import org.apache.pekko
import pekko.actor.{ ActorIdentity, Identify, RootActorPath }
import pekko.testkit.EventFilter
@ -71,7 +73,7 @@ class SerializationErrorSpec extends ArteryMultiNodeSpec(ArterySpecSupport.defau
EventFilter
.error(pattern = """Failed to deserialize message from \[.*\] with serializer id \[4\]""", occurrences = 1)
.intercept {
remoteRef ! "boom".getBytes("utf-8")
remoteRef ! "boom".getBytes(StandardCharsets.UTF_8)
}(systemB)
remoteRef ! "ping2"

View file

@ -180,7 +180,7 @@ class JacksonJsonSerializerSpec extends JacksonSerializerSpec("jackson-json") {
def serializeToJsonString(obj: AnyRef, sys: ActorSystem = system): String = {
val blob = serializeToBinary(obj, sys)
new String(blob, "utf-8")
new String(blob, StandardCharsets.UTF_8)
}
def deserializeFromJsonString(
@ -188,7 +188,7 @@ class JacksonJsonSerializerSpec extends JacksonSerializerSpec("jackson-json") {
serializerId: Int,
manifest: String,
sys: ActorSystem = system): AnyRef = {
val blob = json.getBytes("utf-8")
val blob = json.getBytes(StandardCharsets.UTF_8)
deserializeFromBinary(blob, serializerId, manifest, sys)
}

View file

@ -14,6 +14,7 @@
package org.apache.pekko.event.slf4j
import java.io.ByteArrayOutputStream
import java.nio.charset.StandardCharsets
import scala.concurrent.duration._
@ -71,7 +72,7 @@ object Slf4jLoggerSpec {
class MyLogSource
val output = new ByteArrayOutputStream
def outputString: String = output.toString("UTF-8")
def outputString: String = output.toString(StandardCharsets.UTF_8.name)
class TestAppender[E] extends OutputStreamAppender[E] {

View file

@ -14,6 +14,7 @@
package org.apache.pekko.stream.io.compression
import java.io.{ InputStream, OutputStream }
import java.nio.charset.StandardCharsets
import java.util.zip.{ GZIPInputStream, GZIPOutputStream, ZipException }
import org.apache.pekko
@ -47,7 +48,7 @@ class GzipSpec extends CoderSpec("gzip") {
ex.ultimateCause.getMessage should equal("Truncated GZIP stream")
}
"throw an error if compressed data is just missing the trailer at the end" in {
def brokenCompress(payload: String) = newCompressor().compress(ByteString(payload, "UTF-8"))
def brokenCompress(payload: String) = newCompressor().compress(ByteString(payload, StandardCharsets.UTF_8))
val ex = the[RuntimeException] thrownBy ourDecode(brokenCompress("abcdefghijkl"))
ex.ultimateCause.getMessage should equal("Truncated GZIP stream")
}

View file

@ -13,6 +13,7 @@
package org.apache.pekko.stream.io.compression
import java.nio.charset.StandardCharsets
import java.util.zip.{ Deflater, ZipException }
import org.apache.pekko
@ -39,7 +40,7 @@ class GzipWithCustomCompressionLevelSpec extends GzipSpec {
ex.ultimateCause.getMessage should equal("Truncated GZIP stream")
}
"throw an error if compressed data is just missing the trailer at the end" in {
def brokenCompress(payload: String) = newCompressor().compress(ByteString(payload, "UTF-8"))
def brokenCompress(payload: String) = newCompressor().compress(ByteString(payload, StandardCharsets.UTF_8))
val ex = the[RuntimeException] thrownBy ourDecode(brokenCompress("abcdefghijkl"))
ex.ultimateCause.getMessage should equal("Truncated GZIP stream")
}

View file

@ -13,6 +13,8 @@
package org.apache.pekko.stream.scaladsl
import java.nio.charset.StandardCharsets
import scala.annotation.nowarn
import scala.collection.immutable
import scala.concurrent.Await
@ -31,7 +33,7 @@ class BidiFlowSpec extends StreamSpec {
val bidi = BidiFlow.fromFlows(
Flow[Int].map(x => x.toLong + 2).withAttributes(name("top")),
Flow[ByteString].map(_.decodeString("UTF-8")).withAttributes(name("bottom")))
Flow[ByteString].map(_.decodeString(StandardCharsets.UTF_8)).withAttributes(name("bottom")))
val inverse = BidiFlow.fromFlows(
Flow[Long].map(x => x.toInt + 2).withAttributes(name("top")),
@ -41,7 +43,7 @@ class BidiFlowSpec extends StreamSpec {
Source.single(42) ~> s
val top = b.add(Flow[Int].map(x => x.toLong + 2))
val bottom = b.add(Flow[ByteString].map(_.decodeString("UTF-8")))
val bottom = b.add(Flow[ByteString].map(_.decodeString(StandardCharsets.UTF_8)))
BidiShape(top.in, top.out, bottom.in, bottom.out)
})

View file

@ -15,6 +15,7 @@ package org.apache.pekko.testkit
import java.io._
import java.lang.management.ManagementFactory
import java.nio.charset.StandardCharsets
import java.util.concurrent.Semaphore
import java.util.concurrent.locks.ReentrantLock
@ -28,9 +29,9 @@ class CoronerSpec extends AnyWordSpec with Matchers {
private def captureOutput[A](f: PrintStream => A): (A, String) = {
val bytes = new ByteArrayOutputStream()
val out = new PrintStream(bytes, true, "UTF-8")
val out = new PrintStream(bytes, true, StandardCharsets.UTF_8.name)
val result = f(out)
(result, new String(bytes.toByteArray(), "UTF-8"))
(result, bytes.toString(StandardCharsets.UTF_8.name))
}
"A Coroner" must {