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
This commit is contained in:
JesusFreke@JesusFreke.com 2010-02-22 00:56:36 +00:00
parent d79d9ebbe9
commit 2ba2d0f16b

View File

@ -57,15 +57,22 @@ public final class Utf8Utils {
return result; return result;
} }
private static char[] tempBuffer = null;
/** /**
* Converts an array of UTF-8 bytes into a string. * 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 * @param bytes non-null; the bytes to convert
* @return non-null; the converted string * @return non-null; the converted string
*/ */
public static String utf8BytesToString(byte[] bytes) { public static String utf8BytesToString(byte[] bytes) {
int length = bytes.length; 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; int outAt = 0;
for (int at = 0; length > 0; /*at*/) { for (int at = 0; length > 0; /*at*/) {