1
0

StringUtils.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * Copyright 2005 The Apache Software Foundation
  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. package org.apache.hadoop.util;
  17. import java.io.PrintWriter;
  18. import java.io.StringWriter;
  19. import java.net.URI;
  20. import java.net.URISyntaxException;
  21. import java.text.DecimalFormat;
  22. import org.apache.hadoop.fs.*;
  23. /**
  24. * General string utils
  25. * @author Owen O'Malley
  26. */
  27. public class StringUtils {
  28. /**
  29. * Make a string representation of the exception.
  30. * @param e The exception to stringify
  31. * @return A string with exception name and call stack.
  32. */
  33. public static String stringifyException(Throwable e) {
  34. StringWriter stm = new StringWriter();
  35. PrintWriter wrt = new PrintWriter(stm);
  36. e.printStackTrace(wrt);
  37. wrt.close();
  38. return stm.toString();
  39. }
  40. /**
  41. * Given a full hostname, return the word upto the first dot.
  42. * @param fullHostname the full hostname
  43. * @return the hostname to the first dot
  44. */
  45. public static String simpleHostname(String fullHostname) {
  46. int offset = fullHostname.indexOf('.');
  47. if (offset != -1) {
  48. return fullHostname.substring(0, offset);
  49. }
  50. return fullHostname;
  51. }
  52. private static DecimalFormat oneDecimal = new DecimalFormat("0.0");
  53. /**
  54. * Given an integer, return a string that is in an approximate, but human
  55. * readable format.
  56. * It uses the bases 'k', 'm', and 'g' for 1024, 1024**2, and 1024**3.
  57. * @param number the number to format
  58. * @return a human readable form of the integer
  59. */
  60. public static String humanReadableInt(long number) {
  61. long absNumber = Math.abs(number);
  62. double result = number;
  63. String suffix = "";
  64. if (absNumber < 1024) {
  65. // nothing
  66. } else if (absNumber < 1024 * 1024) {
  67. result = number / 1024.0;
  68. suffix = "k";
  69. } else if (absNumber < 1024 * 1024 * 1024) {
  70. result = number / (1024.0 * 1024);
  71. suffix = "m";
  72. } else {
  73. result = number / (1024.0 * 1024 * 1024);
  74. suffix = "g";
  75. }
  76. return oneDecimal.format(result) + suffix;
  77. }
  78. /**
  79. * Format a percentage for presentation to the user.
  80. * @param done the percentage to format (0.0 to 1.0)
  81. * @param digits the number of digits past the decimal point
  82. * @return a string representation of the percentage
  83. */
  84. public static String formatPercent(double done, int digits) {
  85. DecimalFormat percentFormat = new DecimalFormat("0.00%");
  86. double scale = Math.pow(10.0, digits+2);
  87. double rounded = Math.floor(done * scale);
  88. percentFormat.setDecimalSeparatorAlwaysShown(false);
  89. percentFormat.setMinimumFractionDigits(digits);
  90. percentFormat.setMaximumFractionDigits(digits);
  91. return percentFormat.format(rounded / scale);
  92. }
  93. /**
  94. * Given an array of strings, return a comma-separated list of its elements.
  95. * @param strs Array of strings
  96. * @return Empty string if strs.length is 0, comma separated list of strings
  97. * otherwise
  98. */
  99. public static String arrayToString(String[] strs) {
  100. if (strs.length == 0) { return ""; }
  101. StringBuffer sbuf = new StringBuffer();
  102. sbuf.append(strs[0]);
  103. for (int idx = 1; idx < strs.length; idx++) {
  104. sbuf.append(",");
  105. sbuf.append(strs[idx]);
  106. }
  107. return sbuf.toString();
  108. }
  109. /**
  110. * Given an array of bytes it will convert the bytes to a hex string
  111. * representation of the bytes
  112. * @param bytes
  113. * @return hex string representation of the byte array
  114. */
  115. public static String byteToHexString(byte bytes[]) {
  116. StringBuffer retString = new StringBuffer();
  117. for (int i = 0; i < bytes.length; ++i) {
  118. retString.append(Integer.toHexString(0x0100 + (bytes[i] & 0x00FF))
  119. .substring(1));
  120. }
  121. return retString.toString();
  122. }
  123. /**
  124. * Given a hexstring this will return the byte array corresponding to the
  125. * string
  126. * @param hex the hex String array
  127. * @return a byte array that is a hex string representation of the given
  128. * string. The size of the byte array is therefore hex.length/2
  129. */
  130. public static byte[] hexStringToByte(String hex) {
  131. byte[] bts = new byte[hex.length() / 2];
  132. for (int i = 0; i < bts.length; i++) {
  133. bts[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
  134. }
  135. return bts;
  136. }
  137. /**
  138. *
  139. * @param uris
  140. * @return
  141. */
  142. public static String uriToString(URI[] uris){
  143. String ret = null;
  144. ret = uris[0].toString();
  145. for(int i = 1; i < uris.length;i++){
  146. ret = ret + "," + uris[i].toString();
  147. }
  148. return ret;
  149. }
  150. /**
  151. *
  152. * @param str
  153. * @return
  154. */
  155. public static URI[] stringToURI(String[] str){
  156. if (str == null)
  157. return null;
  158. URI[] uris = new URI[str.length];
  159. for (int i = 0; i < str.length;i++){
  160. try{
  161. uris[i] = new URI(str[i]);
  162. }catch(URISyntaxException ur){
  163. System.out.println("Exception in specified URI's " + StringUtils.stringifyException(ur));
  164. //making sure its asssigned to null in case of an error
  165. uris[i] = null;
  166. }
  167. }
  168. return uris;
  169. }
  170. /**
  171. *
  172. * @param str
  173. * @return
  174. */
  175. public static Path[] stringToPath(String[] str){
  176. Path[] p = new Path[str.length];
  177. for (int i = 0; i < str.length;i++){
  178. p[i] = new Path(str[i]);
  179. }
  180. return p;
  181. }
  182. }