Util.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. * @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
  25. */
  26. public class Util
  27. {
  28. private static final Logger _log = Logger.getLogger(Util.class.getName());
  29. /**
  30. * Checks if a host name is internal
  31. * @param host the host name to check
  32. * @return 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. _log.warning("Util: " + e.getMessage());
  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. * @param data the byte array to be represented in hexadecimal representation
  52. * @param len the number of bytes to represent in hexadecimal representation
  53. * @return byte array represented in hexadecimal format
  54. */
  55. public static String printData(byte[] data, int len)
  56. {
  57. final StringBuilder result = new StringBuilder(len * 4);
  58. int counter = 0;
  59. for (int i = 0; i < len; i++)
  60. {
  61. if ((counter % 16) == 0)
  62. {
  63. result.append(fillHex(i, 4) + ": ");
  64. }
  65. result.append(fillHex(data[i] & 0xff, 2) + " ");
  66. counter++;
  67. if (counter == 16)
  68. {
  69. result.append(" ");
  70. int charpoint = i - 15;
  71. for (int a = 0; a < 16; a++)
  72. {
  73. int t1 = 0xFF & data[charpoint++];
  74. if ((t1 > 0x1f) && (t1 < 0x80))
  75. {
  76. result.append((char) t1);
  77. }
  78. else
  79. {
  80. result.append('.');
  81. }
  82. }
  83. result.append('\n');
  84. counter = 0;
  85. }
  86. }
  87. int rest = data.length % 16;
  88. if (rest > 0)
  89. {
  90. for (int i = 0; i < (17 - rest); i++)
  91. {
  92. result.append(" ");
  93. }
  94. int charpoint = data.length - rest;
  95. for (int a = 0; a < rest; a++)
  96. {
  97. int t1 = 0xFF & data[charpoint++];
  98. if ((t1 > 0x1f) && (t1 < 0x80))
  99. {
  100. result.append((char) t1);
  101. }
  102. else
  103. {
  104. result.append('.');
  105. }
  106. }
  107. result.append('\n');
  108. }
  109. return result.toString();
  110. }
  111. private static String fillHex(int data, int digits)
  112. {
  113. String number = Integer.toHexString(data);
  114. for (int i = number.length(); i < digits; i++)
  115. {
  116. number = "0" + number;
  117. }
  118. return number;
  119. }
  120. /**
  121. * This call is equivalent to Util.printData(data, data.length)
  122. * @see Util#printData(byte[],int)
  123. * @param data data to represent in hexadecimal
  124. * @return byte array represented in hexadecimal format
  125. */
  126. public static String printData(byte[] data)
  127. {
  128. return printData(data, data.length);
  129. }
  130. /**
  131. * Method to represent the remaining bytes of a ByteBuffer as hexadecimal
  132. * @param buf ByteBuffer to represent the remaining bytes of as hexadecimal
  133. * @return hexadecimal representation of remaining bytes of the ByteBuffer
  134. */
  135. public static String printData(ByteBuffer buf)
  136. {
  137. byte[] data = new byte[buf.remaining()];
  138. buf.get(data);
  139. String hex = Util.printData(data, data.length);
  140. buf.position(buf.position() - data.length);
  141. return hex;
  142. }
  143. /**
  144. * Method to generate a random sequence of bytes returned as byte array
  145. * @param size number of random bytes to generate
  146. * @return byte array with sequence of random bytes
  147. */
  148. public static byte[] generateHex(int size)
  149. {
  150. byte[] array = new byte[size];
  151. Rnd.nextBytes(array);
  152. return array;
  153. }
  154. /**
  155. * Method to get the stack trace of a Throwable into a String
  156. * @param t Throwable to get the stacktrace from
  157. * @return stack trace from Throwable as String
  158. */
  159. public static String getStackTrace(Throwable t)
  160. {
  161. StringWriter sw = new StringWriter();
  162. t.printStackTrace(new PrintWriter(sw));
  163. return sw.toString();
  164. }
  165. }