Remove all existing @since annotations
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / internal / ctf / core / event / types / ByteArrayDefinition.java
1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.internal.ctf.core.event.types;
14
15 import java.util.Arrays;
16 import java.util.List;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.eclipse.tracecompass.common.core.NonNullUtils;
21 import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
22 import org.eclipse.tracecompass.ctf.core.event.types.AbstractArrayDefinition;
23 import org.eclipse.tracecompass.ctf.core.event.types.CompoundDeclaration;
24 import org.eclipse.tracecompass.ctf.core.event.types.Definition;
25 import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration;
26 import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition;
27
28 import com.google.common.collect.ImmutableList;
29
30 /**
31 * A fixed length string definition
32 *
33 * @author Matthew Khouzam
34 */
35 @NonNullByDefault
36 public final class ByteArrayDefinition extends AbstractArrayDefinition {
37
38 private final byte[] fContent;
39 private transient @Nullable List<Definition> fDefs;
40
41 /**
42 * An fixed length string declaration, it's created by sequence or array
43 * defintions
44 *
45 * @param declaration
46 * the declaration
47 * @param definitionScope
48 * the definition scope
49 * @param fieldName
50 * the field name
51 * @param content
52 * the string content
53 */
54 public ByteArrayDefinition(CompoundDeclaration declaration,
55 @Nullable IDefinitionScope definitionScope,
56 String fieldName,
57 byte[] content) {
58 super(declaration, definitionScope, fieldName);
59 fContent = content;
60
61 }
62
63 @Override
64 public synchronized List<Definition> getDefinitions() {
65 List<Definition> defs = fDefs;
66 if (defs == null) {
67 ImmutableList.Builder<Definition> builder = new ImmutableList.Builder<>();
68 for (int i = 0; i < fContent.length; i++) {
69 IntegerDeclaration charDecl = IntegerDeclaration.UINT_8_DECL;
70 String fieldName = getFieldName() + '[' + i + ']';
71 byte fieldValue = fContent[i];
72 builder.add(new IntegerDefinition(charDecl, getDefinitionScope(), fieldName, fieldValue));
73 }
74 fDefs = NonNullUtils.checkNotNull(builder.build());
75 return fDefs;
76 }
77
78 return defs;
79 }
80
81 @Override
82 public String toString() {
83 /*
84 * the string is a byte array and may contain more than the string plus
85 * a null char, this will truncate it back to a null char
86 */
87 int pos = -1;
88 for (int i = 0; i < fContent.length; i++) {
89 if (fContent[i] == 0) {
90 pos = i;
91 break;
92 }
93 }
94 byte[] bytes = (pos != -1) ? (Arrays.copyOf(fContent, pos)) : fContent;
95 return new String(bytes);
96 }
97 }
This page took 0.034434 seconds and 5 git commands to generate.