Util.java 4.8 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. import java.util.logging.Logger;
  20. /**
  21. * This class ...
  22. *
  23. * @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
  24. */
  25. public class Util
  26. {
  27. private static final Logger _log = Logger.getLogger(Util.class.getName());
  28. /**
  29. * Checks if a host name is internal
  30. *
  31. * @param host
  32. * the host name to check
  33. * @return
  34. * true: host name is internal<br>
  35. * false: host name is external
  36. */
  37. public static boolean isInternalHostname(String host)
  38. {
  39. try
  40. {
  41. InetAddress addr = InetAddress.getByName(host);
  42. return addr.isSiteLocalAddress() || addr.isLoopbackAddress();
  43. }
  44. catch (UnknownHostException e)
  45. {
  46. _log.warning("Util: " + e.getMessage());
  47. }
  48. return false;
  49. }
  50. /**
  51. * Method to generate the hexadecimal representation of a byte array.<br>
  52. * 16 bytes per row, while ascii chars or "." is shown at the end of the line.
  53. *
  54. * @param data
  55. * the byte array to be represented in hexadecimal representation
  56. * @param len
  57. * the number of bytes to represent in hexadecimal representation
  58. * @return
  59. * byte array represented in hexadecimal format
  60. */
  61. public static String printData(byte[] data, int len)
  62. {
  63. final StringBuilder result = new StringBuilder(len * 4);
  64. int counter = 0;
  65. for (int i = 0; i < len; i++)
  66. {
  67. if (counter % 16 == 0)
  68. {
  69. result.append(fillHex(i, 4) + ": ");
  70. }
  71. result.append(fillHex(data[i] & 0xff, 2) + " ");
  72. counter++;
  73. if (counter == 16)
  74. {
  75. result.append(" ");
  76. int charpoint = i - 15;
  77. for (int a = 0; a < 16; a++)
  78. {
  79. int t1 = 0xFF & data[charpoint++];
  80. if (t1 > 0x1f && t1 < 0x80)
  81. {
  82. result.append((char) t1);
  83. }
  84. else
  85. {
  86. result.append('.');
  87. }
  88. }
  89. result.append("\n");
  90. counter = 0;
  91. }
  92. }
  93. int rest = data.length % 16;
  94. if (rest > 0)
  95. {
  96. for (int i = 0; i < 17 - rest; i++)
  97. {
  98. result.append(" ");
  99. }
  100. int charpoint = data.length - rest;
  101. for (int a = 0; a < rest; a++)
  102. {
  103. int t1 = 0xFF & data[charpoint++];
  104. if (t1 > 0x1f && t1 < 0x80)
  105. {
  106. result.append((char) t1);
  107. }
  108. else
  109. {
  110. result.append('.');
  111. }
  112. }
  113. result.append("\n");
  114. }
  115. return result.toString();
  116. }
  117. private static String fillHex(int data, int digits)
  118. {
  119. String number = Integer.toHexString(data);
  120. for (int i = number.length(); i < digits; i++)
  121. {
  122. number = "0" + number;
  123. }
  124. return number;
  125. }
  126. /**
  127. * This call is equivalent to Util.printData(data, data.length)
  128. * @see Util#printData(byte[],int)
  129. *
  130. * @param data
  131. * data to represent in hexadecimal
  132. * @return
  133. * byte array represented in hexadecimal format
  134. */
  135. public static String printData(byte[] data)
  136. {
  137. return printData(data, data.length);
  138. }
  139. /**
  140. * Method to represent the remaining bytes of a ByteBuffer as hexadecimal
  141. *
  142. * @param buf
  143. * ByteBuffer to represent the remaining bytes of as hexadecimal
  144. * @return
  145. * hexadecimal representation of remaining bytes of the ByteBuffer
  146. */
  147. public static String printData(ByteBuffer buf)
  148. {
  149. byte[] data = new byte[buf.remaining()];
  150. buf.get(data);
  151. String hex = Util.printData(data, data.length);
  152. buf.position(buf.position() - data.length);
  153. return hex;
  154. }
  155. /**
  156. * Method to generate a random sequence of bytes returned as byte array
  157. *
  158. * @param size
  159. * number of random bytes to generate
  160. * @return
  161. * byte array with sequence of random bytes
  162. */
  163. public static byte[] generateHex(int size)
  164. {
  165. byte[] array = new byte[size];
  166. Rnd.nextBytes(array);
  167. return array;
  168. }
  169. /**
  170. * Method to get the stack trace of a Throwable into a String
  171. * @param t Throwable to get the stacktrace from
  172. * @return stack trace from Throwable as String
  173. */
  174. public static String getStackTrace(Throwable t)
  175. {
  176. StringWriter sw = new StringWriter();
  177. t.printStackTrace(new PrintWriter(sw));
  178. return sw.toString();
  179. }
  180. }