From 8136c94b8ef619ca81845f8d1c7b8544aa40c282 Mon Sep 17 00:00:00 2001 From: Joris Borgdorff Date: Tue, 23 Jun 2020 13:40:05 +0200 Subject: [PATCH] Slightly faster Base64 implementation --- radar-commons/src/main/java/org/radarbase/util/Base64.java | 4 +++- .../src/test/java/org/radarbase/util/Base64Test.java | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/radar-commons/src/main/java/org/radarbase/util/Base64.java b/radar-commons/src/main/java/org/radarbase/util/Base64.java index 7f0945a9..62d630b1 100644 --- a/radar-commons/src/main/java/org/radarbase/util/Base64.java +++ b/radar-commons/src/main/java/org/radarbase/util/Base64.java @@ -25,6 +25,8 @@ package org.radarbase.util; +import static java.nio.charset.StandardCharsets.UTF_8; // Since Android API 19 + /** * This class consists exclusively of static methods for obtaining * encoders and decoders for the Base64 encoding scheme. The @@ -134,7 +136,7 @@ public String encode(byte[] src) { dst[dstP] = '='; } - return new String(dst); + return new String(dst, UTF_8); } } } diff --git a/radar-commons/src/test/java/org/radarbase/util/Base64Test.java b/radar-commons/src/test/java/org/radarbase/util/Base64Test.java index cf0bfb0f..a22f6bf9 100644 --- a/radar-commons/src/test/java/org/radarbase/util/Base64Test.java +++ b/radar-commons/src/test/java/org/radarbase/util/Base64Test.java @@ -14,7 +14,7 @@ public void encoderTest() { java.util.Base64.Encoder javaEncoder = java.util.Base64.getEncoder(); ThreadLocalRandom random = ThreadLocalRandom.current(); - for (int i = 0; i < 2_000; i++) { + for (int i = 0; i < 2_000; i += 7) { byte[] src = new byte[i]; random.nextBytes(src); String actual = encoder.encode(src);