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