Util.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.util;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.io.PrintWriter;
  23. import java.io.StringWriter;
  24. import java.net.InetAddress;
  25. import java.net.UnknownHostException;
  26. import java.nio.ByteBuffer;
  27. import java.util.logging.Logger;
  28. /**
  29. * Useful utilities common to L2J Server.
  30. */
  31. public final class Util
  32. {
  33. private static final Logger _log = Logger.getLogger(Util.class.getName());
  34. private static final char[] ILLEGAL_CHARACTERS =
  35. {
  36. '/',
  37. '\n',
  38. '\r',
  39. '\t',
  40. '\0',
  41. '\f',
  42. '`',
  43. '?',
  44. '*',
  45. '\\',
  46. '<',
  47. '>',
  48. '|',
  49. '\"',
  50. ':'
  51. };
  52. /**
  53. * Checks if a host name is internal
  54. * @param host the host name to check
  55. * @return true: host name is internal<br>
  56. * false: host name is external
  57. */
  58. public static boolean isInternalHostname(String host)
  59. {
  60. try
  61. {
  62. InetAddress addr = InetAddress.getByName(host);
  63. return addr.isSiteLocalAddress() || addr.isLoopbackAddress();
  64. }
  65. catch (UnknownHostException e)
  66. {
  67. _log.warning("Util: " + e.getMessage());
  68. }
  69. return false;
  70. }
  71. /**
  72. * Method to generate the hexadecimal representation of a byte array.<br>
  73. * 16 bytes per row, while ascii chars or "." is shown at the end of the line.
  74. * @param data the byte array to be represented in hexadecimal representation
  75. * @param len the number of bytes to represent in hexadecimal representation
  76. * @return byte array represented in hexadecimal format
  77. */
  78. public static String printData(byte[] data, int len)
  79. {
  80. return new String(HexUtils.bArr2HexEdChars(data, len));
  81. }
  82. /**
  83. * This call is equivalent to Util.printData(data, data.length)
  84. * @see Util#printData(byte[],int)
  85. * @param data data to represent in hexadecimal
  86. * @return byte array represented in hexadecimal format
  87. */
  88. public static String printData(byte[] data)
  89. {
  90. return printData(data, data.length);
  91. }
  92. /**
  93. * Method to represent the remaining bytes of a ByteBuffer as hexadecimal
  94. * @param buf ByteBuffer to represent the remaining bytes of as hexadecimal
  95. * @return hexadecimal representation of remaining bytes of the ByteBuffer
  96. */
  97. public static String printData(ByteBuffer buf)
  98. {
  99. byte[] data = new byte[buf.remaining()];
  100. buf.get(data);
  101. String hex = Util.printData(data, data.length);
  102. buf.position(buf.position() - data.length);
  103. return hex;
  104. }
  105. /**
  106. * Method to generate a random sequence of bytes returned as byte array
  107. * @param size number of random bytes to generate
  108. * @return byte array with sequence of random bytes
  109. */
  110. public static byte[] generateHex(int size)
  111. {
  112. byte[] array = new byte[size];
  113. Rnd.nextBytes(array);
  114. return array;
  115. }
  116. /**
  117. * Method to get the stack trace of a Throwable into a String
  118. * @param t Throwable to get the stacktrace from
  119. * @return stack trace from Throwable as String
  120. */
  121. public static String getStackTrace(Throwable t)
  122. {
  123. StringWriter sw = new StringWriter();
  124. t.printStackTrace(new PrintWriter(sw));
  125. return sw.toString();
  126. }
  127. /**
  128. * Replaces most invalid characters for the given string with an underscore.
  129. * @param str the string that may contain invalid characters
  130. * @return the string with invalid character replaced by underscores
  131. */
  132. public static String replaceIllegalCharacters(String str)
  133. {
  134. String valid = str;
  135. for (char c : ILLEGAL_CHARACTERS)
  136. {
  137. valid = valid.replace(c, '_');
  138. }
  139. return valid;
  140. }
  141. /**
  142. * Verify if a file name is valid.
  143. * @param name the name of the file
  144. * @return {@code true} if the file name is valid, {@code false} otherwise
  145. */
  146. public static boolean isValidFileName(String name)
  147. {
  148. final File f = new File(name);
  149. try
  150. {
  151. f.getCanonicalPath();
  152. return true;
  153. }
  154. catch (IOException e)
  155. {
  156. return false;
  157. }
  158. }
  159. /**
  160. * Split words with a space.
  161. * @param input the string to split
  162. * @return the split string
  163. */
  164. public static String splitWords(String input)
  165. {
  166. return input.replaceAll("(\\p{Ll})(\\p{Lu})", "$1 $2");
  167. }
  168. }