Util.java 3.2 KB

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