mirror of
https://github.com/revanced/smali.git
synced 2025-05-11 11:54:29 +02:00
Use thread local storage for the temporary buffer in Utf8Utils
This commit is contained in:
parent
9f1d05eb44
commit
09e6d003cb
@ -65,23 +65,28 @@ public final class Utf8Utils {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static char[] tempBuffer = null;
|
private static final ThreadLocal<char[]> localBuffer =
|
||||||
|
new ThreadLocal<char[]> () {
|
||||||
|
@Override protected char[] initialValue() {
|
||||||
|
// A reasonably sized initial value
|
||||||
|
return new char[256];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
* @param start the start index of the utf8 string to convert
|
* @param start the start index of the utf8 string to convert
|
||||||
* @param length the length of the utf8 string to convert, not including any null-terminator that might be present
|
* @param length the length of the utf8 string to convert, not including any null-terminator that might be present
|
||||||
* @return non-null; the converted string
|
* @return non-null; the converted string
|
||||||
*/
|
*/
|
||||||
public static String utf8BytesToString(byte[] bytes, int start, int length) {
|
public static String utf8BytesToString(byte[] bytes, int start, int length) {
|
||||||
if (tempBuffer == null || tempBuffer.length < length) {
|
char[] chars = localBuffer.get();
|
||||||
tempBuffer = new char[length];
|
if (chars == null || chars.length < length) {
|
||||||
|
chars = new char[length];
|
||||||
|
localBuffer.set(chars);
|
||||||
}
|
}
|
||||||
char[] chars = tempBuffer;
|
|
||||||
int outAt = 0;
|
int outAt = 0;
|
||||||
|
|
||||||
for (int at = start; length > 0; /*at*/) {
|
for (int at = start; length > 0; /*at*/) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user