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 2024/01/16 09:21:32 UTC

(incubator-pekko-http) branch main updated: use StandardCharsets (#421)

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/incubator-pekko-http.git


The following commit(s) were added to refs/heads/main by this push:
     new 448268b21 use StandardCharsets (#421)
448268b21 is described below

commit 448268b21483ffa4a822a7126a6afb497e86a0b3
Author: PJ Fanning <pj...@users.noreply.github.com>
AuthorDate: Tue Jan 16 10:21:27 2024 +0100

    use StandardCharsets (#421)
    
    * use StandardCharsets
    
    * more StandardCharsets
---
 .../directives/FileUploadDirectivesExamplesTest.java     |  4 ++--
 .../pekko/http/shaded/com/twitter/hpack/HpackUtil.java   |  3 ++-
 .../http/impl/engine/ws/MessageToFrameRenderer.scala     |  4 +++-
 .../scala/org/apache/pekko/http/impl/util/package.scala  |  8 ++++----
 .../apache/pekko/http/impl/engine/ws/WSTestUtils.scala   |  8 +++++---
 .../org/apache/pekko/http/impl/util/RenderingSpec.scala  |  4 ++--
 .../directives/FileAndResourceDirectivesSpec.scala       | 16 +++++++++-------
 .../http/scaladsl/unmarshalling/UnmarshallingSpec.scala  |  5 +++--
 8 files changed, 30 insertions(+), 22 deletions(-)

diff --git a/docs/src/test/java/docs/http/javadsl/server/directives/FileUploadDirectivesExamplesTest.java b/docs/src/test/java/docs/http/javadsl/server/directives/FileUploadDirectivesExamplesTest.java
index 547d77856..8a52500b6 100644
--- a/docs/src/test/java/docs/http/javadsl/server/directives/FileUploadDirectivesExamplesTest.java
+++ b/docs/src/test/java/docs/http/javadsl/server/directives/FileUploadDirectivesExamplesTest.java
@@ -27,7 +27,7 @@ import org.junit.Test;
 import scala.concurrent.duration.FiniteDuration;
 
 import java.io.File;
-import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -300,7 +300,7 @@ public class FileUploadDirectivesExamplesTest extends JUnitRouteTest {
       Files.write(
           tempFile.toPath(),
           Arrays.asList("2,3,5", "7,11,13,17,23", "29,31,37"),
-          Charset.forName("UTF-8"));
+          StandardCharsets.UTF_8);
     } catch (Exception e) {
       // ignore
     }
diff --git a/http-core/src/main/java/org/apache/pekko/http/shaded/com/twitter/hpack/HpackUtil.java b/http-core/src/main/java/org/apache/pekko/http/shaded/com/twitter/hpack/HpackUtil.java
index 54fb6da64..3823a48ec 100644
--- a/http-core/src/main/java/org/apache/pekko/http/shaded/com/twitter/hpack/HpackUtil.java
+++ b/http-core/src/main/java/org/apache/pekko/http/shaded/com/twitter/hpack/HpackUtil.java
@@ -32,10 +32,11 @@
 package org.apache.pekko.http.shaded.com.twitter.hpack;
 
 import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 
 final class HpackUtil {
 
-  static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
+  static final Charset ISO_8859_1 = StandardCharsets.ISO_8859_1;
 
   /** A string compare that doesn't leak timing information. */
   static boolean equals(byte[] s1, byte[] s2) {
diff --git a/http-core/src/main/scala/org/apache/pekko/http/impl/engine/ws/MessageToFrameRenderer.scala b/http-core/src/main/scala/org/apache/pekko/http/impl/engine/ws/MessageToFrameRenderer.scala
index 8ca67aaee..839f37fe7 100644
--- a/http-core/src/main/scala/org/apache/pekko/http/impl/engine/ws/MessageToFrameRenderer.scala
+++ b/http-core/src/main/scala/org/apache/pekko/http/impl/engine/ws/MessageToFrameRenderer.scala
@@ -13,6 +13,8 @@
 
 package org.apache.pekko.http.impl.engine.ws
 
+import java.nio.charset.StandardCharsets
+
 import org.apache.pekko
 import pekko.NotUsed
 import pekko.util.ByteString
@@ -43,7 +45,7 @@ private[http] object MessageToFrameRenderer {
       .flatMapConcat {
         case BinaryMessage.Strict(data) => strictFrames(Opcode.Binary, data)
         case bm: BinaryMessage          => streamedFrames(Opcode.Binary, bm.dataStream)
-        case TextMessage.Strict(text)   => strictFrames(Opcode.Text, ByteString(text, "UTF-8"))
+        case TextMessage.Strict(text)   => strictFrames(Opcode.Text, ByteString(text, StandardCharsets.UTF_8))
         case tm: TextMessage            => streamedFrames(Opcode.Text, tm.textStream.via(Utf8Encoder))
       }
   }
diff --git a/http-core/src/main/scala/org/apache/pekko/http/impl/util/package.scala b/http-core/src/main/scala/org/apache/pekko/http/impl/util/package.scala
index 1f98f5b53..bfbe15e09 100644
--- a/http-core/src/main/scala/org/apache/pekko/http/impl/util/package.scala
+++ b/http-core/src/main/scala/org/apache/pekko/http/impl/util/package.scala
@@ -14,7 +14,7 @@
 package org.apache.pekko.http.impl
 
 import language.implicitConversions
-import java.nio.charset.Charset
+import java.nio.charset.StandardCharsets
 
 import com.typesafe.config.Config
 import org.apache.pekko
@@ -32,9 +32,9 @@ import pekko.http.impl.engine.parsing.ParserOutput.RequestStart
 import pekko.http.scaladsl.model.{ HttpEntity, HttpRequest, HttpResponse }
 
 package object util {
-  private[http] val UTF8 = Charset.forName("UTF8")
-  private[http] val ASCII = Charset.forName("ASCII")
-  private[http] val ISO88591 = Charset.forName("ISO-8859-1")
+  private[http] val UTF8 = StandardCharsets.UTF_8
+  private[http] val ASCII = StandardCharsets.US_ASCII
+  private[http] val ISO88591 = StandardCharsets.ISO_8859_1
 
   private[http] val EmptyByteArray = Array.empty[Byte]
 
diff --git a/http-core/src/test/scala/org/apache/pekko/http/impl/engine/ws/WSTestUtils.scala b/http-core/src/test/scala/org/apache/pekko/http/impl/engine/ws/WSTestUtils.scala
index a5a7ec200..dc09ec5d4 100644
--- a/http-core/src/test/scala/org/apache/pekko/http/impl/engine/ws/WSTestUtils.scala
+++ b/http-core/src/test/scala/org/apache/pekko/http/impl/engine/ws/WSTestUtils.scala
@@ -13,6 +13,8 @@
 
 package org.apache.pekko.http.impl.engine.ws
 
+import java.nio.charset.StandardCharsets
+
 import org.apache.pekko
 import pekko.http.impl.engine.ws.Protocol.Opcode
 import pekko.util.ByteString
@@ -62,7 +64,7 @@ object WSTestUtils {
       frameHeader(opcode, data.size, fin, mask = None) ++ data
 
   def closeFrame(closeCode: Int, mask: Boolean, msg: String = ""): ByteString =
-    closeFrame(closeCode, mask, ByteString(msg, "UTF-8"))
+    closeFrame(closeCode, mask, ByteString(msg, StandardCharsets.UTF_8))
 
   def closeFrame(closeCode: Int, mask: Boolean, msgBytes: ByteString): ByteString =
     frame(Opcode.Close, closeFrameData(closeCode, msgBytes), fin = true, mask)
@@ -71,9 +73,9 @@ object WSTestUtils {
     shortBE(closeCode) ++ msgBytes
 
   def maskedASCII(str: String, mask: Int): (ByteString, Int) =
-    FrameEventParser.mask(ByteString(str, "ASCII"), mask)
+    FrameEventParser.mask(ByteString(str, StandardCharsets.US_ASCII), mask)
   def maskedUTF8(str: String, mask: Int): (ByteString, Int) =
-    FrameEventParser.mask(ByteString(str, "UTF-8"), mask)
+    FrameEventParser.mask(ByteString(str, StandardCharsets.UTF_8), mask)
   def maskedBytes(bytes: ByteString, mask: Int): (ByteString, Int) =
     FrameEventParser.mask(bytes, mask)
 
diff --git a/http-core/src/test/scala/org/apache/pekko/http/impl/util/RenderingSpec.scala b/http-core/src/test/scala/org/apache/pekko/http/impl/util/RenderingSpec.scala
index 2adee2c64..94312d5cc 100644
--- a/http-core/src/test/scala/org/apache/pekko/http/impl/util/RenderingSpec.scala
+++ b/http-core/src/test/scala/org/apache/pekko/http/impl/util/RenderingSpec.scala
@@ -19,7 +19,7 @@ import pekko.http.scaladsl.model.headers.RawHeader
 import pekko.testkit.EventFilter
 import org.scalatest.matchers.should.Matchers
 
-import java.nio.charset.Charset
+import java.nio.charset.StandardCharsets
 import scala.reflect.{ classTag, ClassTag }
 
 class RenderingSpec extends PekkoSpecWithMaterializer with Matchers {
@@ -70,7 +70,7 @@ class RenderingSpec extends PekkoSpecWithMaterializer with Matchers {
       setup(new StringRendering)(_.get),
       setup(new ByteArrayRendering(1000, Logging(system, "test").warning))(r => new String(r.get)),
       setup(new ByteStringRendering(1000, Logging(system, "test").warning))(_.get.utf8String),
-      setup(new CustomCharsetByteStringRendering(Charset.forName("ISO-8859-1"), 1000))(_.get.utf8String))
+      setup(new CustomCharsetByteStringRendering(StandardCharsets.ISO_8859_1, 1000))(_.get.utf8String))
 
     renderings.foreach { setup =>
       setup.tag should {
diff --git a/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/server/directives/FileAndResourceDirectivesSpec.scala b/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/server/directives/FileAndResourceDirectivesSpec.scala
index 18542caba..7c1d3645c 100644
--- a/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/server/directives/FileAndResourceDirectivesSpec.scala
+++ b/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/server/directives/FileAndResourceDirectivesSpec.scala
@@ -15,21 +15,23 @@ package org.apache.pekko.http.scaladsl.server
 package directives
 
 import java.io.File
-
-import org.apache.pekko
-import pekko.http.scaladsl.settings.RoutingSettings
-import pekko.http.scaladsl.testkit.RouteTestTimeout
+import java.nio.charset.StandardCharsets
 
 import scala.concurrent.duration._
 import scala.util.Properties
-import org.scalatest.{ Inside, Inspectors }
+
+import org.apache.pekko
 import pekko.http.scaladsl.model.MediaTypes._
+import pekko.http.scaladsl.model.Uri.Path
 import pekko.http.scaladsl.model._
 import pekko.http.scaladsl.model.headers._
+import pekko.http.scaladsl.settings.RoutingSettings
+import pekko.http.scaladsl.testkit.RouteTestTimeout
 import pekko.http.impl.util._
-import pekko.http.scaladsl.model.Uri.Path
 import pekko.testkit._
 
+import org.scalatest.{ Inside, Inspectors }
+
 class FileAndResourceDirectivesSpec extends RoutingSpec with Inspectors with Inside {
 
   // operations touch files, can be randomly hit by slowness
@@ -45,7 +47,7 @@ class FileAndResourceDirectivesSpec extends RoutingSpec with Inspectors with Ins
   """
 
   def writeAllText(text: String, file: File): Unit =
-    java.nio.file.Files.write(file.toPath, text.getBytes("UTF-8"))
+    java.nio.file.Files.write(file.toPath, text.getBytes(StandardCharsets.UTF_8))
 
   "getFromFile" should {
     "reject non-GET requests" in {
diff --git a/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/unmarshalling/UnmarshallingSpec.scala b/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/unmarshalling/UnmarshallingSpec.scala
index 7da31ab22..07278ab92 100644
--- a/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/unmarshalling/UnmarshallingSpec.scala
+++ b/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/unmarshalling/UnmarshallingSpec.scala
@@ -13,6 +13,7 @@
 
 package org.apache.pekko.http.scaladsl.unmarshalling
 
+import java.nio.charset.StandardCharsets
 import java.util.UUID
 
 import org.apache.pekko
@@ -41,7 +42,7 @@ class UnmarshallingSpec extends AnyFreeSpec with Matchers with BeforeAndAfterAll
       Unmarshal(HttpEntity("Hällö")).to[String] should evaluateTo("Hällö")
     }
     "stringUnmarshaller should assume UTF-8 for textual content type with missing charset" in {
-      Unmarshal(HttpEntity(MediaTypes.`text/plain`.withMissingCharset, "Hällö".getBytes("UTF-8"))).to[
+      Unmarshal(HttpEntity(MediaTypes.`text/plain`.withMissingCharset, "Hällö".getBytes(StandardCharsets.UTF_8))).to[
         String] should evaluateTo("Hällö")
     }
     "charArrayUnmarshaller should unmarshal `text/plain` content in UTF-8 to char arrays" in {
@@ -116,7 +117,7 @@ class UnmarshallingSpec extends AnyFreeSpec with Matchers with BeforeAndAfterAll
 
     "should handle media ranges of types with missing charset by assuming UTF-8 charset when matching" in {
       val um = Unmarshaller.stringUnmarshaller.forContentTypes(MediaTypes.`text/plain`)
-      Await.result(um(HttpEntity(MediaTypes.`text/plain`.withMissingCharset, "Hêllö".getBytes("utf-8"))),
+      Await.result(um(HttpEntity(MediaTypes.`text/plain`.withMissingCharset, "Hêllö".getBytes(StandardCharsets.UTF_8))),
         1.second.dilated) should ===("Hêllö")
     }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pekko.apache.org
For additional commands, e-mail: commits-help@pekko.apache.org