Util.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package com.tencent.common;
  2. import com.thoughtworks.xstream.XStream;
  3. import org.slf4j.LoggerFactory;
  4. import java.io.ByteArrayInputStream;
  5. import java.io.ByteArrayOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.lang.reflect.Field;
  9. import java.util.Map;
  10. /**
  11. * User: rizenguo
  12. * Date: 2014/10/23
  13. * Time: 14:59
  14. */
  15. public class Util {
  16. //打log用
  17. private static Log logger = new Log(LoggerFactory.getLogger(Util.class));
  18. /**
  19. * 通过反射的方式遍历对象的属性和属性值,方便调试
  20. *
  21. * @param o 要遍历的对象
  22. * @throws Exception
  23. */
  24. public static void reflect(Object o) throws Exception {
  25. Class cls = o.getClass();
  26. Field[] fields = cls.getDeclaredFields();
  27. for (int i = 0; i < fields.length; i++) {
  28. Field f = fields[i];
  29. f.setAccessible(true);
  30. Util.log(f.getName() + " -> " + f.get(o));
  31. }
  32. }
  33. public static byte[] readInput(InputStream in) throws IOException {
  34. ByteArrayOutputStream out = new ByteArrayOutputStream();
  35. int len = 0;
  36. byte[] buffer = new byte[1024];
  37. while ((len = in.read(buffer)) > 0) {
  38. out.write(buffer, 0, len);
  39. }
  40. out.close();
  41. in.close();
  42. return out.toByteArray();
  43. }
  44. public static String inputStreamToString(InputStream is) throws IOException {
  45. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  46. int i;
  47. while ((i = is.read()) != -1) {
  48. baos.write(i);
  49. }
  50. return baos.toString();
  51. }
  52. public static InputStream getStringStream(String sInputString) {
  53. ByteArrayInputStream tInputStringStream = null;
  54. if (sInputString != null && !sInputString.trim().equals("")) {
  55. tInputStringStream = new ByteArrayInputStream(sInputString.getBytes());
  56. }
  57. return tInputStringStream;
  58. }
  59. public static Object getObjectFromXML(String xml, Class tClass) {
  60. //将从API返回的XML数据映射到Java对象
  61. XStream xStreamForResponseData = new XStream();
  62. xStreamForResponseData.alias("xml", tClass);
  63. xStreamForResponseData.ignoreUnknownElements();//暂时忽略掉一些新增的字段
  64. return xStreamForResponseData.fromXML(xml);
  65. }
  66. public static String getStringFromMap(Map<String, Object> map, String key, String defaultValue) {
  67. if (key == "" || key == null) {
  68. return defaultValue;
  69. }
  70. String result = (String) map.get(key);
  71. if (result == null) {
  72. return defaultValue;
  73. } else {
  74. return result;
  75. }
  76. }
  77. public static int getIntFromMap(Map<String, Object> map, String key) {
  78. if (key == "" || key == null) {
  79. return 0;
  80. }
  81. if (map.get(key) == null) {
  82. return 0;
  83. }
  84. return Integer.parseInt((String) map.get(key));
  85. }
  86. /**
  87. * 打log接口
  88. * @param log 要打印的log字符串
  89. * @return 返回log
  90. */
  91. public static String log(Object log){
  92. logger.d(log.toString());
  93. //System.out.println(log);
  94. return log.toString();
  95. }
  96. /**
  97. * 读取本地的xml数据,一般用来自测用
  98. * @param localPath 本地xml文件路径
  99. * @return 读到的xml字符串
  100. */
  101. public static String getLocalXMLString(String localPath) throws IOException {
  102. return Util.inputStreamToString(Util.class.getResourceAsStream(localPath));
  103. }
  104. }