1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
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
10 * Vincent Perot - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.pcap
.core
.util
;
15 import java
.text
.DateFormat
;
16 import java
.text
.SimpleDateFormat
;
17 import java
.util
.Date
;
19 import org
.eclipse
.linuxtools
.pcap
.core
.protocol
.ethernet2
.EthernetIIValues
;
20 import org
.eclipse
.linuxtools
.pcap
.core
.protocol
.ipv4
.IPv4Values
;
23 * Class for helping with the conversion of data.
25 * @author Vincent Perot
27 public final class ConversionHelper
{
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
);
36 private ConversionHelper() {
40 * Generate an integer from an unsigned byte.
44 * @return the integer representing the unsigned value.
46 public static int unsignedByteToInt(byte n
) {
47 return n
& 0x000000FF;
51 * Generate an integer from an unsigned short.
55 * @return the integer representing the unsigned value.
57 public static int unsignedShortToInt(short n
) {
58 return n
& 0x0000FFFF;
62 * Generate a long from an unsigned integer.
65 * the unsigned integer.
66 * @return the long representing the unsigned value.
68 public static long unsignedIntToLong(int n
) {
69 return n
& 0x00000000FFFFFFFFL
;
73 * Generate an hex number from a byte array.
78 * Whether there must be a space between each byte or not.
79 * @return the hex as a string.
81 public static String
bytesToHex(byte[] bytes
, boolean spaced
) {
82 // No need to check for character encoding since bytes represents a
85 if (bytes
.length
== 0) {
89 char[] hexChars
= spaced ?
new char[bytes
.length
* 3 - 1] : new char[bytes
.length
* 2];
90 int delta
= spaced ?
3 : 2;
93 for (int j
= 0; j
< bytes
.length
; j
++) {
95 int v
= bytes
[j
] & 0xFF;
96 hexChars
[j
* delta
] = HEX_ARRAY
[v
>>> 4];
97 hexChars
[j
* delta
+ 1] = HEX_ARRAY
[v
& 0x0F];
99 if (spaced
&& (j
!= bytes
.length
- 1)) {
100 hexChars
[j
* delta
+ 2] = separator
;
103 return new String(hexChars
);
106 // TODO Add little endian support
108 * Generate a string representing the MAC address.
111 * The MAC address as a byte array.
112 * @return The string representing the MAC address.
114 public static String
toMacAddress(byte[] mac
) {
116 if (mac
.length
!= EthernetIIValues
.MAC_ADDRESS_SIZE
) {
117 throw new IllegalArgumentException();
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$
129 // TODO Add little endian support
131 * Generate a string representing the IP address.
134 * The IP address as a byte array.
135 * @return The string representing the IP address.
137 public static String
toIpAddress(byte[] ip
) {
139 if (ip
.length
!= IPv4Values
.IP_ADDRESS_SIZE
) {
140 throw new IllegalArgumentException();
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);
150 // TODO support non GMT time.
153 * Convert a timestamp into a date.
156 * The timestamp. It represents the time since Epoch in
159 * The scale of the timestamp.
160 * @return The date as a string.
162 public static String
toGMTTime(long ts
, PcapTimestampScale scale
) {
166 timestamp
= ts
* 1000;
172 throw new IllegalArgumentException("The timestamp precision is not valid!"); //$NON-NLS-1$
174 return format(timestamp
);
178 * Format the timestamp to a string.
181 * the timestamp value to format (in ns)
182 * @return the formatted timestamp
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
190 Date dateObject
= new Date(date
);
192 StringBuilder sb
= new StringBuilder(DATE_FORMATTER
.format(dateObject
));
194 .append(String
.format("%03d", cs
)) //$NON-NLS-1$
196 .append(String
.format("%03d", ns
)); //$NON-NLS-1$
198 String string
= sb
.toString();
199 if (string
== null) {