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