Util.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * $Header: Util.java, 14-Jul-2005 03:27:51 luisantonioa Exp $
  3. *
  4. * $Author: luisantonioa $ $Date: 14-Jul-2005 03:27:51 $ $Revision: 1 $ $Log:
  5. * Util.java,v $ Revision 1 14-Jul-2005 03:27:51 luisantonioa Added copyright
  6. * notice
  7. *
  8. *
  9. * This program is free software: you can redistribute it and/or modify it under
  10. * the terms of the GNU General Public License as published by the Free Software
  11. * Foundation, either version 3 of the License, or (at your option) any later
  12. * version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  16. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU General Public License along with
  20. * this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. package com.l2jserver.util;
  23. import java.io.PrintWriter;
  24. import java.io.StringWriter;
  25. import java.net.InetAddress;
  26. import java.net.UnknownHostException;
  27. import java.nio.ByteBuffer;
  28. /**
  29. * This class ...
  30. *
  31. * @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
  32. */
  33. public class Util
  34. {
  35. /**
  36. * Checks if a host name is internal
  37. *
  38. * @param host
  39. * the host name to check
  40. * @return
  41. * true: host name is internal<br>
  42. * false: host name is external
  43. */
  44. public static boolean isInternalHostname(String host)
  45. {
  46. try
  47. {
  48. InetAddress addr = InetAddress.getByName(host);
  49. return addr.isSiteLocalAddress() || addr.isLoopbackAddress();
  50. }
  51. catch (UnknownHostException e)
  52. {
  53. e.printStackTrace();
  54. }
  55. return false;
  56. }
  57. /**
  58. * Method to generate the hexadecimal representation of a byte array.<br>
  59. * 16 bytes per row, while ascii chars or "." is shown at the end of the line.
  60. *
  61. * @param data
  62. * the byte array to be represented in hexadecimal representation
  63. * @param len
  64. * the number of bytes to represent in hexadecimal representation
  65. * @return
  66. * byte array represented in hexadecimal format
  67. */
  68. public static String printData(byte[] data, int len)
  69. {
  70. final StringBuilder result = new StringBuilder(len * 4);
  71. int counter = 0;
  72. for (int i = 0; i < len; i++)
  73. {
  74. if (counter % 16 == 0)
  75. {
  76. result.append(fillHex(i, 4) + ": ");
  77. }
  78. result.append(fillHex(data[i] & 0xff, 2) + " ");
  79. counter++;
  80. if (counter == 16)
  81. {
  82. result.append(" ");
  83. int charpoint = i - 15;
  84. for (int a = 0; a < 16; a++)
  85. {
  86. int t1 = 0xFF & data[charpoint++];
  87. if (t1 > 0x1f && t1 < 0x80)
  88. {
  89. result.append((char) t1);
  90. }
  91. else
  92. {
  93. result.append('.');
  94. }
  95. }
  96. result.append("\n");
  97. counter = 0;
  98. }
  99. }
  100. int rest = data.length % 16;
  101. if (rest > 0)
  102. {
  103. for (int i = 0; i < 17 - rest; i++)
  104. {
  105. result.append(" ");
  106. }
  107. int charpoint = data.length - rest;
  108. for (int a = 0; a < rest; a++)
  109. {
  110. int t1 = 0xFF & data[charpoint++];
  111. if (t1 > 0x1f && t1 < 0x80)
  112. {
  113. result.append((char) t1);
  114. }
  115. else
  116. {
  117. result.append('.');
  118. }
  119. }
  120. result.append("\n");
  121. }
  122. return result.toString();
  123. }
  124. private static String fillHex(int data, int digits)
  125. {
  126. String number = Integer.toHexString(data);
  127. for (int i = number.length(); i < digits; i++)
  128. {
  129. number = "0" + number;
  130. }
  131. return number;
  132. }
  133. /**
  134. * This call is equivalent to Util.printData(data, data.length)
  135. * @see Util#printData(byte[],int)
  136. *
  137. * @param data
  138. * data to represent in hexadecimal
  139. * @return
  140. * byte array represented in hexadecimal format
  141. */
  142. public static String printData(byte[] data)
  143. {
  144. return printData(data, data.length);
  145. }
  146. /**
  147. * Method to represent the remaining bytes of a ByteBuffer as hexadecimal
  148. *
  149. * @param buf
  150. * ByteBuffer to represent the remaining bytes of as hexadecimal
  151. * @return
  152. * hexadecimal representation of remaining bytes of the ByteBuffer
  153. */
  154. public static String printData(ByteBuffer buf)
  155. {
  156. byte[] data = new byte[buf.remaining()];
  157. buf.get(data);
  158. String hex = Util.printData(data, data.length);
  159. buf.position(buf.position() - data.length);
  160. return hex;
  161. }
  162. /**
  163. * Method to generate a random sequence of bytes returned as byte array
  164. *
  165. * @param size
  166. * number of random bytes to generate
  167. * @return
  168. * byte array with sequence of random bytes
  169. */
  170. public static byte[] generateHex(int size)
  171. {
  172. byte[] array = new byte[size];
  173. Rnd.nextBytes(array);
  174. return array;
  175. }
  176. /**
  177. * Method to get the stack trace of a Throwable into a String
  178. *
  179. * @param t
  180. * Throwable to get the stacktrace from
  181. * @return
  182. * stack trace from Throwable as String
  183. */
  184. public static String getStackTrace(Throwable t)
  185. {
  186. StringWriter sw = new StringWriter();
  187. t.printStackTrace(new PrintWriter(sw));
  188. return sw.toString();
  189. }
  190. }