changeset 4:1308ee8666a2

Add BufferUtil class and test. Contributed by Michael Starzinger.
author Mario Torre <neugens.limasoftware@gmail.com>
date Mon, 07 Mar 2011 20:30:39 +0100
parents 8ae08d25777e
children bfd49213d13c
files src/main/java/org/icedrobot/daneel/util/BufferUtil.java src/test/java/org/icedrobot/daneel/util/BufferUtilTest.java
diffstat 2 files changed, 97 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/icedrobot/daneel/util/BufferUtil.java	Mon Mar 07 20:30:39 2011 +0100
@@ -0,0 +1,56 @@
+/*
+ * Daneel - Dalvik to Java bytecode compiler
+ * Copyright (C) 2011  IcedRobot team
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package org.icedrobot.daneel.util;
+
+import java.nio.ByteBuffer;
+
+public class BufferUtil {
+
+    /**
+     * Reads an array of integers from the buffer's current position.
+     * @param buffer The byte buffer to read from.
+     * @param size The number of values to read.
+     * @return An array containing the read values.
+     */
+    public static int[] getInts(ByteBuffer buffer, int size) {
+        int[] ints = new int[size];
+        for (int i = 0; i < size; i++)
+            ints[i] = buffer.getInt();
+        return ints;
+    }
+
+    /**
+     * Reads a "Little-Endian Base 128" variable-length encoded value at the
+     * buffer's current position. A maximum of five bytes is read because only
+     * 32-bit values are encoded this way. For details about this encoding see
+     * the "Dalvik Executable Format" specification.
+     * @param buffer The byte buffer to read from.
+     * @return The decoded unsigned 32-bit value.
+     */
+    public static int getULEB128(ByteBuffer buffer) {
+        int result = 0;
+        for (int i = 0; i < 5; i++) {
+            byte b = buffer.get();
+            result |= ((b & 0x7f) << (7 * i));
+            if ((b & 0x80) == 0)
+                break;
+        }
+        return result;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/java/org/icedrobot/daneel/util/BufferUtilTest.java	Mon Mar 07 20:30:39 2011 +0100
@@ -0,0 +1,41 @@
+/*
+ * Daneel - Dalvik to Java bytecode compiler
+ * Copyright (C) 2011  IcedRobot team
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package org.icedrobot.daneel.util;
+
+import static org.junit.Assert.assertEquals;
+
+import java.nio.ByteBuffer;
+
+import org.junit.Test;
+
+public class BufferUtilTest {
+
+    private static final byte[] SEQ1 = new byte[] { 0 };
+    private static final byte[] SEQ2 = new byte[] { 1 };
+    private static final byte[] SEQ3 = new byte[] { 127 };
+    private static final byte[] SEQ4 = new byte[] { -128, 127 };
+
+    @Test
+    public void testGetULEB128() {
+        assertEquals(0, BufferUtil.getULEB128(ByteBuffer.wrap(SEQ1)));
+        assertEquals(1, BufferUtil.getULEB128(ByteBuffer.wrap(SEQ2)));
+        assertEquals(127, BufferUtil.getULEB128(ByteBuffer.wrap(SEQ3)));
+        assertEquals(16256, BufferUtil.getULEB128(ByteBuffer.wrap(SEQ4)));
+    }
+}