Util.java 4.8 KB

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