Commit | Line | Data |
---|---|---|
5255c030 VP |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made 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 | * Vincent Perot - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
93d1d135 | 13 | package org.eclipse.linuxtools.internal.pcap.core.util; |
5255c030 VP |
14 | |
15 | import java.text.DateFormat; | |
16 | import java.text.SimpleDateFormat; | |
17 | import java.util.Date; | |
18 | ||
b2b163e9 | 19 | import org.eclipse.jdt.annotation.NonNull; |
93d1d135 | 20 | import org.eclipse.linuxtools.internal.pcap.core.protocol.ethernet2.EthernetIIValues; |
5255c030 VP |
21 | |
22 | /** | |
23 | * Class for helping with the conversion of data. | |
24 | * | |
25 | * @author Vincent Perot | |
26 | */ | |
27 | public final class ConversionHelper { | |
28 | ||
5255c030 | 29 | @SuppressWarnings("null") |
b2b163e9 | 30 | private static final @NonNull char[] HEX_ARRAY = "0123456789abcdef".toCharArray(); //$NON-NLS-1$ |
5255c030 VP |
31 | private static final String EMPTY_STRING = ""; //$NON-NLS-1$ |
32 | private static final String DEFAULT_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS"; //$NON-NLS-1$ | |
33 | private static final DateFormat DATE_FORMATTER = new SimpleDateFormat(DEFAULT_TIME_PATTERN); | |
34 | ||
35 | private ConversionHelper() { | |
36 | } | |
37 | ||
38 | /** | |
39 | * Generate an integer from an unsigned byte. | |
40 | * | |
41 | * @param n | |
42 | * the unsigned byte. | |
43 | * @return the integer representing the unsigned value. | |
44 | */ | |
45 | public static int unsignedByteToInt(byte n) { | |
46 | return n & 0x000000FF; | |
47 | } | |
48 | ||
49 | /** | |
50 | * Generate an integer from an unsigned short. | |
51 | * | |
52 | * @param n | |
53 | * the unsigned short. | |
54 | * @return the integer representing the unsigned value. | |
55 | */ | |
56 | public static int unsignedShortToInt(short n) { | |
57 | return n & 0x0000FFFF; | |
58 | } | |
59 | ||
60 | /** | |
61 | * Generate a long from an unsigned integer. | |
62 | * | |
63 | * @param n | |
64 | * the unsigned integer. | |
65 | * @return the long representing the unsigned value. | |
66 | */ | |
67 | public static long unsignedIntToLong(int n) { | |
68 | return n & 0x00000000FFFFFFFFL; | |
69 | } | |
70 | ||
71 | /** | |
72 | * Generate an hex number from a byte array. | |
73 | * | |
74 | * @param bytes | |
75 | * The array of bytes. | |
76 | * @param spaced | |
77 | * Whether there must be a space between each byte or not. | |
78 | * @return the hex as a string. | |
79 | */ | |
80 | public static String bytesToHex(byte[] bytes, boolean spaced) { | |
81 | // No need to check for character encoding since bytes represents a | |
82 | // number. | |
83 | ||
84 | if (bytes.length == 0) { | |
85 | return EMPTY_STRING; | |
86 | } | |
87 | ||
88 | char[] hexChars = spaced ? new char[bytes.length * 3 - 1] : new char[bytes.length * 2]; | |
89 | int delta = spaced ? 3 : 2; | |
90 | char separator = ' '; | |
91 | ||
92 | for (int j = 0; j < bytes.length; j++) { | |
93 | ||
94 | int v = bytes[j] & 0xFF; | |
95 | hexChars[j * delta] = HEX_ARRAY[v >>> 4]; | |
96 | hexChars[j * delta + 1] = HEX_ARRAY[v & 0x0F]; | |
97 | ||
98 | if (spaced && (j != bytes.length - 1)) { | |
99 | hexChars[j * delta + 2] = separator; | |
100 | } | |
101 | } | |
102 | return new String(hexChars); | |
103 | } | |
104 | ||
105 | // TODO Add little endian support | |
106 | /** | |
107 | * Generate a string representing the MAC address. | |
108 | * | |
109 | * @param mac | |
110 | * The MAC address as a byte array. | |
111 | * @return The string representing the MAC address. | |
112 | */ | |
113 | public static String toMacAddress(byte[] mac) { | |
114 | ||
115 | if (mac.length != EthernetIIValues.MAC_ADDRESS_SIZE) { | |
116 | throw new IllegalArgumentException(); | |
117 | } | |
118 | char separator = ':'; | |
119 | return String.format("%02x", mac[0]) + separator + //$NON-NLS-1$ | |
120 | String.format("%02x", mac[1]) + separator + //$NON-NLS-1$ | |
121 | String.format("%02x", mac[2]) + separator + //$NON-NLS-1$ | |
122 | String.format("%02x", mac[3]) + separator + //$NON-NLS-1$ | |
123 | String.format("%02x", mac[4]) + separator + //$NON-NLS-1$ | |
124 | String.format("%02x", mac[5]); //$NON-NLS-1$ | |
125 | ||
126 | } | |
127 | ||
5255c030 VP |
128 | // TODO support non GMT time. |
129 | ||
130 | /** | |
131 | * Convert a timestamp into a date. | |
132 | * | |
133 | * @param ts | |
134 | * The timestamp. It represents the time since Epoch in | |
135 | * microseconds. | |
136 | * @param scale | |
137 | * The scale of the timestamp. | |
138 | * @return The date as a string. | |
139 | */ | |
140 | public static String toGMTTime(long ts, PcapTimestampScale scale) { | |
141 | long timestamp; | |
142 | switch (scale) { | |
143 | case MICROSECOND: | |
144 | timestamp = ts * 1000; | |
145 | break; | |
146 | case NANOSECOND: | |
147 | timestamp = ts; | |
148 | break; | |
149 | default: | |
150 | throw new IllegalArgumentException("The timestamp precision is not valid!"); //$NON-NLS-1$ | |
151 | } | |
152 | return format(timestamp); | |
153 | } | |
154 | ||
155 | /** | |
156 | * Format the timestamp to a string. | |
157 | * | |
158 | * @param value | |
159 | * the timestamp value to format (in ns) | |
160 | * @return the formatted timestamp | |
161 | */ | |
162 | private static String format(long value) { | |
163 | // Split the timestamp value into its sub-components | |
164 | long date = value / 1000000; // milliseconds since epoch | |
165 | long cs = Math.abs((value % 1000000) / 1000); // microseconds | |
166 | long ns = Math.abs(value % 1000); // nanoseconds | |
167 | ||
168 | Date dateObject = new Date(date); | |
169 | ||
170 | StringBuilder sb = new StringBuilder(DATE_FORMATTER.format(dateObject)); | |
171 | sb.append('.') | |
172 | .append(String.format("%03d", cs)) //$NON-NLS-1$ | |
173 | .append('.') | |
174 | .append(String.format("%03d", ns)); //$NON-NLS-1$ | |
175 | ||
176 | String string = sb.toString(); | |
177 | if (string == null) { | |
178 | return EMPTY_STRING; | |
179 | } | |
180 | return string; | |
181 | ||
182 | } | |
183 | ||
184 | } |