2
0

Util.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * $Header: Util.java, 14-Jul-2005 03:27:51 luisantonioa Exp $
  3. *
  4. * $Author: luisantonioa $ $Date: 14-Jul-2005 03:27:51 $ $Revision: 1 $ $Log:
  5. * Util.java,v $ Revision 1 14-Jul-2005 03:27:51 luisantonioa Added copyright
  6. * notice
  7. *
  8. *
  9. * This program is free software: you can redistribute it and/or modify it under
  10. * the terms of the GNU General Public License as published by the Free Software
  11. * Foundation, either version 3 of the License, or (at your option) any later
  12. * version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  16. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU General Public License along with
  20. * this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. package com.l2jserver.util;
  23. import java.io.PrintWriter;
  24. import java.io.StringWriter;
  25. import java.net.UnknownHostException;
  26. import java.nio.ByteBuffer;
  27. /**
  28. * This class ...
  29. *
  30. * @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
  31. */
  32. public class Util
  33. {
  34. public static boolean isInternalIP(String ipAddress)
  35. {
  36. java.net.InetAddress addr = null;
  37. try
  38. {
  39. addr = java.net.InetAddress.getByName(ipAddress);
  40. return addr.isSiteLocalAddress() || addr.isLoopbackAddress();
  41. }
  42. catch (UnknownHostException e)
  43. {
  44. e.printStackTrace();
  45. }
  46. return false;
  47. }
  48. public static String printData(byte[] data, int len)
  49. {
  50. final StringBuilder result = new StringBuilder(len * 4);
  51. int counter = 0;
  52. for (int i = 0; i < len; i++)
  53. {
  54. if (counter % 16 == 0)
  55. {
  56. result.append(fillHex(i, 4) + ": ");
  57. }
  58. result.append(fillHex(data[i] & 0xff, 2) + " ");
  59. counter++;
  60. if (counter == 16)
  61. {
  62. result.append(" ");
  63. int charpoint = i - 15;
  64. for (int a = 0; a < 16; a++)
  65. {
  66. int t1 = 0xFF & data[charpoint++];
  67. if (t1 > 0x1f && t1 < 0x80)
  68. {
  69. result.append((char) t1);
  70. }
  71. else
  72. {
  73. result.append('.');
  74. }
  75. }
  76. result.append("\n");
  77. counter = 0;
  78. }
  79. }
  80. int rest = data.length % 16;
  81. if (rest > 0)
  82. {
  83. for (int i = 0; i < 17 - rest; i++)
  84. {
  85. result.append(" ");
  86. }
  87. int charpoint = data.length - rest;
  88. for (int a = 0; a < rest; a++)
  89. {
  90. int t1 = 0xFF & data[charpoint++];
  91. if (t1 > 0x1f && t1 < 0x80)
  92. {
  93. result.append((char) t1);
  94. }
  95. else
  96. {
  97. result.append('.');
  98. }
  99. }
  100. result.append("\n");
  101. }
  102. return result.toString();
  103. }
  104. public static String fillHex(int data, int digits)
  105. {
  106. String number = Integer.toHexString(data);
  107. for (int i = number.length(); i < digits; i++)
  108. {
  109. number = "0" + number;
  110. }
  111. return number;
  112. }
  113. /**
  114. * @param raw
  115. * @return
  116. */
  117. public static String printData(byte[] raw)
  118. {
  119. return printData(raw, raw.length);
  120. }
  121. public static String printData(ByteBuffer buf)
  122. {
  123. byte[] data = new byte[buf.remaining()];
  124. buf.get(data);
  125. String hex = Util.printData(data, data.length);
  126. buf.position(buf.position() - data.length);
  127. return hex;
  128. }
  129. public static byte[] generateHex(int size)
  130. {
  131. byte[] array = new byte[size];
  132. Rnd.nextBytes(array);
  133. return array;
  134. }
  135. public static String getStackTrace(Throwable t)
  136. {
  137. StringWriter sw = new StringWriter();
  138. t.printStackTrace(new PrintWriter(sw));
  139. return sw.toString();
  140. }
  141. }