Util.java 4.7 KB

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