From e8c414b9cc72cd0e4f8c66b836de4c43f134b102 Mon Sep 17 00:00:00 2001 From: Matthias Sohn Date: Fri, 6 Sep 2024 12:26:44 +0200 Subject: [PATCH] Replace custom encoder `Constants#encode` by JDK implementation Using the implementation provided in the JDK since Java 1.6 by `String#getBytes(Charset)` reduces JGit maintenance effort and improves performance. The method Constants#encode was implemented when JGit still used Java 1.5. See [1]. Kudos to Marcin for proposing to use this improvement in RefWriter [2]. I think it should be used generally. [1] https://repo.or.cz/jgit.git?a=commit;h=bfa3da225f198b19061158499b1135aff07d85b3 [2] https://eclipse.gerrithub.io/c/eclipse-jgit/jgit/+/1195180 Also-By: Marcin Czech Change-Id: I361ed6286b98351a315b8a8ffc3cb845831d35b2 (cherry picked from commit e5d2898997462e0f2409c09497ab62c6cda2dbaf) --- .../src/org/eclipse/jgit/lib/Constants.java | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java index 1a97d111e3..5d7eacb4b8 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java @@ -14,7 +14,6 @@ package org.eclipse.jgit.lib; import static java.nio.charset.StandardCharsets.UTF_8; -import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -688,17 +687,7 @@ public final class Constants { * @see #CHARACTER_ENCODING */ public static byte[] encode(String str) { - final ByteBuffer bb = UTF_8.encode(str); - final int len = bb.limit(); - if (bb.hasArray() && bb.arrayOffset() == 0) { - final byte[] arr = bb.array(); - if (arr.length == len) - return arr; - } - - final byte[] arr = new byte[len]; - bb.get(arr); - return arr; + return str.getBytes(UTF_8); } static { -- 2.39.5