btf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / 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.linuxtools.internal.ctf.core.event.types;
14
15 import java.util.Arrays;
16 import java.util.List;
17
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
22 import org.eclipse.linuxtools.ctf.core.event.types.AbstractArrayDefinition;
23 import org.eclipse.linuxtools.ctf.core.event.types.CompoundDeclaration;
24 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
25 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
26 import org.eclipse.linuxtools.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 * @since 3.1
35 */
36 @NonNullByDefault
37 public final class ByteArrayDefinition extends AbstractArrayDefinition {
38
39 private final byte[] fContent;
40 private transient @Nullable List<Definition> fDefs;
41
42 /**
43 * An fixed length string declaration, it's created by sequence or array
44 * defintions
45 *
46 * @param declaration
47 * the declaration
48 * @param definitionScope
49 * the definition scope
50 * @param fieldName
51 * the field name
52 * @param content
53 * the string content
54 */
55 public ByteArrayDefinition(CompoundDeclaration declaration,
56 @Nullable IDefinitionScope definitionScope,
57 String fieldName,
58 byte[] content) {
59 super(declaration, definitionScope, fieldName);
60 fContent = content;
61
62 }
63
64 @Override
65 public synchronized List<Definition> getDefinitions() {
66 List<Definition> defs = fDefs;
67 if (defs == null) {
68 ImmutableList.Builder<Definition> builder = new ImmutableList.Builder<>();
69 for (int i = 0; i < fContent.length; i++) {
70 IntegerDeclaration charDecl = IntegerDeclaration.UINT_8_DECL;
71 String fieldName = getFieldName() + '[' + i + ']';
72 byte fieldValue = fContent[i];
73 builder.add(new IntegerDefinition(charDecl, getDefinitionScope(), fieldName, fieldValue));
74 }
75 @SuppressWarnings("null")
76 @NonNull List<Definition> ret = builder.build();
77 fDefs = ret;
78 return ret;
79 }
80
81 return defs;
82 }
83
84 @Override
85 public String toString() {
86 /*
87 * the string is a byte array and may contain more than the string plus
88 * a null char, this will truncate it back to a null char
89 */
90 int pos = -1;
91 for (int i = 0; i < fContent.length; i++) {
92 if (fContent[i] == 0) {
93 pos = i;
94 break;
95 }
96 }
97 byte[] bytes = (pos != -1) ? (Arrays.copyOf(fContent, pos)) : fContent;
98 return new String(bytes);
99 }
100 }
This page took 0.038085 seconds and 5 git commands to generate.