1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
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
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.types
;
15 import java
.util
.Arrays
;
16 import java
.util
.List
;
18 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
19 import org
.eclipse
.jdt
.annotation
.Nullable
;
20 import org
.eclipse
.tracecompass
.ctf
.core
.event
.scope
.IDefinitionScope
;
21 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.AbstractArrayDefinition
;
22 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.CompoundDeclaration
;
23 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.Definition
;
24 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDeclaration
;
25 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDefinition
;
27 import com
.google
.common
.base
.Joiner
;
28 import com
.google
.common
.collect
.ImmutableList
;
31 * A fixed length string definition
33 * @author Matthew Khouzam
36 public final class ByteArrayDefinition
extends AbstractArrayDefinition
{
38 private final byte[] fContent
;
39 private transient @Nullable List
<Definition
> fDefs
;
42 * An fixed length string declaration, it's created by sequence or array
47 * @param definitionScope
48 * the definition scope
54 public ByteArrayDefinition(CompoundDeclaration declaration
,
55 @Nullable IDefinitionScope definitionScope
,
58 super(declaration
, definitionScope
, fieldName
);
64 public int getLength() {
65 return fContent
.length
;
69 public synchronized List
<Definition
> getDefinitions() {
70 List
<Definition
> defs
= fDefs
;
72 ImmutableList
.Builder
<Definition
> builder
= new ImmutableList
.Builder
<>();
73 for (int i
= 0; i
< fContent
.length
; i
++) {
74 IntegerDeclaration charDecl
= IntegerDeclaration
.UINT_8_DECL
;
75 String fieldName
= getFieldName() + '[' + i
+ ']';
76 byte fieldValue
= fContent
[i
];
77 builder
.add(new IntegerDefinition(charDecl
, getDefinitionScope(), fieldName
, fieldValue
));
79 fDefs
= builder
.build();
87 public String
toString() {
88 if (((CompoundDeclaration
) getDeclaration()).isString()) {
90 * the string is a byte array and may contain more than the string
91 * plus a null char, this will truncate it back to a null char
94 for (int i
= 0; i
< fContent
.length
; i
++) {
95 if (fContent
[i
] == 0) {
100 byte[] bytes
= (pos
!= -1) ?
(Arrays
.copyOf(fContent
, pos
)) : fContent
;
101 return new String(bytes
);
103 StringBuilder b
= new StringBuilder();
105 Joiner
.on(", ").appendTo(b
, Arrays
.asList(fContent
)); //$NON-NLS-1$
111 * Get a byte of the byte array
112 * @param index the index of the byte
116 public byte getByte(int index
) {
117 return fContent
[index
];