diff options
author | Brian Carlstrom <bdc@google.com> | 2013-04-29 16:30:16 -0700 |
---|---|---|
committer | Brian Carlstrom <bdc@google.com> | 2013-04-30 14:50:12 -0700 |
commit | df60589435658db76be61708bfa66515ffba40af (patch) | |
tree | 8993ef964f31268b55ad763a76ef93247784ea2a /dex/src/main | |
parent | 9252b359d9dcc775f9b0ac0b73c227d855c4a09d (diff) | |
download | libcore-df60589435658db76be61708bfa66515ffba40af.zip libcore-df60589435658db76be61708bfa66515ffba40af.tar.gz libcore-df60589435658db76be61708bfa66515ffba40af.tar.bz2 |
Fix Dex.create(ByteBuffer) to not allocate a byte[] to check magic
(cherry picked from commit 34c119762d55411ec2451b38f4782b923217aa2c)
Change-Id: I968762e1962fb720bbcf0c2866f17cd0500a9e54
Diffstat (limited to 'dex/src/main')
-rw-r--r-- | dex/src/main/java/com/android/dex/Dex.java | 36 |
1 files changed, 19 insertions, 17 deletions
diff --git a/dex/src/main/java/com/android/dex/Dex.java b/dex/src/main/java/com/android/dex/Dex.java index c4181b0..3fa5ab5 100644 --- a/dex/src/main/java/com/android/dex/Dex.java +++ b/dex/src/main/java/com/android/dex/Dex.java @@ -123,7 +123,11 @@ public final class Dex { * {@code data} after using it to create a dex buffer. */ public Dex(byte[] data) throws IOException { - this.data = ByteBuffer.wrap(data); + this(ByteBuffer.wrap(data)); + } + + private Dex(ByteBuffer data) throws IOException { + this.data = data; this.data.order(ByteOrder.LITTLE_ENDIAN); this.tableOfContents.readFrom(this); } @@ -169,24 +173,22 @@ public final class Dex { * transfers ownership of {@code bytes} to the returned Dex: it is an error * to access the buffer after calling this method. */ - public static Dex create(ByteBuffer bytes) throws IOException { - bytes.order(ByteOrder.LITTLE_ENDIAN); + public static Dex create(ByteBuffer data) throws IOException { + data.order(ByteOrder.LITTLE_ENDIAN); // 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); - 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); + if (data.get(0) == 'd' + && data.get(1) == 'e' + && data.get(2) == 'y' + && data.get(3) == '\n') { + data.position(8); + int offset = data.getInt(); + int length = data.getInt(); + data.position(offset); + data.limit(offset + length); + data = data.slice(); + } + return new Dex(data); } |