rcp: Move plugins to their own sub-directory
[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 static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
17 import java.util.Arrays;
18 import java.util.List;
19
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;
29
30 import com.google.common.base.Joiner;
31 import com.google.common.collect.ImmutableList;
32
33 /**
34 * A fixed length string definition
35 *
36 * @author Matthew Khouzam
37 */
38 @NonNullByDefault
39 public final class ByteArrayDefinition extends AbstractArrayDefinition {
40
41 private final byte[] fContent;
42 private transient @Nullable List<Definition> fDefs;
43
44 /**
45 * An fixed length string declaration, it's created by sequence or array
46 * defintions
47 *
48 * @param declaration
49 * the declaration
50 * @param definitionScope
51 * the definition scope
52 * @param fieldName
53 * the field name
54 * @param content
55 * the string content
56 */
57 public ByteArrayDefinition(CompoundDeclaration declaration,
58 @Nullable IDefinitionScope definitionScope,
59 String fieldName,
60 byte[] content) {
61 super(declaration, definitionScope, fieldName);
62 fContent = content;
63
64 }
65
66 @Override
67 public int getLength() {
68 return fContent.length;
69 }
70
71 @Override
72 public synchronized List<Definition> getDefinitions() {
73 List<Definition> defs = fDefs;
74 if (defs == null) {
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));
81 }
82 fDefs = NonNullUtils.checkNotNull(builder.build());
83 return fDefs;
84 }
85
86 return defs;
87 }
88
89 @Override
90 public String toString() {
91 if (((CompoundDeclaration) getDeclaration()).isString()) {
92 /*
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
95 */
96 int pos = -1;
97 for (int i = 0; i < fContent.length; i++) {
98 if (fContent[i] == 0) {
99 pos = i;
100 break;
101 }
102 }
103 byte[] bytes = (pos != -1) ? (Arrays.copyOf(fContent, pos)) : fContent;
104 return new String(bytes);
105 }
106 StringBuilder b = new StringBuilder();
107 b.append('[');
108 Joiner.on(", ").appendTo(b, Arrays.asList(fContent)); //$NON-NLS-1$
109 b.append(']');
110 return checkNotNull(b.toString());
111 }
112 }
This page took 0.032216 seconds and 5 git commands to generate.