Commit | Line | Data |
---|---|---|
866e5b51 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2011-2012 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.linuxtools.ctf.core.trace; | |
14 | ||
15 | import java.util.UUID; | |
16 | ||
17 | /** | |
866e5b51 | 18 | * Various utilities. |
0594c61c | 19 | * |
d37aaa7f FC |
20 | * @version 1.0 |
21 | * @author Matthew Khouzam | |
22 | * @author Simon Marchi | |
866e5b51 FC |
23 | */ |
24 | public class Utils { | |
25 | ||
0594c61c AM |
26 | private Utils() {} |
27 | ||
866e5b51 FC |
28 | // ------------------------------------------------------------------------ |
29 | // Constants | |
30 | // ------------------------------------------------------------------------ | |
31 | ||
32 | /** | |
9ac2eb62 | 33 | * CTF magic number. (sort of looks like CTF CTF CT) |
866e5b51 | 34 | */ |
0594c61c | 35 | public static final int CTF_MAGIC = 0xC1FC1FC1; |
866e5b51 FC |
36 | |
37 | /** | |
9ac2eb62 | 38 | * TSDL magic number. (sort of looks like TSDL LSDT) |
866e5b51 | 39 | */ |
0594c61c | 40 | public static final int TSDL_MAGIC = 0x75D11D57; |
866e5b51 FC |
41 | |
42 | /** | |
43 | * TSDL magic number length in bytes. | |
44 | */ | |
0594c61c | 45 | public static final int TSDL_MAGIC_LEN = 4; |
866e5b51 FC |
46 | |
47 | /** | |
48 | * Directory separator on the current platform. | |
49 | */ | |
0594c61c | 50 | public static final String SEPARATOR = System.getProperty("file.separator"); //$NON-NLS-1$ |
866e5b51 FC |
51 | |
52 | /** | |
53 | * Length in bytes of a UUID value. | |
54 | */ | |
0594c61c | 55 | public static final int UUID_LEN = 16; |
866e5b51 FC |
56 | |
57 | // ------------------------------------------------------------------------ | |
58 | // Operations | |
59 | // ------------------------------------------------------------------------ | |
60 | ||
61 | /** | |
62 | * Unsigned long comparison. | |
63 | * | |
64 | * @param a | |
65 | * First operand. | |
66 | * @param b | |
67 | * Second operand. | |
68 | * @return -1 if a < b, 1 if a > b, 0 if a == b. | |
69 | */ | |
70 | public static int unsignedCompare(long a, long b) { | |
71 | boolean aLeftBit = (a & (1 << (Long.SIZE - 1))) != 0; | |
72 | boolean bLeftBit = (b & (1 << (Long.SIZE - 1))) != 0; | |
73 | ||
74 | if (aLeftBit && !bLeftBit) { | |
75 | return 1; | |
76 | } else if (!aLeftBit && bLeftBit) { | |
77 | return -1; | |
78 | } else { | |
79 | if (a < b) { | |
80 | return -1; | |
81 | } else if (a > b) { | |
82 | return 1; | |
83 | } else { | |
84 | return 0; | |
85 | } | |
86 | } | |
87 | } | |
88 | ||
89 | /** | |
90 | * Creates a UUID object from an array of 16 bytes. | |
91 | * | |
92 | * @param bytes | |
93 | * Array of 16 bytes. | |
94 | * @return A UUID object. | |
95 | */ | |
96 | public static UUID makeUUID(byte bytes[]) { | |
97 | long high = 0; | |
98 | long low = 0; | |
99 | ||
100 | assert (bytes.length == Utils.UUID_LEN); | |
101 | ||
102 | for (int i = 0; i < 8; i++) { | |
103 | low = (low << 8) | (bytes[i + 8] & 0xFF); | |
104 | high = (high << 8) | (bytes[i] & 0xFF); | |
105 | } | |
106 | ||
107 | UUID uuid = new UUID(high, low); | |
108 | ||
109 | return uuid; | |
110 | } | |
111 | ||
112 | } |