ctf: make BitBuffer's Constructor only take non-null ByteBuffers
authorMatthew Khouzam <matthew.khouzam@ericsson.com>
Wed, 18 Jun 2014 13:33:39 +0000 (09:33 -0400)
committerMatthew Khouzam <matthew.khouzam@ericsson.com>
Thu, 26 Jun 2014 17:34:35 +0000 (13:34 -0400)
Change-Id: I70490af35ea9d1ec559828d81fcb6b65b65d12ca
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/28685
Tested-by: Hudson CI
Reviewed-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
19 files changed:
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/event/CTFEventFieldTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/io/BitBufferIntTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/io/BitBufferTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/io/Util.java [new file with mode: 0644]
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/ArrayDeclaration2Test.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/ArrayDefinition2Test.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/EnumDeclarationTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerEndiannessTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/SequenceDeclaration2Test.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/SequenceDefinition2Test.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StringDeclarationTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StringDefinitionTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StructDeclarationTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StructDefinitionTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/VariantDeclarationTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/VariantDefinitionTest.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/io/BitBuffer.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFStreamInput.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFTrace.java

index b74d65fa3ece13ff343495b4367883478b28e2ab..a149e444832219952c4053171cdd1dab5459a95a 100644 (file)
@@ -43,7 +43,8 @@ import com.google.common.collect.ImmutableList;
 @SuppressWarnings("javadoc")
 public class CTFEventFieldTest {
 
-    @NonNull private static final String fieldName = "id";
+    @NonNull
+    private static final String fieldName = "id";
 
     /**
      * Run the CTFEventField parseField(Definition,String) method test.
@@ -78,7 +79,7 @@ public class CTFEventFieldTest {
                 });
 
         SequenceDeclaration sd = new SequenceDeclaration(lengthName, id);
-        ByteBuffer byb = ByteBuffer.allocate(1024);
+        ByteBuffer byb = testMemory(ByteBuffer.allocate(1024));
         for (int i = 0; i < 1024; i++) {
             byb.put((byte) i);
         }
@@ -88,6 +89,14 @@ public class CTFEventFieldTest {
         assertNotNull(fieldDef);
     }
 
+    @NonNull
+    private static ByteBuffer testMemory(ByteBuffer buffer) {
+        if (buffer == null) {
+            throw new IllegalStateException("Failed to allocate memory");
+        }
+        return buffer;
+    }
+
     /**
      * Run the CTFEventField parseField(Definition,String) method test.
      *
@@ -97,8 +106,7 @@ public class CTFEventFieldTest {
     public void testParseField_simple() throws CTFReaderException {
         final StringDeclaration elemType = new StringDeclaration();
         byte[] bytes = { 'T', 'e', 's', 't', '\0' };
-        ByteBuffer bb = ByteBuffer.wrap(bytes);
-
+        ByteBuffer bb = testMemory(ByteBuffer.wrap(bytes));
         Definition fieldDef = elemType.createDefinition(null, fieldName, new BitBuffer(bb));
 
         assertNotNull(fieldDef);
index bb6a5bb02cbe011b771e81f0de21ff1bf3d19812..4159ad71b75ecc0854a7312f7bf95a8b5dffa8e4 100644 (file)
@@ -41,7 +41,11 @@ public class BitBufferIntTest {
      */
     @Before
     public void setUp() throws CTFReaderException {
-        fixture = new BitBuffer(ByteBuffer.allocateDirect(128));
+        ByteBuffer allocateDirect = ByteBuffer.allocateDirect(128);
+        if (allocateDirect == null) {
+            throw new IllegalStateException("Failed to allocate memory");
+        }
+        fixture = new BitBuffer(allocateDirect);
         fixture.setByteOrder(ByteOrder.BIG_ENDIAN);
         fixture = createBuffer();
     }
@@ -55,7 +59,11 @@ public class BitBufferIntTest {
         for (int i = 0; i < j; i++) {
             bytes[i] = (byte) (i % 0xff);
         }
-        BitBuffer fixture = new BitBuffer(ByteBuffer.wrap(bytes));
+        ByteBuffer wrap = ByteBuffer.wrap(bytes);
+        if (wrap == null) {
+            throw new IllegalStateException("Failed to allocate memory");
+        }
+        BitBuffer fixture = new BitBuffer(wrap);
         fixture.position(1);
         return fixture;
     }
index 2f6757f0380d1f2276aed614c406287359e6c6f8..9a88de959e0f659b53b1d549ea78142c0d60f374 100644 (file)
@@ -42,7 +42,7 @@ public class BitBufferTest {
      */
     @Before
     public void setUp() throws CTFReaderException {
-        fixture = new BitBuffer(java.nio.ByteBuffer.allocateDirect(1));
+        fixture = new BitBuffer(Util.testMemory(ByteBuffer.allocateDirect(1)));
         fixture.setByteOrder(ByteOrder.BIG_ENDIAN);
         fixture.position(1);
     }
@@ -56,7 +56,7 @@ public class BitBufferTest {
 
         assertNotNull(result);
         assertEquals(0, result.position());
-        assertNotNull( result.getByteBuffer());
+        assertNotNull(result.getByteBuffer());
     }
 
     /**
@@ -64,9 +64,7 @@ public class BitBufferTest {
      */
     @Test
     public void testBitBuffer_fromByteBuffer() {
-        ByteBuffer buf = ByteBuffer.allocate(0);
-        BitBuffer result = new BitBuffer(buf);
-
+        BitBuffer result = new BitBuffer(Util.testMemory(ByteBuffer.allocate(0)));
         assertNotNull(result);
         assertEquals(0, result.position());
     }
@@ -205,8 +203,7 @@ public class BitBufferTest {
         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 = new BitBuffer(Util.testMemory(ByteBuffer.wrap(new String("hello world").getBytes())));
         fixture.position(6 * 8);
         fixture.get(data);
         String actual = new String(data);
diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/io/Util.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/io/Util.java
new file mode 100644 (file)
index 0000000..490ea9b
--- /dev/null
@@ -0,0 +1,42 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Matthew Khouzam - Initial API and implementation
+ ******************************************************************************/
+
+package org.eclipse.linuxtools.ctf.core.tests.io;
+
+import java.nio.ByteBuffer;
+
+import org.eclipse.jdt.annotation.NonNull;
+
+/**
+ * Helpers for the tests
+ *
+ * @author Matthew Khouzam
+ */
+public final class Util {
+
+    private Util() {
+    }
+
+    /**
+     * Wrapper to make sure the bytebuffer is not null
+     *
+     * @param buffer
+     *            a potentially null byte buffer
+     * @return a non-null byte buffer or an illegal state exception
+     */
+    @NonNull
+    public static ByteBuffer testMemory(ByteBuffer buffer) {
+        if (buffer == null) {
+            throw new IllegalStateException("Failed to alloc");
+        }
+        return buffer;
+    }
+}
index 0777ed5a53ee4897404ea9285cf365290eef4460..7fc30296b63d71928047b5e52c4741aabf6f50e8 100644 (file)
@@ -26,6 +26,7 @@ import org.eclipse.linuxtools.ctf.core.event.types.Encoding;
 import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration;
+import org.eclipse.linuxtools.ctf.core.tests.io.Util;
 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 import org.eclipse.linuxtools.internal.ctf.core.event.types.ArrayDeclaration;
 import org.junit.Before;
@@ -79,13 +80,14 @@ public class ArrayDeclaration2Test {
         IDefinitionScope definitionScope = null;
         AbstractArrayDefinition result;
         byte[] array = { 't', 'e', 's', 't', '\0', 't', 'h', 'i', 's', '\0' };
-        ByteBuffer byb = ByteBuffer.wrap(array);
-        BitBuffer bb = new BitBuffer(byb);
+        BitBuffer bb = new BitBuffer(Util.testMemory(ByteBuffer.wrap(array)));
         result = fixture.createDefinition(definitionScope, fieldName, bb);
 
         assertNotNull(result);
     }
 
+
+
     /**
      * Run the Declaration getElementType() method test.
      */
index 276d0959b2e32a27f45fcd23b5951e12ee108b24..93f7b0fd553cdf6b4fcab9d0d3e1fd84672a8a51 100644 (file)
@@ -31,6 +31,7 @@ import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
+import org.eclipse.linuxtools.ctf.core.tests.io.Util;
 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
 import org.eclipse.linuxtools.internal.ctf.core.event.types.ArrayDeclaration;
@@ -45,7 +46,7 @@ import org.junit.Test;
  */
 public class ArrayDefinition2Test {
 
-    @NonNull private CTFTrace trace = new CTFTrace();
+    private @NonNull CTFTrace trace = new CTFTrace();
     private ArrayDefinition charArrayFixture;
     private ArrayDefinition stringArrayFixture;
     private ArrayDefinition longArrayFixture;
@@ -91,8 +92,8 @@ public class ArrayDefinition2Test {
         return temp;
     }
 
-    private @NonNull
-    static List<Definition> createIntDefs(int size, int bits) {
+    @NonNull
+    private static List<Definition> createIntDefs(int size, int bits) {
         List<Definition> defs = new ArrayList<>(size);
         for (int i = 0; i < size; i++) {
             String content = "test" + i;
@@ -102,8 +103,8 @@ public class ArrayDefinition2Test {
         return defs;
     }
 
-    private @NonNull
-    static List<Definition> createDefs() {
+    @NonNull
+    private static List<Definition> createDefs() {
         int size = 4;
         List<Definition> defs = new ArrayList<>();
         for (int i = 0; i < size; i++) {
@@ -140,7 +141,7 @@ public class ArrayDefinition2Test {
 
         String fieldName = "";
         @SuppressWarnings("null")
-        ArrayDefinition result = new ArrayDefinition(declaration, definitionScope, fieldName , Arrays.asList(new Definition[0]));
+        ArrayDefinition result = new ArrayDefinition(declaration, definitionScope, fieldName, Arrays.asList(new Definition[0]));
         assertNotNull(result);
     }
 
@@ -180,7 +181,8 @@ public class ArrayDefinition2Test {
         assertNotNull(result);
     }
 
-    @NonNull private static IDefinitionScope getDefinitionScope() {
+    @NonNull
+    private static IDefinitionScope getDefinitionScope() {
         return new IDefinitionScope() {
 
             @Override
@@ -203,7 +205,7 @@ public class ArrayDefinition2Test {
      */
     @Test
     public void testRead_noDefs() throws CTFReaderException {
-        BitBuffer input = new BitBuffer(ByteBuffer.allocateDirect(128));
+        BitBuffer input = new BitBuffer(Util.testMemory(ByteBuffer.allocateDirect(128)));
         charArrayFixture.getDeclaration().createDefinition(null, "test", input);
     }
 
index cf9f940276b06b846fb333ca09428ea2a8602770..4a6e53df7e3dfc49e781dfb58a8afb1828a7f2ed 100644 (file)
@@ -25,6 +25,7 @@ import org.eclipse.linuxtools.ctf.core.event.types.Encoding;
 import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
+import org.eclipse.linuxtools.ctf.core.tests.io.Util;
 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 import org.junit.Before;
 import org.junit.Test;
@@ -90,8 +91,7 @@ public class EnumDeclarationTest {
         IDefinitionScope definitionScope = null;
         String fieldName = "";
         byte[] array = { 't', 'e', 's', 't', '\0', 't', 'h', 'i', 's', '\0' };
-        ByteBuffer byb = ByteBuffer.wrap(array);
-        BitBuffer bb = new BitBuffer(byb);
+        BitBuffer bb = new BitBuffer(Util.testMemory(ByteBuffer.wrap(array)));
 
         EnumDefinition result = fixture.createDefinition(definitionScope,
                 fieldName, bb);
index 9ce8bc81d57b4f5f4aeab95dcb5ce4202a8844c9..38eb823ed4c42316338116383f7076cb1633b9b4 100644 (file)
@@ -35,11 +35,12 @@ import org.junit.Test;
  */
 public class IntegerEndiannessTest {
 
-    @NonNull private static final String name = "testInt";
-    @NonNull private static final String clockName = "clock";
+    private static final @NonNull String name = "testInt";
+    private static final @NonNull String clockName = "clock";
 
     private ByteBuffer bb;
-    @NonNull private BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocate(0));
+
+    private @NonNull BitBuffer input = new BitBuffer();
 
     /**
      * Set up the bit-buffer to be used
@@ -47,6 +48,10 @@ public class IntegerEndiannessTest {
     @Before
     public void setUp() {
         bb = java.nio.ByteBuffer.allocateDirect(8);
+        final ByteBuffer byb = bb;
+        if (byb == null) {
+            throw new IllegalStateException("Failed to allocate memory");
+        }
         bb.put((byte) 0xab);
         bb.put((byte) 0xcd);
         bb.put((byte) 0xef);
@@ -55,7 +60,8 @@ public class IntegerEndiannessTest {
         bb.put((byte) 0x56);
         bb.put((byte) 0x78);
         bb.put((byte) 0x9a);
-        input = new BitBuffer(bb);
+
+        input = new BitBuffer(byb);
     }
 
     /**
index 861f079b1c4434bfd519ee7d5aadc8eccdfaaab9..21dec6dcc62d0b178b7988d81431e8f37a010037 100644 (file)
@@ -55,6 +55,9 @@ public class SequenceDeclaration2Test {
         fixture = new SequenceDeclaration(FIELD_NAME, new StringDeclaration());
         byte array[] = { 't', 'e', 's', 't', '\0', 't', 'h', 'i', 's', '\0' };
         ByteBuffer byb = ByteBuffer.wrap(array);
+        if( byb == null){
+            throw new IllegalStateException("Failed to allocate memory");
+        }
         input = new BitBuffer(byb);
     }
 
index c981f48c28df7fa45671130ad0269d83984dfae1..8e74413342a5cc5a1d38cb08823b2cfa5caca7f8 100644 (file)
@@ -13,6 +13,7 @@ package org.eclipse.linuxtools.ctf.core.tests.types;
 
 import static org.junit.Assert.assertNotNull;
 
+import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 
 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
@@ -71,8 +72,11 @@ public class SequenceDefinition2Test {
         structDef = new StructDefinition(structDec, null, "x", wrap(lengthName), new Definition[] { new IntegerDefinition(id, null, lengthName, seqLen) });
 
         SequenceDeclaration sd = new SequenceDeclaration(lengthName, id);
-        BitBuffer input = new BitBuffer(
-                java.nio.ByteBuffer.allocateDirect(seqLen * len));
+        ByteBuffer allocateDirect = java.nio.ByteBuffer.allocateDirect(seqLen * len);
+        if( allocateDirect == null){
+            throw new IllegalStateException("Failed to allocate memory");
+        }
+        BitBuffer input = new BitBuffer(allocateDirect);
         for (int i = 0; i < seqLen; i++) {
             input.putInt(i);
         }
index 57c7015b8e94d4515c5a7607a49e60a70be84a12..1042ecfdeaffbd816760402c2ee3aac05004c19b 100644 (file)
@@ -72,13 +72,19 @@ public class StringDeclarationTest {
     /**
      * Run the StringDefinition createDefinition(DefinitionScope,String) method
      * test.
-     * @throws CTFReaderException out of buffer exception
+     *
+     * @throws CTFReaderException
+     *             out of buffer exception
      */
     @Test
     public void testCreateDefinition() throws CTFReaderException {
         IDefinitionScope definitionScope = null;
         String fieldName = "id";
-        BitBuffer bb = new BitBuffer(ByteBuffer.allocate(100));
+        ByteBuffer allocate = ByteBuffer.allocate(100);
+        if (allocate == null) {
+            throw new IllegalStateException("Failed to allocate memory");
+        }
+        BitBuffer bb = new BitBuffer(allocate);
         StringDefinition result = fixture.createDefinition(definitionScope,
                 fieldName, bb);
 
@@ -98,7 +104,7 @@ public class StringDeclarationTest {
         assertEquals(1, result.ordinal());
     }
 
-     /**
+    /**
      * Run the String toString() method test.
      */
     @Test
index 0b46d80fbf5ce4851e2d45f5e090237f7a5a358f..6a93c92643b112c81ae3f8722a3d97636bc95afd 100644 (file)
@@ -39,13 +39,17 @@ public class StringDefinitionTest {
     /**
      * Perform pre-test initialization.
      *
-     * @throws CTFReaderException won't happen
+     * @throws CTFReaderException
+     *             won't happen
      */
     @Before
     public void setUp() throws CTFReaderException {
         String name = "testString";
         StringDeclaration stringDec = new StringDeclaration();
         ByteBuffer byteBuffer = ByteBuffer.allocate(100);
+        if (byteBuffer == null) {
+            throw new IllegalStateException("Failed to allocate memory");
+        }
         BitBuffer bb = new BitBuffer(byteBuffer);
         byteBuffer.mark();
         testString = new String("testString");
index 98ebb77770627f286c782188deaf9a50e48396b8..3b7bd9d99faa15704bf1f3fa298c7b7f82e32229 100644 (file)
@@ -78,7 +78,11 @@ public class StructDeclarationTest {
     @Test
     public void testCreateDefinition() throws CTFReaderException {
         String fieldName = "";
-        BitBuffer bb = new BitBuffer(ByteBuffer.allocate(100));
+        ByteBuffer allocate = ByteBuffer.allocate(100);
+        if( allocate == null){
+            throw new IllegalStateException("Failed to allocate memory");
+        }
+        BitBuffer bb = new BitBuffer(allocate);
         StructDefinition result = fixture.createDefinition(null, fieldName, bb);
         assertNotNull(result);
     }
index 5183f971d96c3c6f9b9032183d95816c2e19ab55..bf18deec98d993427ee77082af048c4b0ee35caf 100644 (file)
@@ -31,6 +31,7 @@ import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition;
+import org.eclipse.linuxtools.ctf.core.tests.io.Util;
 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 import org.eclipse.linuxtools.internal.ctf.core.event.types.SequenceDeclaration;
 import org.junit.Before;
@@ -45,16 +46,16 @@ import org.junit.Test;
  */
 public class StructDefinitionTest {
 
-    @NonNull private static final String TEST_STRUCT_ID = "testStruct";
-    @NonNull private static final String ENUM_2 = "y";
-    @NonNull private static final String ENUM_1 = "x";
-    @NonNull private static final String TAG_ID = "Tag";
-    @NonNull private static final String INT_ID = "_id";
-    @NonNull private static final String STRING_ID = "_args";
-    @NonNull private static final String ENUM_ID = "_enumArgs";
-    @NonNull private static final String SEQUENCE_ID = "_seq";
-    @NonNull private static final String LENGTH_SEQ = "_len";
-    @NonNull private static final String VAR_FIELD_NAME = "SomeVariant";
+    private static final @NonNull String TEST_STRUCT_ID = "testStruct";
+    private static final @NonNull String ENUM_2 = "y";
+    private static final @NonNull String ENUM_1 = "x";
+    private static final @NonNull String TAG_ID = "Tag";
+    private static final @NonNull String INT_ID = "_id";
+    private static final @NonNull String STRING_ID = "_args";
+    private static final @NonNull String ENUM_ID = "_enumArgs";
+    private static final @NonNull String SEQUENCE_ID = "_seq";
+    private static final @NonNull String LENGTH_SEQ = "_len";
+    private static final @NonNull String VAR_FIELD_NAME = "SomeVariant";
 
     private StructDefinition fixture;
     private StructDefinition emptyStruct;
@@ -92,8 +93,7 @@ public class StructDefinitionTest {
         bytes[4] = 1;
         bytes[8] = 2;
         bytes[13] = 3;
-        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
-        BitBuffer bb = new BitBuffer(byteBuffer);
+        BitBuffer bb = new BitBuffer(Util.testMemory(ByteBuffer.wrap(bytes)));
         fixture = sDec.createDefinition(null, TEST_STRUCT_ID, bb);
         EnumDefinition eDef = tagDec.createDefinition(fixture, TAG_ID, bb);
         assertNotNull(eDef);
index 70e154b6385d3a626fd7aa74fdc965425faef04c..f37ce6b1e5727e82dc2f3d428e320d263149d0c0 100644 (file)
@@ -140,7 +140,11 @@ public class VariantDeclarationTest {
         fixture.addField("a", IntegerDeclaration.UINT_64B_DECL);
         IDefinitionScope definitionScope = createDefinitionScope();
         String fieldName = "";
-        BitBuffer bb = new BitBuffer(ByteBuffer.allocate(100));
+        ByteBuffer allocate = ByteBuffer.allocate(100);
+        if( allocate == null){
+            throw new IllegalStateException("Failed to allocate memory");
+        }
+        BitBuffer bb = new BitBuffer(allocate);
         VariantDefinition result = fixture.createDefinition(definitionScope, fieldName, bb);
 
         assertNotNull(result);
index da10efecd928234103b07a3e559f279c93819022..ca2c2a954928f190202934f14534236bbe35c234 100644 (file)
@@ -35,6 +35,7 @@ import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition;
+import org.eclipse.linuxtools.ctf.core.tests.io.Util;
 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 import org.eclipse.linuxtools.internal.ctf.core.event.types.ArrayDeclaration;
 import org.junit.Before;
@@ -107,7 +108,7 @@ public class VariantDefinitionTest {
         sDec.addField(VAR_FIELD_NAME, varDec);
         varDec.setTag(TAG_ID);
 
-        ByteBuffer byteBuffer = ByteBuffer.allocate(100);
+        final ByteBuffer byteBuffer = Util.testMemory(ByteBuffer.allocate(100));
         BitBuffer bb = new BitBuffer(byteBuffer);
         byteBuffer.mark();
         byteBuffer.putInt(1);
index 89dabb260659172e7cdc89613d13ca756c20f8a5..0556371273f198fda7582458eed24df8bdf6cdbf 100644 (file)
@@ -66,8 +66,9 @@ public final class BitBuffer {
     /**
      * Default constructor, makes a big-endian buffer
      */
+    @SuppressWarnings("null")
     public BitBuffer() {
-        this(ByteBuffer.allocate(0), ByteOrder.BIG_ENDIAN);
+        this(ByteBuffer.allocateDirect(0), ByteOrder.BIG_ENDIAN);
     }
 
     /**
@@ -76,7 +77,7 @@ public final class BitBuffer {
      * @param buf
      *            the bytebuffer to read
      */
-    public BitBuffer(ByteBuffer buf) {
+    public BitBuffer(@NonNull ByteBuffer buf) {
         this(buf, ByteOrder.BIG_ENDIAN);
     }
 
@@ -88,10 +89,7 @@ public final class BitBuffer {
      * @param order
      *            the byte order (big-endian, little-endian, network?)
      */
-    public BitBuffer(ByteBuffer buf, ByteOrder order) {
-        if (buf == null) {
-            throw new IllegalArgumentException("Buffer cannot be null"); //$NON-NLS-1$
-        }
+    public BitBuffer(@NonNull ByteBuffer buf, ByteOrder order) {
         fBuffer = buf;
         setByteOrder(order);
         resetPosition();
index cdda8a817666c24fa6bf691fa0d23925f9df4a31..62b3e3377efe23c4a059fe86fdf55b077a6bd5b5 100644 (file)
@@ -17,6 +17,7 @@ import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.nio.ByteBuffer;
+import java.nio.MappedByteBuffer;
 import java.nio.channels.FileChannel;
 import java.nio.channels.FileChannel.MapMode;
 import java.util.UUID;
@@ -310,17 +311,16 @@ public class CTFStreamInput implements IDefinitionScope, AutoCloseable {
                 + ((packetIndex.getPacketSizeBits() + 7) / 8);
     }
 
-    ByteBuffer getByteBufferAt(long position, long size) throws IOException {
-        return fFileChannel.map(MapMode.READ_ONLY, position, size);
+    @NonNull
+    ByteBuffer getByteBufferAt(long position, long size) throws CTFReaderException, IOException {
+        MappedByteBuffer map = fFileChannel.map(MapMode.READ_ONLY, position, size);
+        if (map == null) {
+            throw new CTFReaderException("Failed to allocate mapped byte buffer"); //$NON-NLS-1$
+        }
+        return map;
     }
 
-    /**
-     * @param fileSizeBytes
-     * @param packetOffsetBytes
-     * @param packetIndex
-     * @return
-     * @throws CTFReaderException
-     */
+    @NonNull
     private ByteBuffer createPacketBitBuffer(long fileSizeBytes,
             long packetOffsetBytes, StreamInputPacketIndexEntry packetIndex) throws CTFReaderException {
         /*
index cacc47e8a50a1bee166a9e192f91c60e9ca090d1..d09ac722151ab87c47cf764fb70afa6b82ddc688 100644 (file)
@@ -504,6 +504,9 @@ public class CTFTrace implements IDefinitionScope, AutoCloseable {
                 FileChannel fc = fis.getChannel()) {
             /* Map one memory page of 4 kiB */
             byteBuffer = fc.map(MapMode.READ_ONLY, 0, (int) Math.min(fc.size(), 4096L));
+            if( byteBuffer == null){
+                throw new IllegalStateException("Failed to allocate memory"); //$NON-NLS-1$
+            }
         } catch (IOException e) {
             /* Shouldn't happen at this stage if every other check passed */
             throw new CTFReaderException(e);
This page took 0.03629 seconds and 5 git commands to generate.