1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.asterisk.manager;
18
19 /***
20 * Utilitiy class with some static helper methods that are used in multiple contexts.
21 *
22 * @author srt
23 * @version $Id: Util.java,v 1.3 2005/04/21 23:34:40 srt Exp $
24 */
25 public class Util
26 {
27 public static char INTERNAL_ACTION_ID_DELIMITER = '#';
28
29 /***
30 * The hex digits used to build a hex string representation of a byte array.
31 */
32 private static final char[] hexChar = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
33 'f'};
34
35 /***
36 * Converts a byte array to a hex string representing it. The hex digits are lower case.
37 *
38 * @param b the byte array to convert
39 *
40 * @return the hex representation of b
41 */
42 public static String toHexString(byte[] b)
43 {
44 StringBuffer sb = new StringBuffer(b.length * 2);
45
46 for (int i = 0; i < b.length; i++)
47 {
48 sb.append(hexChar[(b[i] & 0xf0) >>> 4]);
49 sb.append(hexChar[b[i] & 0x0f]);
50 }
51
52 return sb.toString();
53 }
54
55 public static String getInternalActionId(String actionId)
56 {
57 int delimiterIndex;
58
59 delimiterIndex = actionId.indexOf(INTERNAL_ACTION_ID_DELIMITER);
60 if (delimiterIndex > 0)
61 {
62 return actionId.substring(0, delimiterIndex);
63 }
64 else
65 {
66 return null;
67 }
68 }
69
70 public static String stripInternalActionId(String actionId)
71 {
72 int delimiterIndex;
73
74 delimiterIndex = actionId.indexOf(INTERNAL_ACTION_ID_DELIMITER);
75 if (delimiterIndex > 0)
76 {
77 if (actionId.length() > delimiterIndex + 1)
78 {
79 return actionId.substring(delimiterIndex + 1);
80 }
81 else
82 {
83 return null;
84 }
85 }
86 else
87 {
88 return null;
89 }
90 }
91
92 public static String addInternalActionId(String actionId, String internalActionId)
93 {
94 if (actionId == null)
95 {
96 return internalActionId + INTERNAL_ACTION_ID_DELIMITER;
97 }
98 else
99 {
100 return internalActionId + INTERNAL_ACTION_ID_DELIMITER + actionId;
101 }
102 }
103 }