PageRenderTime 41ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/org/bouncycastle/util/Strings.java

https://bitbucket.org/devconcert1/textsecure
Java | 246 lines | 197 code | 31 blank | 18 comment | 46 complexity | 55adc74debca32386e6a8a78072b7105 MD5 | raw file
  1. package org.bouncycastle.util;
  2. import java.io.ByteArrayOutputStream;
  3. import java.util.Vector;
  4. public final class Strings
  5. {
  6. public static String fromUTF8ByteArray(byte[] bytes)
  7. {
  8. int i = 0;
  9. int length = 0;
  10. while (i < bytes.length)
  11. {
  12. length++;
  13. if ((bytes[i] & 0xf0) == 0xf0)
  14. {
  15. // surrogate pair
  16. length++;
  17. i += 4;
  18. }
  19. else if ((bytes[i] & 0xe0) == 0xe0)
  20. {
  21. i += 3;
  22. }
  23. else if ((bytes[i] & 0xc0) == 0xc0)
  24. {
  25. i += 2;
  26. }
  27. else
  28. {
  29. i += 1;
  30. }
  31. }
  32. char[] cs = new char[length];
  33. i = 0;
  34. length = 0;
  35. while (i < bytes.length)
  36. {
  37. char ch;
  38. if ((bytes[i] & 0xf0) == 0xf0)
  39. {
  40. int codePoint = ((bytes[i] & 0x03) << 18) | ((bytes[i+1] & 0x3F) << 12) | ((bytes[i+2] & 0x3F) << 6) | (bytes[i+3] & 0x3F);
  41. int U = codePoint - 0x10000;
  42. char W1 = (char)(0xD800 | (U >> 10));
  43. char W2 = (char)(0xDC00 | (U & 0x3FF));
  44. cs[length++] = W1;
  45. ch = W2;
  46. i += 4;
  47. }
  48. else if ((bytes[i] & 0xe0) == 0xe0)
  49. {
  50. ch = (char)(((bytes[i] & 0x0f) << 12)
  51. | ((bytes[i + 1] & 0x3f) << 6) | (bytes[i + 2] & 0x3f));
  52. i += 3;
  53. }
  54. else if ((bytes[i] & 0xd0) == 0xd0)
  55. {
  56. ch = (char)(((bytes[i] & 0x1f) << 6) | (bytes[i + 1] & 0x3f));
  57. i += 2;
  58. }
  59. else if ((bytes[i] & 0xc0) == 0xc0)
  60. {
  61. ch = (char)(((bytes[i] & 0x1f) << 6) | (bytes[i + 1] & 0x3f));
  62. i += 2;
  63. }
  64. else
  65. {
  66. ch = (char)(bytes[i] & 0xff);
  67. i += 1;
  68. }
  69. cs[length++] = ch;
  70. }
  71. return new String(cs);
  72. }
  73. public static byte[] toUTF8ByteArray(String string)
  74. {
  75. return toUTF8ByteArray(string.toCharArray());
  76. }
  77. public static byte[] toUTF8ByteArray(char[] string)
  78. {
  79. ByteArrayOutputStream bOut = new ByteArrayOutputStream();
  80. char[] c = string;
  81. int i = 0;
  82. while (i < c.length)
  83. {
  84. char ch = c[i];
  85. if (ch < 0x0080)
  86. {
  87. bOut.write(ch);
  88. }
  89. else if (ch < 0x0800)
  90. {
  91. bOut.write(0xc0 | (ch >> 6));
  92. bOut.write(0x80 | (ch & 0x3f));
  93. }
  94. // surrogate pair
  95. else if (ch >= 0xD800 && ch <= 0xDFFF)
  96. {
  97. // in error - can only happen, if the Java String class has a
  98. // bug.
  99. if (i + 1 >= c.length)
  100. {
  101. throw new IllegalStateException("invalid UTF-16 codepoint");
  102. }
  103. char W1 = ch;
  104. ch = c[++i];
  105. char W2 = ch;
  106. // in error - can only happen, if the Java String class has a
  107. // bug.
  108. if (W1 > 0xDBFF)
  109. {
  110. throw new IllegalStateException("invalid UTF-16 codepoint");
  111. }
  112. int codePoint = (((W1 & 0x03FF) << 10) | (W2 & 0x03FF)) + 0x10000;
  113. bOut.write(0xf0 | (codePoint >> 18));
  114. bOut.write(0x80 | ((codePoint >> 12) & 0x3F));
  115. bOut.write(0x80 | ((codePoint >> 6) & 0x3F));
  116. bOut.write(0x80 | (codePoint & 0x3F));
  117. }
  118. else
  119. {
  120. bOut.write(0xe0 | (ch >> 12));
  121. bOut.write(0x80 | ((ch >> 6) & 0x3F));
  122. bOut.write(0x80 | (ch & 0x3F));
  123. }
  124. i++;
  125. }
  126. return bOut.toByteArray();
  127. }
  128. /**
  129. * A locale independent version of toUpperCase.
  130. *
  131. * @param string input to be converted
  132. * @return a US Ascii uppercase version
  133. */
  134. public static String toUpperCase(String string)
  135. {
  136. boolean changed = false;
  137. char[] chars = string.toCharArray();
  138. for (int i = 0; i != chars.length; i++)
  139. {
  140. char ch = chars[i];
  141. if ('a' <= ch && 'z' >= ch)
  142. {
  143. changed = true;
  144. chars[i] = (char)(ch - 'a' + 'A');
  145. }
  146. }
  147. if (changed)
  148. {
  149. return new String(chars);
  150. }
  151. return string;
  152. }
  153. /**
  154. * A locale independent version of toLowerCase.
  155. *
  156. * @param string input to be converted
  157. * @return a US ASCII lowercase version
  158. */
  159. public static String toLowerCase(String string)
  160. {
  161. boolean changed = false;
  162. char[] chars = string.toCharArray();
  163. for (int i = 0; i != chars.length; i++)
  164. {
  165. char ch = chars[i];
  166. if ('A' <= ch && 'Z' >= ch)
  167. {
  168. changed = true;
  169. chars[i] = (char)(ch - 'A' + 'a');
  170. }
  171. }
  172. if (changed)
  173. {
  174. return new String(chars);
  175. }
  176. return string;
  177. }
  178. public static byte[] toByteArray(String string)
  179. {
  180. byte[] bytes = new byte[string.length()];
  181. for (int i = 0; i != bytes.length; i++)
  182. {
  183. char ch = string.charAt(i);
  184. bytes[i] = (byte)ch;
  185. }
  186. return bytes;
  187. }
  188. public static String[] split(String input, char delimiter)
  189. {
  190. Vector v = new Vector();
  191. boolean moreTokens = true;
  192. String subString;
  193. while (moreTokens)
  194. {
  195. int tokenLocation = input.indexOf(delimiter);
  196. if (tokenLocation > 0)
  197. {
  198. subString = input.substring(0, tokenLocation);
  199. v.addElement(subString);
  200. input = input.substring(tokenLocation + 1);
  201. }
  202. else
  203. {
  204. moreTokens = false;
  205. v.addElement(input);
  206. }
  207. }
  208. String[] res = new String[v.size()];
  209. for (int i = 0; i != res.length; i++)
  210. {
  211. res[i] = (String)v.elementAt(i);
  212. }
  213. return res;
  214. }
  215. }