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
.ctf
.core
.CTFException
;
18 import org
.eclipse
.tracecompass
.ctf
.core
.event
.io
.BitBuffer
;
19 import org
.eclipse
.tracecompass
.ctf
.core
.event
.scope
.IDefinitionScope
;
22 * A CTF string declaration.
24 * Strings are an array of bytes of variable size and are terminated by a '\0'
25 * "NULL" character. Their encoding is described in the TSDL meta-data. In
26 * absence of encoding attribute information, the default encoding is UTF-8.
29 * @author Matthew Khouzam
30 * @author Simon Marchi
33 public final class StringDeclaration
extends Declaration
{
35 private static final StringDeclaration STRING_DEC_UTF8
= new StringDeclaration(Encoding
.UTF8
);
36 private static final StringDeclaration STRING_DEC_ASCII
= new StringDeclaration(Encoding
.ASCII
);
37 private static final StringDeclaration STRING_DEC_NO_ENC
= new StringDeclaration(Encoding
.NONE
);
39 // ------------------------------------------------------------------------
41 // ------------------------------------------------------------------------
43 private static final int BITS_PER_BYTE
= Byte
.SIZE
;
44 private final Encoding fEncoding
;
46 // ------------------------------------------------------------------------
48 // ------------------------------------------------------------------------
51 * Generate an encoded string declaration
54 * the encoding, utf8 or ascii
56 private StringDeclaration(Encoding encoding
) {
61 * Create a StringDeclaration with the default UTF-8 encoding
63 * @return a {@link StringDeclaration} with UTF-8 encoding
65 public static StringDeclaration
getStringDeclaration() {
66 return STRING_DEC_UTF8
;
70 * Create a StringDeclaration
73 * the {@link Encoding} can be Encoding.UTF8, Encoding.ASCII or
75 * @return a {@link StringDeclaration}
76 * @throws IllegalArgumentException
77 * if the encoding is not recognized.
79 public static StringDeclaration
getStringDeclaration(Encoding encoding
) {
82 return STRING_DEC_ASCII
;
84 return STRING_DEC_NO_ENC
;
86 return STRING_DEC_UTF8
;
88 throw new IllegalArgumentException("Unrecognized encoding: " + encoding
); //$NON-NLS-1$
92 // ------------------------------------------------------------------------
93 // Getters/Setters/Predicates
94 // ------------------------------------------------------------------------
98 * @return the character encoding.
100 public Encoding
getEncoding() {
105 public long getAlignment() {
106 // See ctf 4.2.5: Strings are always aligned on byte size.
107 return BITS_PER_BYTE
;
111 public int getMaximumSize() {
113 * Every definition can have a different size, so we do not scope this.
114 * Minimum size is one byte (8 bits) though.
119 // ------------------------------------------------------------------------
121 // ------------------------------------------------------------------------
124 public StringDefinition
createDefinition(@Nullable IDefinitionScope definitionScope
,
125 String fieldName
, BitBuffer input
) throws CTFException
{
126 String value
= read(input
);
127 return new StringDefinition(this, definitionScope
, fieldName
, value
);
130 private String
read(BitBuffer input
) throws CTFException
{
131 /* Offset the buffer position wrt the current alignment */
134 StringBuilder sb
= new StringBuilder();
135 char c
= (char) input
.get(BITS_PER_BYTE
, false);
138 c
= (char) input
.get(BITS_PER_BYTE
, false);
140 return sb
.toString();
144 public String
toString() {
145 /* Only used for debugging */
146 return "[declaration] string[" + Integer
.toHexString(hashCode()) + ']'; //$NON-NLS-1$
150 public int hashCode() {
151 final int prime
= 31;
170 public boolean equals(@Nullable Object obj
) {
177 if (getClass() != obj
.getClass()) {
180 StringDeclaration other
= (StringDeclaration
) obj
;
181 if (fEncoding
!= other
.fEncoding
) {
188 public boolean isBinaryEquivalent(@Nullable IDeclaration other
) {
189 return equals(other
);