View Javadoc

1   /* ***** BEGIN LICENSE BLOCK *****
2    * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3    *
4    * The contents of this file are subject to the Mozilla Public License Version
5    * 1.1 (the "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    * http://www.mozilla.org/MPL/
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis,
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11   * for the specific language governing rights and limitations under the
12   * License.
13   *
14   * The Original Code is "SMS Library for the Java platform".
15   *
16   * The Initial Developer of the Original Code is Markus Eriksson.
17   * Portions created by the Initial Developer are Copyright (C) 2002
18   * the Initial Developer. All Rights Reserved.
19   *
20   * Contributor(s):
21   *
22   * Alternatively, the contents of this file may be used under the terms of
23   * either the GNU General Public License Version 2 or later (the "GPL"), or
24   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25   * in which case the provisions of the GPL or the LGPL are applicable instead
26   * of those above. If you wish to allow use of your version of this file only
27   * under the terms of either the GPL or the LGPL, and not to allow others to
28   * use your version of this file under the terms of the MPL, indicate your
29   * decision by deleting the provisions above and replace them with the notice
30   * and other provisions required by the GPL or the LGPL. If you do not delete
31   * the provisions above, a recipient may use your version of this file under
32   * the terms of any one of the MPL, the GPL or the LGPL.
33   *
34   * ***** END LICENSE BLOCK ***** */
35  package org.marre.util;
36  
37  import java.util.Random;
38  
39  /***
40   * Various functions to encode and decode strings.
41   * 
42   * @author Markus Eriksson
43   */
44  public final class StringUtil
45  {
46      private static final char[] RANDOM_CHARS = "abcdefghijklmnopqrstuvwxyz1234567890".toCharArray();
47  
48      private static Random myRandom = new Random();
49  
50      /***
51       * This class isn't intended to be instantiated.
52       */
53      private StringUtil()
54      {
55      }
56  
57      /***
58       * 
59       * @param stringTable
60       * @param text
61       * @return
62       */
63      public static int findString(String[] stringTable, String text)
64      {
65          if (stringTable != null)
66          {
67              for (int i = 0; i < stringTable.length; i++)
68              {
69                  if ((stringTable[i] != null) && (stringTable[i].equals(text)))
70                  {
71                      return i;
72                  }
73              }
74          }
75          return -1;
76      }
77  
78      /***
79       * Converts a byte array to a string with hex values.
80       * 
81       * @param theData
82       *            Data to convert
83       * @return the encoded string
84       */
85      public static String bytesToHexString(byte[] theData)
86      {
87          StringBuffer hexStrBuff = new StringBuffer(theData.length * 2);
88  
89          for (int i = 0; i < theData.length; i++)
90          {
91              String hexByteStr = Integer.toHexString(theData[i] & 0xff).toUpperCase();
92              if (hexByteStr.length() == 1)
93              {
94                  hexStrBuff.append("0");
95              }
96              hexStrBuff.append(hexByteStr);
97          }
98  
99          return hexStrBuff.toString();
100     }
101 
102     /***
103      * Converts a byte to a string with hex values.
104      * 
105      * @param theByte
106      *            Byte to convert
107      * @return the encoded string
108      */
109     public static String byteToHexString(byte theByte)
110     {
111         StringBuffer hexStrBuff = new StringBuffer(2);
112 
113         String hexByteStr = Integer.toHexString(theByte & 0xff).toUpperCase();
114         if (hexByteStr.length() == 1)
115         {
116             hexStrBuff.append("0");
117         }
118         hexStrBuff.append(hexByteStr);
119 
120         return hexStrBuff.toString();
121     }
122 
123     /***
124      * Converts a string of hex characters to a byte array.
125      * 
126      * @param theHexString
127      *            The hex string to read
128      * @return the resulting byte array
129      */
130     public static byte[] hexStringToBytes(String theHexString)
131     {
132         byte[] data = new byte[theHexString.length() / 2];
133 
134         for (int i = 0; i < data.length; i++)
135         {
136             String a = theHexString.substring(i * 2, i * 2 + 2);
137             data[i] = (byte) Integer.parseInt(a, 16);
138         }
139 
140         return data;
141     }
142 
143     /***
144      * Method intToString.
145      * 
146      * Converst an integer to nChars characters
147      * 
148      * @param value
149      *            Integer value
150      * @param nChars
151      *            Number of chars to represent the "value"
152      * @return String The string representing "value"
153      */
154     public static String intToString(int value, int nChars)
155     {
156         String strValue = Integer.toString(value);
157         StringBuffer strBuf = new StringBuffer(nChars);
158 
159         for (int i = strValue.length(); i < nChars; i++)
160         {
161             strBuf.append('0');
162         }
163         strBuf.append(strValue);
164 
165         return strBuf.toString();
166     }
167 
168     /***
169      * Generates a random string of the given length.
170      * 
171      * "abcdefghijklmnopqrstuvwxyz1234567890"
172      * 
173      * @param length
174      * @return A random string
175      */
176     public static String randString(int length)
177     {
178         StringBuffer sb = new StringBuffer(length);
179 
180         for (int i = 0; i < length; i++)
181         {
182             sb.append(RANDOM_CHARS[myRandom.nextInt(RANDOM_CHARS.length)]);
183         }
184 
185         return sb.toString();
186     }
187 }