From dfc5e8e159e7df3efa47d553b7725be22839665d Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Fri, 14 Jan 2011 13:16:06 -0800 Subject: Read Dex files from a byte[] rather than a RandomAccessFile. This improves time to merge a trivial file with core.dex from 3.8 seconds to 0.8 seconds. Also fixing read method names to be consistent with the spec. Change-Id: I9033bcb497afe7a0d73e00cca14fa046b53a62bf --- dx/src/com/android/dx/dex/file/CatchStructs.java | 14 +-- dx/src/com/android/dx/dex/file/ClassDataItem.java | 5 +- .../com/android/dx/dex/file/DebugInfoEncoder.java | 24 ++-- dx/src/com/android/dx/dex/file/EncodedField.java | 4 +- dx/src/com/android/dx/dex/file/EncodedMethod.java | 6 +- dx/src/com/android/dx/dex/file/StringDataItem.java | 2 +- dx/src/com/android/dx/dex/file/ValueEncoder.java | 8 +- dx/src/com/android/dx/merge/DexMerger.java | 48 ++++---- .../android/dx/merge/EncodedValueTransformer.java | 12 +- .../android/dx/util/ByteArrayAnnotatedOutput.java | 4 +- dx/src/com/android/dx/util/DexReader.java | 131 ++++++++++++++++----- dx/src/com/android/dx/util/DexWriter.java | 6 +- dx/src/com/android/dx/util/Mutf8.java | 2 + dx/src/com/android/dx/util/Output.java | 4 +- 14 files changed, 171 insertions(+), 99 deletions(-) diff --git a/dx/src/com/android/dx/dex/file/CatchStructs.java b/dx/src/com/android/dx/dex/file/CatchStructs.java index e07ec294e..8b0f1bdd7 100644 --- a/dx/src/com/android/dx/dex/file/CatchStructs.java +++ b/dx/src/com/android/dx/dex/file/CatchStructs.java @@ -19,8 +19,6 @@ package com.android.dx.dex.file; import com.android.dx.dex.code.CatchHandlerList; import com.android.dx.dex.code.CatchTable; import com.android.dx.dex.code.DalvCode; -import com.android.dx.rop.cst.CstType; -import com.android.dx.rop.type.Type; import com.android.dx.util.AnnotatedOutput; import com.android.dx.util.ByteArrayAnnotatedOutput; import com.android.dx.util.Hex; @@ -141,7 +139,7 @@ public final class CatchStructs { // Write out the handlers "header" consisting of its size in entries. encodedHandlerHeaderSize = - out.writeUnsignedLeb128(handlerOffsets.size()); + out.writeUleb128(handlerOffsets.size()); // Now write the lists out in order, noting the offset of each. for (Map.Entry mapping : @@ -155,21 +153,21 @@ public final class CatchStructs { if (catchesAll) { // A size <= 0 means that the list ends with a catch-all. - out.writeSignedLeb128(-(listSize - 1)); + out.writeSleb128(-(listSize - 1)); listSize--; } else { - out.writeSignedLeb128(listSize); + out.writeSleb128(listSize); } for (int i = 0; i < listSize; i++) { CatchHandlerList.Entry entry = list.get(i); - out.writeUnsignedLeb128( + out.writeUleb128( typeIds.indexOf(entry.getExceptionType())); - out.writeUnsignedLeb128(entry.getHandler()); + out.writeUleb128(entry.getHandler()); } if (catchesAll) { - out.writeUnsignedLeb128(list.get(listSize).getHandler()); + out.writeUleb128(list.get(listSize).getHandler()); } } diff --git a/dx/src/com/android/dx/dex/file/ClassDataItem.java b/dx/src/com/android/dx/dex/file/ClassDataItem.java index 275ae9980..e9ae18ba3 100644 --- a/dx/src/com/android/dx/dex/file/ClassDataItem.java +++ b/dx/src/com/android/dx/dex/file/ClassDataItem.java @@ -23,15 +23,12 @@ import com.android.dx.rop.cst.CstType; import com.android.dx.rop.cst.Zeroes; import com.android.dx.util.ByteArrayAnnotatedOutput; import com.android.dx.util.AnnotatedOutput; -import com.android.dx.util.Hex; import com.android.dx.util.Writers; import java.io.PrintWriter; import java.io.Writer; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; -import java.util.List; import java.util.HashMap; /** @@ -379,7 +376,7 @@ public final class ClassDataItem extends OffsettedItem { size)); } - out.writeUnsignedLeb128(size); + out.writeUleb128(size); } /** diff --git a/dx/src/com/android/dx/dex/file/DebugInfoEncoder.java b/dx/src/com/android/dx/dex/file/DebugInfoEncoder.java index 08b663793..d9d4ebcdc 100644 --- a/dx/src/com/android/dx/dex/file/DebugInfoEncoder.java +++ b/dx/src/com/android/dx/dex/file/DebugInfoEncoder.java @@ -376,7 +376,7 @@ public final class DebugInfoEncoder { PositionList.Entry entry = sortedPositions.get(0); line = entry.getPosition().getLine(); } - output.writeUnsignedLeb128(line); + output.writeUleb128(line); if (annotate) { annotate(output.getCursor() - mark, "line_start: " + line); @@ -403,7 +403,7 @@ public final class DebugInfoEncoder { // Write out the number of parameter entries that will follow. mark = output.getCursor(); - output.writeUnsignedLeb128(szParamTypes); + output.writeUleb128(szParamTypes); if (annotate) { annotate(output.getCursor() - mark, @@ -638,10 +638,10 @@ public final class DebugInfoEncoder { */ private void emitStringIndex(CstUtf8 string) throws IOException { if ((string == null) || (file == null)) { - output.writeUnsignedLeb128(0); + output.writeUleb128(0); } else { - output.writeUnsignedLeb128( - 1 + file.getStringIds().indexOf(string)); + output.writeUleb128( + 1 + file.getStringIds().indexOf(string)); } if (DEBUG) { @@ -659,10 +659,10 @@ public final class DebugInfoEncoder { */ private void emitTypeIndex(CstType type) throws IOException { if ((type == null) || (file == null)) { - output.writeUnsignedLeb128(0); + output.writeUleb128(0); } else { - output.writeUnsignedLeb128( - 1 + file.getTypeIds().indexOf(type)); + output.writeUleb128( + 1 + file.getTypeIds().indexOf(type)); } if (DEBUG) { @@ -748,7 +748,7 @@ public final class DebugInfoEncoder { int mark = output.getCursor(); output.writeByte(DBG_END_LOCAL); - output.writeUnsignedLeb128(entry.getRegister()); + output.writeUleb128(entry.getRegister()); if (annotateTo != null || debugPrint != null) { annotate(output.getCursor() - mark, @@ -851,7 +851,7 @@ public final class DebugInfoEncoder { int mark = output.getCursor(); output.writeByte(DBG_ADVANCE_LINE); - output.writeSignedLeb128(deltaLines); + output.writeSleb128(deltaLines); line += deltaLines; if (annotateTo != null || debugPrint != null) { @@ -875,7 +875,7 @@ public final class DebugInfoEncoder { int mark = output.getCursor(); output.writeByte(DBG_ADVANCE_PC); - output.writeUnsignedLeb128(deltaAddress); + output.writeUleb128(deltaAddress); address += deltaAddress; if (annotateTo != null || debugPrint != null) { @@ -903,7 +903,7 @@ public final class DebugInfoEncoder { "Signed value where unsigned required: " + n); } - output.writeUnsignedLeb128(n); + output.writeUleb128(n); } /** diff --git a/dx/src/com/android/dx/dex/file/EncodedField.java b/dx/src/com/android/dx/dex/file/EncodedField.java index f2a8184a8..d9724791d 100644 --- a/dx/src/com/android/dx/dex/file/EncodedField.java +++ b/dx/src/com/android/dx/dex/file/EncodedField.java @@ -146,8 +146,8 @@ public final class EncodedField extends EncodedMember AccessFlags.fieldString(accessFlags)); } - out.writeUnsignedLeb128(diff); - out.writeUnsignedLeb128(accessFlags); + out.writeUleb128(diff); + out.writeUleb128(accessFlags); return fieldIdx; } diff --git a/dx/src/com/android/dx/dex/file/EncodedMethod.java b/dx/src/com/android/dx/dex/file/EncodedMethod.java index 1b0770f49..e707de48d 100644 --- a/dx/src/com/android/dx/dex/file/EncodedMethod.java +++ b/dx/src/com/android/dx/dex/file/EncodedMethod.java @@ -187,9 +187,9 @@ public final class EncodedMethod extends EncodedMember " code_off: " + Hex.u4(codeOff)); } - out.writeUnsignedLeb128(diff); - out.writeUnsignedLeb128(accessFlags); - out.writeUnsignedLeb128(codeOff); + out.writeUleb128(diff); + out.writeUleb128(accessFlags); + out.writeUleb128(codeOff); return methodIdx; } diff --git a/dx/src/com/android/dx/dex/file/StringDataItem.java b/dx/src/com/android/dx/dex/file/StringDataItem.java index 80dbced8c..3752cb283 100644 --- a/dx/src/com/android/dx/dex/file/StringDataItem.java +++ b/dx/src/com/android/dx/dex/file/StringDataItem.java @@ -78,7 +78,7 @@ public final class StringDataItem extends OffsettedItem { out.annotate(bytes.size() + 1, value.toQuoted()); } - out.writeUnsignedLeb128(utf16Size); + out.writeUleb128(utf16Size); out.write(bytes); out.writeByte(0); } diff --git a/dx/src/com/android/dx/dex/file/ValueEncoder.java b/dx/src/com/android/dx/dex/file/ValueEncoder.java index fba64a726..7a608e075 100644 --- a/dx/src/com/android/dx/dex/file/ValueEncoder.java +++ b/dx/src/com/android/dx/dex/file/ValueEncoder.java @@ -277,7 +277,7 @@ public final class ValueEncoder { out.annotate(" size: " + Hex.u4(size)); } - out.writeUnsignedLeb128(size); + out.writeUleb128(size); for (int i = 0; i < size; i++) { Constant cst = list.get(i); @@ -319,7 +319,7 @@ public final class ValueEncoder { type.toHuman()); } - out.writeUnsignedLeb128(typeIds.indexOf(annotation.getType())); + out.writeUleb128(typeIds.indexOf(annotation.getType())); Collection pairs = annotation.getNameValuePairs(); int size = pairs.size(); @@ -328,7 +328,7 @@ public final class ValueEncoder { out.annotate(" size: " + Hex.u4(size)); } - out.writeUnsignedLeb128(size); + out.writeUleb128(size); int at = 0; for (NameValuePair pair : pairs) { @@ -343,7 +343,7 @@ public final class ValueEncoder { name.toHuman()); } - out.writeUnsignedLeb128(nameIdx); + out.writeUleb128(nameIdx); if (annotates) { out.annotate(" value: " + constantToHuman(value)); diff --git a/dx/src/com/android/dx/merge/DexMerger.java b/dx/src/com/android/dx/merge/DexMerger.java index cedefc71e..54adff243 100644 --- a/dx/src/com/android/dx/merge/DexMerger.java +++ b/dx/src/com/android/dx/merge/DexMerger.java @@ -524,17 +524,17 @@ public final class DexMerger { private void transformClassData(DexReader in, IndexMap indexMap) throws IOException { contentsOut.classDatas.size++; - int staticFieldsSize = in.readUnsignedLeb128(); - classDataItemWriter.writeUnsignedLeb128(staticFieldsSize); + int staticFieldsSize = in.readUleb128(); + classDataItemWriter.writeUleb128(staticFieldsSize); - int instanceFieldsSize = in.readUnsignedLeb128(); - classDataItemWriter.writeUnsignedLeb128(instanceFieldsSize); + int instanceFieldsSize = in.readUleb128(); + classDataItemWriter.writeUleb128(instanceFieldsSize); - int directMethodsSize = in.readUnsignedLeb128(); - classDataItemWriter.writeUnsignedLeb128(directMethodsSize); + int directMethodsSize = in.readUleb128(); + classDataItemWriter.writeUleb128(directMethodsSize); - int virtualMethodsSize = in.readUnsignedLeb128(); - classDataItemWriter.writeUnsignedLeb128(virtualMethodsSize); + int virtualMethodsSize = in.readUleb128(); + classDataItemWriter.writeUleb128(virtualMethodsSize); transformEncodedFields(in, indexMap, staticFieldsSize); transformEncodedFields(in, indexMap, instanceFieldsSize); @@ -548,12 +548,12 @@ public final class DexMerger { int inFieldIndex = 0; int lastOutFieldIndex = 0; for (int i = 0; i < count; i++) { - inFieldIndex += in.readUnsignedLeb128(); // field idx diff + inFieldIndex += in.readUleb128(); // field idx diff int outFieldIndex = indexMap.fieldIds[inFieldIndex]; - classDataItemWriter.writeUnsignedLeb128(outFieldIndex - lastOutFieldIndex); + classDataItemWriter.writeUleb128(outFieldIndex - lastOutFieldIndex); lastOutFieldIndex = outFieldIndex; - classDataItemWriter.writeUnsignedLeb128(in.readUnsignedLeb128()); // access flags + classDataItemWriter.writeUleb128(in.readUleb128()); // access flags } } @@ -565,20 +565,20 @@ public final class DexMerger { int inMethodIndex = 0; int lastOutMethodIndex = 0; for (int i = 0; i < count; i++) { - inMethodIndex += in.readUnsignedLeb128(); // method idx diff + inMethodIndex += in.readUleb128(); // method idx diff int outMethodIndex = indexMap.methodIds[inMethodIndex]; - classDataItemWriter.writeUnsignedLeb128(outMethodIndex - lastOutMethodIndex); + classDataItemWriter.writeUleb128(outMethodIndex - lastOutMethodIndex); lastOutMethodIndex = outMethodIndex; - classDataItemWriter.writeUnsignedLeb128(in.readUnsignedLeb128()); // access flags + classDataItemWriter.writeUleb128(in.readUleb128()); // access flags - int codeOff = in.readUnsignedLeb128(); // code off + int codeOff = in.readUleb128(); // code off if (codeOff == 0) { - classDataItemWriter.writeUnsignedLeb128(0); + classDataItemWriter.writeUleb128(0); } else { int inPosition = in.getPosition(); codeItemWriter.alignToFourBytes(); - classDataItemWriter.writeUnsignedLeb128(codeItemWriter.getCursor()); + classDataItemWriter.writeUleb128(codeItemWriter.getCursor()); in.seek(codeOff); transformCodeItem(in, indexMap); @@ -636,8 +636,8 @@ public final class DexMerger { private void transformEncodedCatchHandlerList(DexReader in, IndexMap indexMap) throws IOException { - int size = in.readUnsignedLeb128(); // size - codeItemWriter.writeUnsignedLeb128(size); + int size = in.readUleb128(); // size + codeItemWriter.writeUleb128(size); for (int i = 0; i < size; i++) { transformEncodedCatchHandler(in, indexMap); @@ -645,17 +645,17 @@ public final class DexMerger { } private void transformEncodedCatchHandler(DexReader in, IndexMap indexMap) throws IOException { - int size = in.readSignedLeb128(); // size - codeItemWriter.writeSignedLeb128(size); + int size = in.readSleb128(); // size + codeItemWriter.writeSleb128(size); int handlersCount = Math.abs(size); for (int i = 0; i < handlersCount; i++) { - codeItemWriter.writeUnsignedLeb128(indexMap.typeIds[in.readUnsignedLeb128()]); // type idx - codeItemWriter.writeUnsignedLeb128(in.readUnsignedLeb128()); // addr + codeItemWriter.writeUleb128(indexMap.typeIds[in.readUleb128()]); // type idx + codeItemWriter.writeUleb128(in.readUleb128()); // addr } if (size <= 0) { - codeItemWriter.writeUnsignedLeb128(in.readUnsignedLeb128()); // catch all addr + codeItemWriter.writeUleb128(in.readUleb128()); // catch all addr } } diff --git a/dx/src/com/android/dx/merge/EncodedValueTransformer.java b/dx/src/com/android/dx/merge/EncodedValueTransformer.java index 480370dcc..aed59e828 100644 --- a/dx/src/com/android/dx/merge/EncodedValueTransformer.java +++ b/dx/src/com/android/dx/merge/EncodedValueTransformer.java @@ -34,21 +34,21 @@ public final class EncodedValueTransformer { } public void transformArray() throws IOException { - int size = in.readUnsignedLeb128(); // size - out.writeUnsignedLeb128(size); + int size = in.readUleb128(); // size + out.writeUleb128(size); for (int i = 0; i < size; i++) { transformValue(); } } public void transformAnnotation() throws IOException { - out.writeUnsignedLeb128(indexMap.typeIds[in.readUnsignedLeb128()]); // type idx + out.writeUleb128(indexMap.typeIds[in.readUleb128()]); // type idx - int size = in.readUnsignedLeb128(); // size - out.writeUnsignedLeb128(size); + int size = in.readUleb128(); // size + out.writeUleb128(size); for (int i = 0; i < size; i++) { - out.writeUnsignedLeb128(indexMap.stringIds[in.readUnsignedLeb128()]); // name idx + out.writeUleb128(indexMap.stringIds[in.readUleb128()]); // name idx transformValue(); } } diff --git a/dx/src/com/android/dx/util/ByteArrayAnnotatedOutput.java b/dx/src/com/android/dx/util/ByteArrayAnnotatedOutput.java index e7ed9189f..def49a658 100644 --- a/dx/src/com/android/dx/util/ByteArrayAnnotatedOutput.java +++ b/dx/src/com/android/dx/util/ByteArrayAnnotatedOutput.java @@ -224,7 +224,7 @@ public final class ByteArrayAnnotatedOutput } /** {@inheritDoc} */ - public int writeUnsignedLeb128(int value) { + public int writeUleb128(int value) { if (stretchy) { ensureCapacity(cursor + 5); // pessimistic } @@ -234,7 +234,7 @@ public final class ByteArrayAnnotatedOutput } /** {@inheritDoc} */ - public int writeSignedLeb128(int value) { + public int writeSleb128(int value) { if (stretchy) { ensureCapacity(cursor + 5); // pessimistic } diff --git a/dx/src/com/android/dx/util/DexReader.java b/dx/src/com/android/dx/util/DexReader.java index 836bafb8d..ed8a68a41 100644 --- a/dx/src/com/android/dx/util/DexReader.java +++ b/dx/src/com/android/dx/util/DexReader.java @@ -20,26 +20,49 @@ import com.android.dx.dex.DexException; import com.android.dx.dex.SizeOf; import com.android.dx.dex.TableOfContents; import java.io.Closeable; +import java.io.DataInput; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; -import java.io.RandomAccessFile; +import java.util.Arrays; /** * All int offsets are unsigned. */ public final class DexReader implements Closeable { private final String name; - private final RandomAccessFile randomAccessFile; + private final byte[] fileContents; private final TableOfContents tableOfContents; + private int position = 0; + + private final DataInput asDataInput = new DataInputStub() { + public byte readByte() throws IOException { + return DexReader.this.readByte(); + } + }; /** * Creates a new DexReader that reads ints and shorts in little-endian byte * order. */ public DexReader(File file) throws IOException { - this.name = file.getPath(); - this.randomAccessFile = new RandomAccessFile(file, "r"); - this.tableOfContents = new TableOfContents(this); + name = file.getPath(); + + FileInputStream in = new FileInputStream(file); + int length = (int) file.length(); + fileContents = new byte[length]; + + int count = 0; + while (count < length) { + int bytesRead = in.read(fileContents, count, length - count); + if (bytesRead == -1) { + throw new IOException("Expected " + length + " bytes but was " + count); + } + count += bytesRead; + } + in.close(); + + tableOfContents = new TableOfContents(this); } public TableOfContents getTableOfContents() { @@ -47,34 +70,38 @@ public final class DexReader implements Closeable { } public int getPosition() throws IOException { - return (int) randomAccessFile.getFilePointer(); + return position; } public void seek(int offset) throws IOException { - randomAccessFile.seek(offset & 0xFFFFFFFFL); + position = offset; } - public void close() throws IOException { - randomAccessFile.close(); - } + public void close() throws IOException {} public int readInt() throws IOException { - int v = randomAccessFile.readInt(); - return Integer.reverseBytes(v); + int result = (fileContents[position] & 0xff) + | (fileContents[position + 1] & 0xff) << 8 + | (fileContents[position + 2] & 0xff) << 16 + | (fileContents[position + 3] & 0xff) << 24; + position += 4; + return result; } public short readShort() throws IOException { - short v = randomAccessFile.readShort(); - return Short.reverseBytes(v); + int result = (fileContents[position] & 0xff) + | (fileContents[position + 1] & 0xff) << 8; + position += 2; + return (short) result; } public byte readByte() throws IOException { - return randomAccessFile.readByte(); + return (byte) (fileContents[position++] & 0xff); } public byte[] readByteArray(int length) throws IOException { - byte[] result = new byte[length]; - randomAccessFile.readFully(result); + byte[] result = Arrays.copyOfRange(fileContents, position, position + length); + position += length; return result; } @@ -86,32 +113,32 @@ public final class DexReader implements Closeable { return result; } - public int readUnsignedLeb128() throws IOException { - return Leb128Utils.readUnsignedLeb128(randomAccessFile); + public int readUleb128() throws IOException { + return Leb128Utils.readUnsignedLeb128(asDataInput); } - public int readSignedLeb128() throws IOException { - return Leb128Utils.readSignedLeb128(randomAccessFile); + public int readSleb128() throws IOException { + return Leb128Utils.readSignedLeb128(asDataInput); } public short[] readTypeList(int offset) throws IOException { if (offset == 0) { return new short[0]; } - long position = randomAccessFile.getFilePointer(); - randomAccessFile.seek(offset); + int savedPosition = position; + position = offset; int size = readInt(); short[] parameters = new short[size]; for (int i = 0; i < size; i++) { parameters[i] = readShort(); } - randomAccessFile.seek(position); + position = savedPosition; return parameters; } public String readStringDataItem() throws IOException { - int expectedLength = readUnsignedLeb128(); - String result = Mutf8.decode(randomAccessFile, new char[expectedLength]); + int expectedLength = readUleb128(); + String result = Mutf8.decode(asDataInput, new char[expectedLength]); if (result.length() != expectedLength) { throw new DexException("Declared length " + expectedLength + " doesn't match decoded " + "length of " + result.length()); @@ -123,16 +150,64 @@ public final class DexReader implements Closeable { * Reads a string at the given index. This method does not disturb the seek position. */ public String readString(int index) throws IOException { - long position = randomAccessFile.getFilePointer(); + int savedPosition = position; seek(tableOfContents.stringIds.off + (index * SizeOf.STRING_ID_ITEM)); int stringDataOff = readInt(); seek(stringDataOff); String result = readStringDataItem(); - randomAccessFile.seek(position); + position = savedPosition; return result; } @Override public String toString() { return name; } + + private static class DataInputStub implements DataInput { + public byte readByte() throws IOException { + throw new UnsupportedOperationException(); + } + public void readFully(byte[] buffer) throws IOException { + throw new UnsupportedOperationException(); + } + public void readFully(byte[] buffer, int offset, int count) throws IOException { + throw new UnsupportedOperationException(); + } + public int skipBytes(int i) throws IOException { + throw new UnsupportedOperationException(); + } + public boolean readBoolean() throws IOException { + throw new UnsupportedOperationException(); + } + public int readUnsignedByte() throws IOException { + throw new UnsupportedOperationException(); + } + public short readShort() throws IOException { + throw new UnsupportedOperationException(); + } + public int readUnsignedShort() throws IOException { + throw new UnsupportedOperationException(); + } + public char readChar() throws IOException { + throw new UnsupportedOperationException(); + } + public int readInt() throws IOException { + throw new UnsupportedOperationException(); + } + public long readLong() throws IOException { + throw new UnsupportedOperationException(); + } + public float readFloat() throws IOException { + throw new UnsupportedOperationException(); + } + public double readDouble() throws IOException { + throw new UnsupportedOperationException(); + } + public String readLine() throws IOException { + throw new UnsupportedOperationException(); + } + public String readUTF() throws IOException { + throw new UnsupportedOperationException(); + } + } } diff --git a/dx/src/com/android/dx/util/DexWriter.java b/dx/src/com/android/dx/util/DexWriter.java index 252b7e5fb..2651d4d55 100644 --- a/dx/src/com/android/dx/util/DexWriter.java +++ b/dx/src/com/android/dx/util/DexWriter.java @@ -159,19 +159,19 @@ public final class DexWriter implements Closeable { bufferedByteCount += 4; } - public void writeUnsignedLeb128(int i) throws IOException { + public void writeUleb128(int i) throws IOException { ensureCapacity(5); bufferedByteCount += Leb128Utils.writeUnsignedLeb128(buffer, bufferedByteCount, i); } - public void writeSignedLeb128(int i) throws IOException { + public void writeSleb128(int i) throws IOException { ensureCapacity(5); bufferedByteCount += Leb128Utils.writeSignedLeb128(buffer, bufferedByteCount, i); } public void writeStringDataItem(String value) throws IOException { int length = value.length(); - writeUnsignedLeb128(length); + writeUleb128(length); write(Mutf8.encode(value)); writeByte(0); } diff --git a/dx/src/com/android/dx/util/Mutf8.java b/dx/src/com/android/dx/util/Mutf8.java index dcf585f86..ffa43a502 100644 --- a/dx/src/com/android/dx/util/Mutf8.java +++ b/dx/src/com/android/dx/util/Mutf8.java @@ -22,6 +22,8 @@ import java.io.UTFDataFormatException; /** * Modified UTF-8 as described in the dex file format spec. + * + *

Derived from libcore's MUTF-8 encoder at java.nio.charset.ModifiedUtf8. */ public final class Mutf8 { private Mutf8() {} diff --git a/dx/src/com/android/dx/util/Output.java b/dx/src/com/android/dx/util/Output.java index 402fa83ba..12eaa4c6f 100644 --- a/dx/src/com/android/dx/util/Output.java +++ b/dx/src/com/android/dx/util/Output.java @@ -75,7 +75,7 @@ public interface Output { * @param value value to write, treated as an unsigned value * @return {@code 1..5;} the number of bytes actually written */ - public int writeUnsignedLeb128(int value); + public int writeUleb128(int value); /** * Writes a DWARFv3-style unsigned LEB128 integer. For details, @@ -85,7 +85,7 @@ public interface Output { * @param value value to write * @return {@code 1..5;} the number of bytes actually written */ - public int writeSignedLeb128(int value); + public int writeSleb128(int value); /** * Writes a {@link ByteArray} to this instance. -- cgit v1.2.3