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 static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
17 import java
.util
.Arrays
;
18 import java
.util
.List
;
20 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
21 import org
.eclipse
.jdt
.annotation
.Nullable
;
22 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
23 import org
.eclipse
.tracecompass
.ctf
.core
.event
.scope
.IDefinitionScope
;
24 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.AbstractArrayDefinition
;
25 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.CompoundDeclaration
;
26 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.Definition
;
27 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDeclaration
;
28 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDefinition
;
30 import com
.google
.common
.base
.Joiner
;
31 import com
.google
.common
.collect
.ImmutableList
;
34 * A fixed length string definition
36 * @author Matthew Khouzam
39 public final class ByteArrayDefinition
extends AbstractArrayDefinition
{
41 private final byte[] fContent
;
42 private transient @Nullable List
<Definition
> fDefs
;
45 * An fixed length string declaration, it's created by sequence or array
50 * @param definitionScope
51 * the definition scope
57 public ByteArrayDefinition(CompoundDeclaration declaration
,
58 @Nullable IDefinitionScope definitionScope
,
61 super(declaration
, definitionScope
, fieldName
);
67 public int getLength() {
68 return fContent
.length
;
72 public synchronized List
<Definition
> getDefinitions() {
73 List
<Definition
> defs
= fDefs
;
75 ImmutableList
.Builder
<Definition
> builder
= new ImmutableList
.Builder
<>();
76 for (int i
= 0; i
< fContent
.length
; i
++) {
77 IntegerDeclaration charDecl
= IntegerDeclaration
.UINT_8_DECL
;
78 String fieldName
= getFieldName() + '[' + i
+ ']';
79 byte fieldValue
= fContent
[i
];
80 builder
.add(new IntegerDefinition(charDecl
, getDefinitionScope(), fieldName
, fieldValue
));
82 fDefs
= NonNullUtils
.checkNotNull(builder
.build());
90 public String
toString() {
91 if (((CompoundDeclaration
) getDeclaration()).isString()) {
93 * the string is a byte array and may contain more than the string
94 * plus a null char, this will truncate it back to a null char
97 for (int i
= 0; i
< fContent
.length
; i
++) {
98 if (fContent
[i
] == 0) {
103 byte[] bytes
= (pos
!= -1) ?
(Arrays
.copyOf(fContent
, pos
)) : fContent
;
104 return new String(bytes
);
106 StringBuilder b
= new StringBuilder();
108 Joiner
.on(", ").appendTo(b
, Arrays
.asList(fContent
)); //$NON-NLS-1$
110 return checkNotNull(b
.toString());
114 * Get a byte of the byte array
115 * @param index the index of the byte
119 public byte getByte(int index
) {
120 return fContent
[index
];