Remove unneeded checkNotNull() calls
[deliverable/tracecompass.git] / ctf / 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.ctf.core.event.scope.IDefinitionScope;
21 import org.eclipse.tracecompass.ctf.core.event.types.AbstractArrayDefinition;
22 import org.eclipse.tracecompass.ctf.core.event.types.CompoundDeclaration;
23 import org.eclipse.tracecompass.ctf.core.event.types.Definition;
24 import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration;
25 import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition;
26
27 import com.google.common.base.Joiner;
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 int getLength() {
65 return fContent.length;
66 }
67
68 @Override
69 public synchronized List<Definition> getDefinitions() {
70 List<Definition> defs = fDefs;
71 if (defs == null) {
72 ImmutableList.Builder<Definition> builder = new ImmutableList.Builder<>();
73 for (int i = 0; i < fContent.length; i++) {
74 IntegerDeclaration charDecl = IntegerDeclaration.UINT_8_DECL;
75 String fieldName = getFieldName() + '[' + i + ']';
76 byte fieldValue = fContent[i];
77 builder.add(new IntegerDefinition(charDecl, getDefinitionScope(), fieldName, fieldValue));
78 }
79 fDefs = builder.build();
80 return fDefs;
81 }
82
83 return defs;
84 }
85
86 @Override
87 public String toString() {
88 if (((CompoundDeclaration) getDeclaration()).isString()) {
89 /*
90 * the string is a byte array and may contain more than the string
91 * plus a null char, this will truncate it back to a null char
92 */
93 int pos = -1;
94 for (int i = 0; i < fContent.length; i++) {
95 if (fContent[i] == 0) {
96 pos = i;
97 break;
98 }
99 }
100 byte[] bytes = (pos != -1) ? (Arrays.copyOf(fContent, pos)) : fContent;
101 return new String(bytes);
102 }
103 StringBuilder b = new StringBuilder();
104 b.append('[');
105 Joiner.on(", ").appendTo(b, Arrays.asList(fContent)); //$NON-NLS-1$
106 b.append(']');
107 return b.toString();
108 }
109
110 /**
111 * Get a byte of the byte array
112 * @param index the index of the byte
113 *
114 * @return the byte
115 */
116 public byte getByte(int index) {
117 return fContent[index];
118 }
119 }
This page took 0.033069 seconds and 5 git commands to generate.