Util.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.net.UnknownHostException;
  24. import java.nio.ByteBuffer;
  25. /**
  26. * This class ...
  27. *
  28. * @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
  29. */
  30. public class Util
  31. {
  32. public static boolean isInternalIP(String ipAddress)
  33. {
  34. java.net.InetAddress addr = null;
  35. try
  36. {
  37. addr = java.net.InetAddress.getByName(ipAddress);
  38. return addr.isSiteLocalAddress() || addr.isLoopbackAddress();
  39. }
  40. catch (UnknownHostException e)
  41. {
  42. e.printStackTrace();
  43. }
  44. return false;
  45. }
  46. public static String printData(byte[] data, int len)
  47. {
  48. final StringBuilder result = new StringBuilder(len * 4);
  49. int counter = 0;
  50. for (int i = 0; i < len; i++)
  51. {
  52. if (counter % 16 == 0)
  53. {
  54. result.append(fillHex(i, 4) + ": ");
  55. }
  56. result.append(fillHex(data[i] & 0xff, 2) + " ");
  57. counter++;
  58. if (counter == 16)
  59. {
  60. result.append(" ");
  61. int charpoint = i - 15;
  62. for (int a = 0; a < 16; a++)
  63. {
  64. int t1 = 0xFF & data[charpoint++];
  65. if (t1 > 0x1f && t1 < 0x80)
  66. {
  67. result.append((char) t1);
  68. }
  69. else
  70. {
  71. result.append('.');
  72. }
  73. }
  74. result.append("\n");
  75. counter = 0;
  76. }
  77. }
  78. int rest = data.length % 16;
  79. if (rest > 0)
  80. {
  81. for (int i = 0; i < 17 - rest; i++)
  82. {
  83. result.append(" ");
  84. }
  85. int charpoint = data.length - rest;
  86. for (int a = 0; a < rest; a++)
  87. {
  88. int t1 = 0xFF & data[charpoint++];
  89. if (t1 > 0x1f && t1 < 0x80)
  90. {
  91. result.append((char) t1);
  92. }
  93. else
  94. {
  95. result.append('.');
  96. }
  97. }
  98. result.append("\n");
  99. }
  100. return result.toString();
  101. }
  102. public static String fillHex(int data, int digits)
  103. {
  104. String number = Integer.toHexString(data);
  105. for (int i = number.length(); i < digits; i++)
  106. {
  107. number = "0" + number;
  108. }
  109. return number;
  110. }
  111. /**
  112. * @param raw
  113. * @return
  114. */
  115. public static String printData(byte[] raw)
  116. {
  117. return printData(raw, raw.length);
  118. }
  119. public static String printData(ByteBuffer buf)
  120. {
  121. byte[] data = new byte[buf.remaining()];
  122. buf.get(data);
  123. String hex = Util.printData(data, data.length);
  124. buf.position(buf.position() - data.length);
  125. return hex;
  126. }
  127. public static byte[] generateHex(int size)
  128. {
  129. byte[] array = new byte[size];
  130. Rnd.nextBytes(array);
  131. return array;
  132. }
  133. }