Util.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. return new String(HexUtils.bArr2HexEdChars(data, len));
  58. }
  59. /**
  60. * This call is equivalent to Util.printData(data, data.length)
  61. * @see Util#printData(byte[],int)
  62. * @param data data to represent in hexadecimal
  63. * @return byte array represented in hexadecimal format
  64. */
  65. public static String printData(byte[] data)
  66. {
  67. return printData(data, data.length);
  68. }
  69. /**
  70. * Method to represent the remaining bytes of a ByteBuffer as hexadecimal
  71. * @param buf ByteBuffer to represent the remaining bytes of as hexadecimal
  72. * @return hexadecimal representation of remaining bytes of the ByteBuffer
  73. */
  74. public static String printData(ByteBuffer buf)
  75. {
  76. byte[] data = new byte[buf.remaining()];
  77. buf.get(data);
  78. String hex = Util.printData(data, data.length);
  79. buf.position(buf.position() - data.length);
  80. return hex;
  81. }
  82. /**
  83. * Method to generate a random sequence of bytes returned as byte array
  84. * @param size number of random bytes to generate
  85. * @return byte array with sequence of random bytes
  86. */
  87. public static byte[] generateHex(int size)
  88. {
  89. byte[] array = new byte[size];
  90. Rnd.nextBytes(array);
  91. return array;
  92. }
  93. /**
  94. * Method to get the stack trace of a Throwable into a String
  95. * @param t Throwable to get the stacktrace from
  96. * @return stack trace from Throwable as String
  97. */
  98. public static String getStackTrace(Throwable t)
  99. {
  100. StringWriter sw = new StringWriter();
  101. t.printStackTrace(new PrintWriter(sw));
  102. return sw.toString();
  103. }
  104. }