Commit | Line | Data |
---|---|---|
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 | 13 | package org.eclipse.tracecompass.internal.ctf.core.event.types; |
7b4f13e6 | 14 | |
15f6223a MK |
15 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
16 | ||
7b4f13e6 MK |
17 | import java.util.Arrays; |
18 | import java.util.List; | |
19 | ||
7b4f13e6 MK |
20 | import org.eclipse.jdt.annotation.NonNullByDefault; |
21 | import org.eclipse.jdt.annotation.Nullable; | |
5db5a3a4 | 22 | import org.eclipse.tracecompass.common.core.NonNullUtils; |
f357bcd4 AM |
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; | |
7b4f13e6 | 29 | |
15f6223a | 30 | import com.google.common.base.Joiner; |
7b4f13e6 MK |
31 | import com.google.common.collect.ImmutableList; |
32 | ||
33 | /** | |
34 | * A fixed length string definition | |
35 | * | |
36 | * @author Matthew Khouzam | |
7b4f13e6 MK |
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 | ||
15f6223a MK |
66 | @Override |
67 | public int getLength() { | |
68 | return fContent.length; | |
69 | } | |
70 | ||
7b4f13e6 MK |
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 | } | |
5db5a3a4 AM |
82 | fDefs = NonNullUtils.checkNotNull(builder.build()); |
83 | return fDefs; | |
7b4f13e6 MK |
84 | } |
85 | ||
86 | return defs; | |
87 | } | |
88 | ||
89 | @Override | |
90 | public String toString() { | |
15f6223a MK |
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 | } | |
7b4f13e6 | 102 | } |
15f6223a MK |
103 | byte[] bytes = (pos != -1) ? (Arrays.copyOf(fContent, pos)) : fContent; |
104 | return new String(bytes); | |
7b4f13e6 | 105 | } |
15f6223a MK |
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()); | |
7b4f13e6 MK |
111 | } |
112 | } |