-
Notifications
You must be signed in to change notification settings - Fork 11
/
ByteArrayDefinition.java
142 lines (129 loc) · 4.8 KB
/
ByteArrayDefinition.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*******************************************************************************
* Copyright (c) 2014 Ericsson
*
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0 which
* accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Matthew Khouzam - Initial API and implementation
*******************************************************************************/
package org.eclipse.tracecompass.internal.ctf.core.event.types;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
import org.eclipse.tracecompass.ctf.core.event.types.AbstractArrayDefinition;
import org.eclipse.tracecompass.ctf.core.event.types.CompoundDeclaration;
import org.eclipse.tracecompass.ctf.core.event.types.Definition;
import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration;
import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration;
import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
/**
* A fixed length string definition
*
* @author Matthew Khouzam
*/
@NonNullByDefault
public final class ByteArrayDefinition extends AbstractArrayDefinition {
private final byte[] fContent;
private transient @Nullable List<Definition> fDefs;
/**
* An fixed length string declaration, it's created by sequence or array
* defintions
*
* @param declaration
* the declaration
* @param definitionScope
* the definition scope
* @param fieldName
* the field name
* @param content
* the string content
*/
public ByteArrayDefinition(CompoundDeclaration declaration,
@Nullable IDefinitionScope definitionScope,
String fieldName,
byte[] content) {
super(declaration, definitionScope, fieldName);
fContent = content;
}
@Override
public int getLength() {
return fContent.length;
}
@Override
public synchronized List<Definition> getDefinitions() {
List<Definition> defs = fDefs;
if (defs == null) {
ImmutableList.Builder<Definition> builder = new ImmutableList.Builder<>();
for (int i = 0; i < fContent.length; i++) {
IntegerDeclaration charDecl = IntegerDeclaration.UINT_8_DECL;
String fieldName = getFieldName() + '[' + i + ']';
byte fieldValue = fContent[i];
builder.add(new IntegerDefinition(charDecl, getDefinitionScope(), fieldName, fieldValue));
}
fDefs = builder.build();
return fDefs;
}
return defs;
}
@Override
public String toString() {
CompoundDeclaration compoundDeclaration = (CompoundDeclaration) getDeclaration();
if (compoundDeclaration.isString()) {
/*
* the string is a byte array and may contain more than the string
* plus a null char, this will truncate it back to a null char
*/
int pos = -1;
for (int i = 0; i < fContent.length; i++) {
if (fContent[i] == 0) {
pos = i;
break;
}
}
byte[] bytes = (pos != -1) ? (Arrays.copyOf(fContent, pos)) : fContent;
return new String(bytes);
}
StringBuilder b = new StringBuilder();
b.append('[');
IDeclaration elementType = compoundDeclaration.getElementType();
// Handle 0xff hex
boolean isHex = false;
// this should ALWAYS be true, but in case...
if (elementType instanceof IntegerDeclaration) {
IntegerDeclaration integerDeclaration = (IntegerDeclaration) elementType;
isHex = integerDeclaration.getBase() == 16;
}
List<String> elements = new ArrayList<>();
if (isHex) {
for (byte element : fContent) {
elements.add(String.format("0x%02X", element)); //$NON-NLS-1$
}
} else {
for (byte element : fContent) {
elements.add(String.format("%d", element)); //$NON-NLS-1$
}
}
Joiner.on(", ").appendTo(b, elements); //$NON-NLS-1$
b.append(']');
return b.toString();
}
/**
* Get a byte of the byte array
* @param index the index of the byte
*
* @return the byte
*/
public byte getByte(int index) {
return fContent[index];
}
}