ctf: Rename packages to org.eclipse.tracecompass.*
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / ctf / core / trace / Utils.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others
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: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.ctf.core.trace;
14
15 import java.util.UUID;
16
17 import org.eclipse.tracecompass.ctf.core.event.types.AbstractArrayDefinition;
18 import org.eclipse.tracecompass.ctf.core.event.types.CompoundDeclaration;
19 import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration;
20 import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration;
21 import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition;
22
23 /**
24 * Various utilities.
25 *
26 * @version 1.0
27 * @author Matthew Khouzam
28 * @author Simon Marchi
29 */
30 public final class Utils {
31
32 private Utils() {
33 }
34
35 // ------------------------------------------------------------------------
36 // Constants
37 // ------------------------------------------------------------------------
38
39 /**
40 * CTF magic number. (sort of looks like CTF CTF CT)
41 */
42 public static final int CTF_MAGIC = 0xC1FC1FC1;
43
44 /**
45 * TSDL magic number. (sort of looks like TSDL LSDT)
46 */
47 public static final int TSDL_MAGIC = 0x75D11D57;
48
49 /**
50 * TSDL magic number length in bytes.
51 */
52 public static final int TSDL_MAGIC_LEN = 4;
53
54 /**
55 * Directory separator on the current platform.
56 */
57 public static final String SEPARATOR = System.getProperty("file.separator"); //$NON-NLS-1$
58
59 /**
60 * Length in bytes of a UUID value.
61 */
62 public static final int UUID_LEN = 16;
63
64 // ------------------------------------------------------------------------
65 // Operations
66 // ------------------------------------------------------------------------
67
68 /**
69 * Performs an unsigned long comparison on two unsigned long numbers.
70 *
71 * <strong> As Java does not support unsigned types and arithmetic,
72 * parameters are received encoded as a signed long (two-complement) but the
73 * operation is an unsigned comparator.</strong>
74 *
75 * @param left
76 * Left operand of the comparator.
77 * @param right
78 * Right operand of the comparator.
79 * @return -1 if left &lt; right, 1 if left &gt; right, 0 if left == right.
80 */
81 public static int unsignedCompare(long left, long right) {
82 /*
83 * This method assumes that the arithmetic overflow on signed integer
84 * wrap on a circular domain (modulo arithmetic in two-complement),
85 * which is the defined behavior in Java.
86 *
87 * This idea is to rotate the domain by the length of the negative
88 * space, and then use the signed operator.
89 */
90 final long a = left + Long.MIN_VALUE;
91 final long b = right + Long.MIN_VALUE;
92 if (a < b) {
93 return -1;
94 } else if (a > b) {
95 return 1;
96 }
97 return 0;
98 }
99
100 /**
101 * Gets a UUID from an array defintion
102 *
103 * @param uuidDef
104 * the array defintions, must contain integer bytes
105 * @return the UUID
106 * @throws CTFReaderException
107 * if the definition contains less than 16 elements
108 * @since 3.1
109 */
110 public static UUID getUUIDfromDefinition(AbstractArrayDefinition uuidDef) throws CTFReaderException {
111 byte[] uuidArray = new byte[16];
112 IDeclaration declaration = uuidDef.getDeclaration();
113 if (!(declaration instanceof CompoundDeclaration)) {
114 throw new CTFReaderException("UUID must be a sequence of unsigned bytes"); //$NON-NLS-1$
115 }
116 CompoundDeclaration uuidDec = (CompoundDeclaration) declaration;
117
118 IDeclaration uuidElem = uuidDec.getElementType();
119 if (!(uuidElem instanceof IntegerDeclaration)) {
120 throw new CTFReaderException("UUID must be a sequence of unsigned bytes"); //$NON-NLS-1$
121 }
122 IntegerDeclaration intUuidElem = (IntegerDeclaration) uuidElem;
123 if (!intUuidElem.isUnsignedByte()) {
124 throw new CTFReaderException("UUID must be a sequence of unsigned bytes"); //$NON-NLS-1$
125 }
126 return getUUID(uuidDef, uuidArray);
127 }
128
129 private static UUID getUUID(AbstractArrayDefinition uuidDef, byte[] uuidArray) throws CTFReaderException {
130 for (int i = 0; i < uuidArray.length; i++) {
131 IntegerDefinition uuidByteDef = (IntegerDefinition) uuidDef.getDefinitions().get(i);
132 if (uuidByteDef == null) {
133 throw new CTFReaderException("UUID incomplete, only " + i + " bytes available"); //$NON-NLS-1$ //$NON-NLS-2$
134 }
135 uuidArray[i] = (byte) uuidByteDef.getValue();
136 }
137
138 UUID uuid = Utils.makeUUID(uuidArray);
139 return uuid;
140 }
141
142 /**
143 * Gets a UUID from an array defintion
144 *
145 * @param uuidDef
146 * the array defintions, must contain integer bytes
147 * @return the UUID
148 * @throws CTFReaderException
149 * if the definition contains less than 16 elements
150 * @since 3.1
151 * @deprecated use
152 * {@link Utils#getUUIDfromDefinition(AbstractArrayDefinition uuidDef)}
153 */
154 @Deprecated
155 public static UUID getUUIDfromDefinition(org.eclipse.tracecompass.ctf.core.event.types.ArrayDefinition uuidDef) throws CTFReaderException {
156 byte[] uuidArray = new byte[16];
157 return getUUID(uuidDef, uuidArray);
158 }
159
160 /**
161 * Creates a UUID object from an array of 16 bytes.
162 *
163 * @param bytes
164 * Array of 16 bytes.
165 * @return A UUID object.
166 */
167 public static UUID makeUUID(byte bytes[]) {
168 long high = 0;
169 long low = 0;
170
171 assert (bytes.length == Utils.UUID_LEN);
172
173 for (int i = 0; i < 8; i++) {
174 low = (low << 8) | (bytes[i + 8] & 0xFF);
175 high = (high << 8) | (bytes[i] & 0xFF);
176 }
177
178 UUID uuid = new UUID(high, low);
179
180 return uuid;
181 }
182
183 }
This page took 0.035276 seconds and 5 git commands to generate.