diff --git a/crypto/src/main/java/org/springframework/security/crypto/codec/Utf8.java b/crypto/src/main/java/org/springframework/security/crypto/codec/Utf8.java index 9a2d0c568d..70ffc62a18 100644 --- a/crypto/src/main/java/org/springframework/security/crypto/codec/Utf8.java +++ b/crypto/src/main/java/org/springframework/security/crypto/codec/Utf8.java @@ -22,8 +22,10 @@ public final class Utf8 { public static byte[] encode(CharSequence string) { try { ByteBuffer bytes = CHARSET.newEncoder().encode(CharBuffer.wrap(string)); + byte[] bytesCopy = new byte[bytes.limit()]; + System.arraycopy(bytes.array(), 0, bytesCopy, 0, bytes.limit()); - return Arrays.copyOfRange(bytes.array(), 0, bytes.limit()); + return bytesCopy; } catch (CharacterCodingException e) { throw new IllegalArgumentException("Encoding failed", e); }