ctf: Make events immutable
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / StringDeclaration.java
index 972d2829a694895be865ea4d308b01690234151f..710f75aa4e1e8b3f3a1f4ba4bd6d495ad09552ef 100644 (file)
 
 package org.eclipse.linuxtools.ctf.core.event.types;
 
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
+
 /**
  * A CTF string declaration.
  *
@@ -23,13 +27,13 @@ package org.eclipse.linuxtools.ctf.core.event.types;
  * @author Matthew Khouzam
  * @author Simon Marchi
  */
-public class StringDeclaration implements IDeclaration {
+public class StringDeclaration extends Declaration {
 
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
 
-    private Encoding encoding = Encoding.UTF8;
+    private final Encoding fEncoding;
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -39,6 +43,7 @@ public class StringDeclaration implements IDeclaration {
      * Generate a UTF8 string declaration
      */
     public StringDeclaration() {
+        fEncoding = Encoding.UTF8;
     }
 
     /**
@@ -46,7 +51,7 @@ public class StringDeclaration implements IDeclaration {
      * @param encoding the encoding, utf8 or ascii
      */
     public StringDeclaration(Encoding encoding) {
-        this.encoding = encoding;
+        fEncoding = encoding;
     }
 
     // ------------------------------------------------------------------------
@@ -58,15 +63,7 @@ public class StringDeclaration implements IDeclaration {
      * @return the character encoding.
      */
     public Encoding getEncoding() {
-        return encoding;
-    }
-
-    /**
-     *
-     * @param encoding the character encoding to set
-     */
-    public void setEncoding(Encoding encoding) {
-        this.encoding = encoding;
+        return fEncoding;
     }
 
     @Override
@@ -74,16 +71,40 @@ public class StringDeclaration implements IDeclaration {
         // See ctf 4.2.5: Strings are always aligned on byte size.
         return 8;
     }
+
+    /**
+     * @since 3.0
+     */
+    @Override
+    public int getMaximumSize() {
+        return Integer.MAX_VALUE;
+    }
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
 
+    /**
+     * @since 3.0
+     */
     @Override
     public StringDefinition createDefinition(IDefinitionScope definitionScope,
-            String fieldName) {
-        return new StringDefinition(this, definitionScope, fieldName);
+            String fieldName, BitBuffer input) throws CTFReaderException {
+        String value = read(input);
+        return new StringDefinition(this, definitionScope, fieldName, value);
     }
 
+    private String read(BitBuffer input) throws CTFReaderException {
+        /* Offset the buffer position wrt the current alignment */
+        alignRead(input);
+
+        StringBuilder sb = new StringBuilder();
+        char c = (char) input.get(8, false);
+        while (c != 0) {
+            sb.append(c);
+            c = (char) input.get(8, false);
+        }
+        return sb.toString();
+    }
     @Override
     public String toString() {
         /* Only used for debugging */
This page took 0.025264 seconds and 5 git commands to generate.