Change ByteArrayInput.readNullTerminatedBytes() to ByteArrayInput.readNullTerminatedUtf8String()

git-svn-id: https://smali.googlecode.com/svn/trunk@685 55b6fa8a-2a1e-11de-a435-ffa8d773f76a
This commit is contained in:
JesusFreke@JesusFreke.com 2010-04-03 23:02:03 +00:00
parent 89325d96cc
commit 1f29ee7351
4 changed files with 13 additions and 11 deletions

View File

@ -78,12 +78,12 @@ public class StringDataItem extends Item<StringDataItem> {
public static StringDataItem lookupStringDataItem(DexFile dexFile, String value) {
StringDataItem StringDataItem = new StringDataItem(dexFile, value);
return dexFile.StringDataSection.getInternedItem(StringDataItem);
}
}
/** {@inheritDoc} */
protected void readItem(Input in, ReadContext readContext) {
in.readUnsignedLeb128(); //string length
stringValue = Utf8Utils.utf8BytesToString(in.readNullTerminatedBytes());
stringValue = in.realNullTerminatedUtf8String();
}
/** {@inheritDoc} */

View File

@ -296,7 +296,7 @@ public class ByteArrayInput
}
/** {@inheritDoc} */
public byte[] readNullTerminatedBytes() {
public String realNullTerminatedUtf8String() {
int startPosition = cursor;
while (data[cursor] != 0) {
cursor++;
@ -305,12 +305,11 @@ public class ByteArrayInput
}
}
int byteCount = cursor - startPosition;
//skip the terminating null
cursor++;
byte[] result = new byte[byteCount];
System.arraycopy(data, startPosition, result, 0, byteCount);
return result;
return Utf8Utils.utf8BytesToString(data, startPosition, byteCount);
}
/** {@inheritDoc} */

View File

@ -132,11 +132,13 @@ public interface Input {
public byte[] readBytes(int length);
/**
* reads a <code>byte[]</code> from this instance, from the current cursor up to but not including
* reads and decodes a null terminated utf8 string from the current cursor up to but not including
* the next null (0) byte. The terminating null byte is read and discarded, so that after the read,
* the cursor is positioned at the byte immediately after the terminating null
*
* @return a string representing the decoded value
*/
public byte[] readNullTerminatedBytes();
public String realNullTerminatedUtf8String();
/**
* Skips the given number of bytes.

View File

@ -68,17 +68,18 @@ public final class Utf8Utils {
* 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 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
* @return non-null; the converted string
*/
public static String utf8BytesToString(byte[] bytes) {
int length = bytes.length;
public static String utf8BytesToString(byte[] bytes, int start, int length) {
if (tempBuffer == null || tempBuffer.length < length) {
tempBuffer = new char[length];
}
char[] chars = tempBuffer;
int outAt = 0;
for (int at = 0; length > 0; /*at*/) {
for (int at = start; length > 0; /*at*/) {
int v0 = bytes[at] & 0xFF;
char out;
switch (v0 >> 4) {