1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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
.tracecompass
.ctf
.core
.event
.types
;
15 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
16 import org
.eclipse
.jdt
.annotation
.Nullable
;
17 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
18 import org
.eclipse
.tracecompass
.ctf
.core
.event
.io
.BitBuffer
;
19 import org
.eclipse
.tracecompass
.ctf
.core
.event
.scope
.IDefinitionScope
;
20 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFReaderException
;
23 * A CTF string declaration.
25 * Strings are an array of bytes of variable size and are terminated by a '\0'
26 * "NULL" character. Their encoding is described in the TSDL meta-data. In
27 * absence of encoding attribute information, the default encoding is UTF-8.
30 * @author Matthew Khouzam
31 * @author Simon Marchi
34 public class StringDeclaration
extends Declaration
{
36 private static final StringDeclaration STRING_DEC_UTF8
= new StringDeclaration(Encoding
.UTF8
);
37 private static final StringDeclaration STRING_DEC_ASCII
= new StringDeclaration(Encoding
.ASCII
);
38 private static final StringDeclaration STRING_DEC_NO_ENC
= new StringDeclaration(Encoding
.NONE
);
40 // ------------------------------------------------------------------------
42 // ------------------------------------------------------------------------
44 private static final int BITS_PER_BYTE
= Byte
.SIZE
;
45 private final Encoding fEncoding
;
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
52 * Generate an encoded string declaration
55 * the encoding, utf8 or ascii
57 private StringDeclaration(Encoding encoding
) {
62 * Create a StringDeclaration with the default UTF-8 encoding
64 * @return a {@link StringDeclaration} with UTF-8 encoding
66 public static StringDeclaration
getStringDeclaration() {
67 return STRING_DEC_UTF8
;
71 * Create a StringDeclaration
74 * the {@link Encoding} can be Encoding.UTF8, Encoding.ASCII or
76 * @return a {@link StringDeclaration}
77 * @throws IllegalArgumentException
78 * if the encoding is not recognized.
80 public static StringDeclaration
getStringDeclaration(Encoding encoding
) {
83 return STRING_DEC_ASCII
;
85 return STRING_DEC_NO_ENC
;
87 return STRING_DEC_UTF8
;
89 throw new IllegalArgumentException("Unrecognized encoding: " + encoding
); //$NON-NLS-1$
93 // ------------------------------------------------------------------------
94 // Getters/Setters/Predicates
95 // ------------------------------------------------------------------------
99 * @return the character encoding.
101 public Encoding
getEncoding() {
106 public long getAlignment() {
107 // See ctf 4.2.5: Strings are always aligned on byte size.
108 return BITS_PER_BYTE
;
115 public int getMaximumSize() {
116 return Integer
.MAX_VALUE
;
119 // ------------------------------------------------------------------------
121 // ------------------------------------------------------------------------
127 public StringDefinition
createDefinition(@Nullable IDefinitionScope definitionScope
,
128 String fieldName
, BitBuffer input
) throws CTFReaderException
{
129 String value
= read(input
);
130 return new StringDefinition(this, definitionScope
, fieldName
, value
);
133 private String
read(BitBuffer input
) throws CTFReaderException
{
134 /* Offset the buffer position wrt the current alignment */
137 StringBuilder sb
= new StringBuilder();
138 char c
= (char) input
.get(BITS_PER_BYTE
, false);
141 c
= (char) input
.get(BITS_PER_BYTE
, false);
143 return NonNullUtils
.checkNotNull(sb
.toString());
147 public String
toString() {
148 /* Only used for debugging */
149 return "[declaration] string[" + Integer
.toHexString(hashCode()) + ']'; //$NON-NLS-1$
153 public int hashCode() {
154 final int prime
= 31;
173 public boolean equals(@Nullable Object obj
) {
180 if (getClass() != obj
.getClass()) {
183 StringDeclaration other
= (StringDeclaration
) obj
;
184 if (fEncoding
!= other
.fEncoding
) {
191 public boolean isBinaryEquivalent(@Nullable IDeclaration other
) {
192 return equals(other
);