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
.internal
.pcap
.core
.util
;
15 import java
.text
.DateFormat
;
16 import java
.text
.SimpleDateFormat
;
17 import java
.util
.Date
;
19 import org
.eclipse
.jdt
.annotation
.NonNull
;
20 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.protocol
.ethernet2
.EthernetIIValues
;
23 * Class for helping with the conversion of data.
25 * @author Vincent Perot
27 public final class ConversionHelper
{
29 @SuppressWarnings("null")
30 private static final @NonNull char[] HEX_ARRAY
= "0123456789abcdef".toCharArray(); //$NON-NLS-1$
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
);
35 private ConversionHelper() {
39 * Generate an integer from an unsigned byte.
43 * @return the integer representing the unsigned value.
45 public static int unsignedByteToInt(byte n
) {
46 return n
& 0x000000FF;
50 * Generate an integer from an unsigned short.
54 * @return the integer representing the unsigned value.
56 public static int unsignedShortToInt(short n
) {
57 return n
& 0x0000FFFF;
61 * Generate a long from an unsigned integer.
64 * the unsigned integer.
65 * @return the long representing the unsigned value.
67 public static long unsignedIntToLong(int n
) {
68 return n
& 0x00000000FFFFFFFFL
;
72 * Generate an hex number from a byte array.
77 * Whether there must be a space between each byte or not.
78 * @return the hex as a string.
80 public static String
bytesToHex(byte[] bytes
, boolean spaced
) {
81 // No need to check for character encoding since bytes represents a
84 if (bytes
.length
== 0) {
88 char[] hexChars
= spaced ?
new char[bytes
.length
* 3 - 1] : new char[bytes
.length
* 2];
89 int delta
= spaced ?
3 : 2;
92 for (int j
= 0; j
< bytes
.length
; j
++) {
94 int v
= bytes
[j
] & 0xFF;
95 hexChars
[j
* delta
] = HEX_ARRAY
[v
>>> 4];
96 hexChars
[j
* delta
+ 1] = HEX_ARRAY
[v
& 0x0F];
98 if (spaced
&& (j
!= bytes
.length
- 1)) {
99 hexChars
[j
* delta
+ 2] = separator
;
102 return new String(hexChars
);
105 // TODO Add little endian support
107 * Generate a string representing the MAC address.
110 * The MAC address as a byte array.
111 * @return The string representing the MAC address.
113 public static String
toMacAddress(byte[] mac
) {
115 if (mac
.length
!= EthernetIIValues
.MAC_ADDRESS_SIZE
) {
116 throw new IllegalArgumentException();
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$
128 // TODO support non GMT time.
131 * Convert a timestamp into a date.
134 * The timestamp. It represents the time since Epoch in
137 * The scale of the timestamp.
138 * @return The date as a string.
140 public static String
toGMTTime(long ts
, PcapTimestampScale scale
) {
144 timestamp
= ts
* 1000;
150 throw new IllegalArgumentException("The timestamp precision is not valid!"); //$NON-NLS-1$
152 return format(timestamp
);
156 * Format the timestamp to a string.
159 * the timestamp value to format (in ns)
160 * @return the formatted timestamp
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
168 Date dateObject
= new Date(date
);
170 StringBuilder sb
= new StringBuilder(DATE_FORMATTER
.format(dateObject
));
172 .append(String
.format("%03d", cs
)) //$NON-NLS-1$
174 .append(String
.format("%03d", ns
)); //$NON-NLS-1$
176 String string
= sb
.toString();
177 if (string
== null) {