Remove all existing @since annotations
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / internal / ctf / core / event / types / ByteArrayDefinition.java
CommitLineData
7b4f13e6
MK
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
f357bcd4 13package org.eclipse.tracecompass.internal.ctf.core.event.types;
7b4f13e6
MK
14
15import java.util.Arrays;
16import java.util.List;
17
7b4f13e6
MK
18import org.eclipse.jdt.annotation.NonNullByDefault;
19import org.eclipse.jdt.annotation.Nullable;
5db5a3a4 20import org.eclipse.tracecompass.common.core.NonNullUtils;
f357bcd4
AM
21import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
22import org.eclipse.tracecompass.ctf.core.event.types.AbstractArrayDefinition;
23import org.eclipse.tracecompass.ctf.core.event.types.CompoundDeclaration;
24import org.eclipse.tracecompass.ctf.core.event.types.Definition;
25import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration;
26import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition;
7b4f13e6
MK
27
28import com.google.common.collect.ImmutableList;
29
30/**
31 * A fixed length string definition
32 *
33 * @author Matthew Khouzam
7b4f13e6
MK
34 */
35@NonNullByDefault
36public 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 }
5db5a3a4
AM
74 fDefs = NonNullUtils.checkNotNull(builder.build());
75 return fDefs;
7b4f13e6
MK
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.04333 seconds and 5 git commands to generate.