From 2ba2d0f16bf8a40d89f8ba26ed01096b2cb629f3 Mon Sep 17 00:00:00 2001 From: "JesusFreke@JesusFreke.com" Date: Mon, 22 Feb 2010 00:56:36 +0000 Subject: [PATCH] Use a pre-allocated buffer in Utf8Utils.utf8BytesToString, to avoid having to allocate a buffer on each call git-svn-id: https://smali.googlecode.com/svn/trunk@636 55b6fa8a-2a1e-11de-a435-ffa8d773f76a --- .../main/java/org/jf/dexlib/Util/Utf8Utils.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/dexlib/src/main/java/org/jf/dexlib/Util/Utf8Utils.java b/dexlib/src/main/java/org/jf/dexlib/Util/Utf8Utils.java index eb59386b..c4a06813 100644 --- a/dexlib/src/main/java/org/jf/dexlib/Util/Utf8Utils.java +++ b/dexlib/src/main/java/org/jf/dexlib/Util/Utf8Utils.java @@ -20,13 +20,13 @@ package org.jf.dexlib.Util; * Constants of type CONSTANT_Utf8_info. */ public final class Utf8Utils { - + /** * Converts a string into its Java-style UTF-8 form. Java-style UTF-8 * differs from normal UTF-8 in the handling of character '\0' and * surrogate pairs. - * + * * @param string non-null; the string to convert * @return non-null; the UTF-8 bytes for it */ @@ -57,15 +57,22 @@ public final class Utf8Utils { return result; } + private static char[] tempBuffer = null; + /** * Converts an array of UTF-8 bytes into a string. - * + * + * This method uses a global buffer to avoid having to allocate one every time, so it is *not* thread-safe + * * @param bytes non-null; the bytes to convert * @return non-null; the converted string */ public static String utf8BytesToString(byte[] bytes) { int length = bytes.length; - char[] chars = new char[length]; // This is sized to avoid a realloc. + if (tempBuffer == null || tempBuffer.length < length) { + tempBuffer = new char[length]; + } + char[] chars = tempBuffer; int outAt = 0; for (int at = 0; length > 0; /*at*/) { @@ -148,7 +155,7 @@ public final class Utf8Utils { /** * Helper for {@link #utf8BytesToString}, which throws the right * exception for a bogus utf-8 byte. - * + * * @param value the byte value * @param offset the file offset * @return never