ctf: Add get(byte[]) to BitBuffer
authorMatthew Khouzam <matthew.khouzam@ericsson.com>
Wed, 7 May 2014 03:07:23 +0000 (23:07 -0400)
committerMatthew Khouzam <matthew.khouzam@ericsson.com>
Tue, 17 Jun 2014 18:31:54 +0000 (14:31 -0400)
This adds a bulk reader for data.

Change-Id: I8a43b292b8240d070a94473861b7aca711474661
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/26083
Tested-by: Hudson CI
Reviewed-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/io/BitBufferTest.java
org.eclipse.linuxtools.ctf.core/META-INF/MANIFEST.MF
org.eclipse.linuxtools.ctf.core/pom.xml
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/io/BitBuffer.java

index 98d363cc4f65001ad8c3974e84a4055d4e344f2f..e6e39e47c5b2948019b4d8a70c9b79d51d18eb7a 100644 (file)
@@ -17,6 +17,7 @@ import static org.junit.Assert.assertNotNull;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 import org.junit.Before;
@@ -35,7 +36,9 @@ public class BitBufferTest {
 
     /**
      * Perform pre-test initialization.
-     * @throws CTFReaderException An error that cannot happen (position is under 128)
+     *
+     * @throws CTFReaderException
+     *             An error that cannot happen (position is under 128)
      */
     @Before
     public void setUp() throws CTFReaderException {
@@ -188,4 +191,43 @@ public class BitBufferTest {
         ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
         fixture.setByteOrder(byteOrder);
     }
+
+    /**
+     * Test the get function
+     */
+    @Test
+    public void testGetBytes() {
+        @NonNull byte[] data = new byte[2];
+        ByteBuffer bb = ByteBuffer.allocate(10);
+        bb.put((byte) 0);
+        bb.put((byte) 1);
+        bb.put((byte) 1);
+        bb.put((byte) 0);
+        fixture = new BitBuffer(bb);
+        fixture.get(data);
+        assertEquals(0, data[0]);
+        assertEquals(1, data[1]);
+        fixture.get(data);
+        assertEquals(1, data[0]);
+        assertEquals(0, data[1]);
+    }
+
+    /**
+     * Test the get function
+     *
+     * @throws CTFReaderException
+     *             won't happen but we seek in a buffer
+     */
+    @Test
+    public void testGetBytesMiddle() throws CTFReaderException {
+        @NonNull byte[] data = new byte[5];
+        // this string has been carefully selected and tested... don't change
+        // the string and expect the result to be the same.
+        ByteBuffer bb = ByteBuffer.wrap(new String("hello world").getBytes());
+        fixture = new BitBuffer(bb);
+        fixture.position(6 * 8);
+        fixture.get(data);
+        String actual = new String(data);
+        assertEquals("world", actual);
+    }
 }
\ No newline at end of file
index 1bbbc192f5fe7b2a07d4ee11cc6d09da7f2cda56..04087070b65f3488fbe358ed8444c66c37e2e3df 100644 (file)
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
 Bundle-ManifestVersion: 2
 Bundle-Name: %Bundle-Name
 Bundle-Vendor: %Bundle-Vendor
-Bundle-Version: 3.0.0.qualifier
+Bundle-Version: 3.1.0.qualifier
 Bundle-Localization: plugin
 Bundle-SymbolicName: org.eclipse.linuxtools.ctf.core;singleton:=true
 Bundle-Activator: org.eclipse.linuxtools.internal.ctf.core.Activator
index 9b55e9b85bcfb38b33bc0dd9113acd0e4c0b9051..d7829e8d663bb5ba4048522ce0eedb652d6083a1 100644 (file)
@@ -20,7 +20,7 @@
   <name>Linux Tools CTF Core Plug-in</name>
   <groupId>org.eclipse.linuxtools.ctf</groupId>
   <artifactId>org.eclipse.linuxtools.ctf.core</artifactId>
-  <version>3.0.0-SNAPSHOT</version>
+  <version>3.1.0-SNAPSHOT</version>
   <packaging>eclipse-plugin</packaging>
 
  <build>
index d7ed7b3e1f4b3a03486f0efcb7a7278ae1097809..0ccf8c2eb705d6f5966311d47c670f946bfdaf5a 100644 (file)
 
 package org.eclipse.linuxtools.ctf.core.event.io;
 
+import java.nio.BufferUnderflowException;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 
 /**
@@ -50,6 +52,10 @@ public final class BitBuffer {
     // ------------------------------------------------------------------------
 
     private ByteBuffer fBuffer;
+
+    /**
+     * Bit-buffer's position, maximum value = Integer.MAX_VALUE * 8
+     */
     private long fPosition;
     private ByteOrder fByteOrder;
 
@@ -169,6 +175,28 @@ public final class BitBuffer {
         return (signed ? retVal : (retVal & 0xFFFFFFFFL));
     }
 
+    /**
+     * Relative bulk <i>get</i> method.
+     *
+     * <p>
+     * This method transfers <strong>bytes</strong> from this buffer into the
+     * given destination array. This method currently only supports reads
+     * aligned to 8 bytes. It is up to the developer to shift the bits in
+     * post-processing to do unaligned reads.
+     *
+     * @param dst
+     *            the bytes to write to
+     * @throws BufferUnderflowException
+     *             - If there are fewer than length bytes remaining in this
+     *             buffer
+     * @since 3.1
+     */
+    public void get(@NonNull byte[] dst) {
+        fBuffer.position((int) (fPosition / 8));
+        fBuffer.get(dst);
+        fPosition += dst.length * 8;
+    }
+
     /**
      * Relative <i>get</i> method for reading integer of <i>length</i> bits.
      *
This page took 0.029586 seconds and 5 git commands to generate.