1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.ctf
.core
.event
.types
;
15 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
16 import org
.eclipse
.linuxtools
.ctf
.core
.event
.scope
.IDefinitionScope
;
17 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
20 * A CTF string declaration.
22 * Strings are an array of bytes of variable size and are terminated by a '\0'
23 * "NULL" character. Their encoding is described in the TSDL meta-data. In
24 * absence of encoding attribute information, the default encoding is UTF-8.
27 * @author Matthew Khouzam
28 * @author Simon Marchi
30 public class StringDeclaration
extends Declaration
{
32 // ------------------------------------------------------------------------
34 // ------------------------------------------------------------------------
36 private final Encoding fEncoding
;
38 // ------------------------------------------------------------------------
40 // ------------------------------------------------------------------------
43 * Generate a UTF8 string declaration
45 public StringDeclaration() {
46 fEncoding
= Encoding
.UTF8
;
50 * Generate an encoded string declaration
51 * @param encoding the encoding, utf8 or ascii
53 public StringDeclaration(Encoding encoding
) {
57 // ------------------------------------------------------------------------
58 // Getters/Setters/Predicates
59 // ------------------------------------------------------------------------
63 * @return the character encoding.
65 public Encoding
getEncoding() {
70 public long getAlignment() {
71 // See ctf 4.2.5: Strings are always aligned on byte size.
79 public int getMaximumSize() {
80 return Integer
.MAX_VALUE
;
82 // ------------------------------------------------------------------------
84 // ------------------------------------------------------------------------
90 public StringDefinition
createDefinition(IDefinitionScope definitionScope
,
91 String fieldName
, BitBuffer input
) throws CTFReaderException
{
92 String value
= read(input
);
93 return new StringDefinition(this, definitionScope
, fieldName
, value
);
96 private String
read(BitBuffer input
) throws CTFReaderException
{
97 /* Offset the buffer position wrt the current alignment */
100 StringBuilder sb
= new StringBuilder();
101 char c
= (char) input
.get(8, false);
104 c
= (char) input
.get(8, false);
106 return sb
.toString();
109 public String
toString() {
110 /* Only used for debugging */
111 return "[declaration] string[" + Integer
.toHexString(hashCode()) + ']'; //$NON-NLS-1$