1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.asterisk.manager.impl;
18
19 /***
20 * Utilitiy class with some static helper methods that are used in multiple
21 * contexts.<br>
22 * The methods for handling the internal action id are used to make sure we
23 * send unique ids to Asterisk even when the user of Asterisk-Java does not
24 * provide a unique action id or no action id at all.<br>
25 * All the methods contained in this class are supposed to be internally
26 * only.
27 *
28 * @author srt
29 * @version $Id: Util.java,v 1.1 2005/10/25 23:08:02 srt Exp $
30 */
31 public class Util
32 {
33 public static char INTERNAL_ACTION_ID_DELIMITER = '#';
34
35 /***
36 * The hex digits used to build a hex string representation of a byte array.
37 */
38 private static final char[] hexChar = {'0', '1', '2', '3', '4', '5', '6',
39 '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
40
41 /***
42 * Converts a byte array to a hex string representing it. The hex digits are
43 * lower case.
44 *
45 * @param b the byte array to convert
46 * @return the hex representation of b
47 */
48 public static String toHexString(byte[] b)
49 {
50 StringBuffer sb = new StringBuffer(b.length * 2);
51
52 for (int i = 0; i < b.length; i++)
53 {
54 sb.append(hexChar[(b[i] & 0xf0) >>> 4]);
55 sb.append(hexChar[b[i] & 0x0f]);
56 }
57
58 return sb.toString();
59 }
60
61 /***
62 * Returns the internal action id contained in the given action id.
63 *
64 * @param actionId the action id prefixed by the internal action
65 * id as received from Asterisk.
66 * @return the internal action id that has been added before.
67 * @see #addInternalActionId(String, String)
68 */
69 public static String getInternalActionId(String actionId)
70 {
71 int delimiterIndex;
72
73 delimiterIndex = actionId.indexOf(INTERNAL_ACTION_ID_DELIMITER);
74 if (delimiterIndex > 0)
75 {
76 return actionId.substring(0, delimiterIndex);
77 }
78 else
79 {
80 return null;
81 }
82 }
83
84 /***
85 * Strips the internal action id from the given action id.
86 *
87 * @param actionId the action id prefixed by the internal action
88 * id as received from Asterisk.
89 * @return the original action id, that is the action id as it was before
90 * the internal action id was added.
91 * @see #addInternalActionId(String, String)
92 */
93 public static String stripInternalActionId(String actionId)
94 {
95 int delimiterIndex;
96
97 delimiterIndex = actionId.indexOf(INTERNAL_ACTION_ID_DELIMITER);
98 if (delimiterIndex > 0)
99 {
100 if (actionId.length() > delimiterIndex + 1)
101 {
102 return actionId.substring(delimiterIndex + 1);
103 }
104 else
105 {
106 return null;
107 }
108 }
109 else
110 {
111 return null;
112 }
113 }
114
115 /***
116 * Adds the internal action id to the given action id.
117 *
118 * @param actionId the action id as set by the user.
119 * @param internalActionId the internal action id to add.
120 * @return the action id prefixed by the internal action id suitable to be
121 * sent to Asterisk.
122 */
123 public static String addInternalActionId(String actionId,
124 String internalActionId)
125 {
126 if (actionId == null)
127 {
128 return internalActionId + INTERNAL_ACTION_ID_DELIMITER;
129 }
130 else
131 {
132 return internalActionId + INTERNAL_ACTION_ID_DELIMITER + actionId;
133 }
134 }
135 }