View Javadoc

1   /*
2    * Copyright  2004-2005 Stefan Reuter
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
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 }