diff options
author | Brian Carlstrom <bdc@google.com> | 2013-04-29 15:00:29 -0700 |
---|---|---|
committer | Brian Carlstrom <bdc@google.com> | 2013-04-30 14:02:10 -0700 |
commit | c69e85bfb7ddd6c4b0023ca29eeb0416087f9a8b (patch) | |
tree | 38d6e2c8161a4367f9b0c206f134d73278ce6da3 /dex | |
parent | 2bea5ee615b0f4add658d5660bd81c5145a0d05e (diff) | |
download | libcore-c69e85bfb7ddd6c4b0023ca29eeb0416087f9a8b.zip libcore-c69e85bfb7ddd6c4b0023ca29eeb0416087f9a8b.tar.gz libcore-c69e85bfb7ddd6c4b0023ca29eeb0416087f9a8b.tar.bz2 |
Add Dex.create(ByteBuffer)
(cherry picked from commit aca7a4ba7597be831164d94d787f383b8d4ac2fd)
Change-Id: Id517f2ccda31a20e28fe63005120e4e975935980
Diffstat (limited to 'dex')
-rw-r--r-- | dex/src/main/java/com/android/dex/Dex.java | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/dex/src/main/java/com/android/dex/Dex.java b/dex/src/main/java/com/android/dex/Dex.java index f179b99..8d22576 100644 --- a/dex/src/main/java/com/android/dex/Dex.java +++ b/dex/src/main/java/com/android/dex/Dex.java @@ -27,6 +27,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UTFDataFormatException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; import java.util.AbstractList; import java.util.Arrays; import java.util.Collections; @@ -153,6 +155,30 @@ public final class Dex { } } + /** + * Creates a new dex from the contents of {@code bytes}. This API supports + * both {@code .dex} and {@code .odex} input. + */ + public static Dex create(ByteBuffer bytes) throws IOException { + // if it's an .odex file, set position and limit to the .dex section + if (bytes.get(0) == 'd' + && bytes.get(1) == 'e' + && bytes.get(2) == 'y' + && bytes.get(3) == '\n') { + bytes.position(8); + bytes.order(ByteOrder.nativeOrder()); + int offset = bytes.getInt(); + int length = bytes.getInt(); + bytes.position(offset); + bytes.limit(offset + length); + } + + // TODO: don't copy the bytes; use the ByteBuffer directly + byte[] data = new byte[bytes.remaining()]; + bytes.get(data); + return new Dex(data); + } + private void loadFrom(InputStream in) throws IOException { ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); byte[] buffer = new byte[8192]; |