|
@@ -23,6 +23,7 @@ package com.l2jserver.util;
|
|
|
|
|
|
import java.io.PrintWriter;
|
|
|
import java.io.StringWriter;
|
|
|
+import java.net.InetAddress;
|
|
|
import java.net.UnknownHostException;
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
|
@@ -34,12 +35,20 @@ import java.nio.ByteBuffer;
|
|
|
|
|
|
public class Util
|
|
|
{
|
|
|
- public static boolean isInternalIP(String ipAddress)
|
|
|
+ /**
|
|
|
+ * Checks if a host name is internal
|
|
|
+ *
|
|
|
+ * @param host
|
|
|
+ * the host name to check
|
|
|
+ * @return
|
|
|
+ * true: host name is internal<br>
|
|
|
+ * false: host name is external
|
|
|
+ */
|
|
|
+ public static boolean isInternalHostname(String host)
|
|
|
{
|
|
|
- java.net.InetAddress addr = null;
|
|
|
try
|
|
|
{
|
|
|
- addr = java.net.InetAddress.getByName(ipAddress);
|
|
|
+ InetAddress addr = InetAddress.getByName(host);
|
|
|
return addr.isSiteLocalAddress() || addr.isLoopbackAddress();
|
|
|
}
|
|
|
catch (UnknownHostException e)
|
|
@@ -48,7 +57,18 @@ public class Util
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Method to generate the hexadecimal representation of a byte array.<br>
|
|
|
+ * 16 bytes per row, while ascii chars or "." is shown at the end of the line.
|
|
|
+ *
|
|
|
+ * @param data
|
|
|
+ * the byte array to be represented in hexadecimal representation
|
|
|
+ * @param len
|
|
|
+ * the number of bytes to represent in hexadecimal representation
|
|
|
+ * @return
|
|
|
+ * byte array represented in hexadecimal format
|
|
|
+ */
|
|
|
public static String printData(byte[] data, int len)
|
|
|
{
|
|
|
final StringBuilder result = new StringBuilder(len * 4);
|
|
@@ -115,7 +135,7 @@ public class Util
|
|
|
return result.toString();
|
|
|
}
|
|
|
|
|
|
- public static String fillHex(int data, int digits)
|
|
|
+ private static String fillHex(int data, int digits)
|
|
|
{
|
|
|
String number = Integer.toHexString(data);
|
|
|
|
|
@@ -128,14 +148,27 @@ public class Util
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param raw
|
|
|
+ * This call is equivalent to Util.printData(data, data.length)
|
|
|
+ * @see Util#printData(byte[],int)
|
|
|
+ *
|
|
|
+ * @param data
|
|
|
+ * data to represent in hexadecimal
|
|
|
* @return
|
|
|
+ * byte array represented in hexadecimal format
|
|
|
*/
|
|
|
- public static String printData(byte[] raw)
|
|
|
+ public static String printData(byte[] data)
|
|
|
{
|
|
|
- return printData(raw, raw.length);
|
|
|
+ return printData(data, data.length);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Method to represent the remaining bytes of a ByteBuffer as hexadecimal
|
|
|
+ *
|
|
|
+ * @param buf
|
|
|
+ * ByteBuffer to represent the remaining bytes of as hexadecimal
|
|
|
+ * @return
|
|
|
+ * hexadecimal representation of remaining bytes of the ByteBuffer
|
|
|
+ */
|
|
|
public static String printData(ByteBuffer buf)
|
|
|
{
|
|
|
byte[] data = new byte[buf.remaining()];
|
|
@@ -144,14 +177,30 @@ public class Util
|
|
|
buf.position(buf.position() - data.length);
|
|
|
return hex;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Method to generate a random sequence of bytes returned as byte array
|
|
|
+ *
|
|
|
+ * @param size
|
|
|
+ * number of random bytes to generate
|
|
|
+ * @return
|
|
|
+ * byte array with sequence of random bytes
|
|
|
+ */
|
|
|
public static byte[] generateHex(int size)
|
|
|
{
|
|
|
byte[] array = new byte[size];
|
|
|
Rnd.nextBytes(array);
|
|
|
return array;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Method to get the stack trace of a Throwable into a String
|
|
|
+ *
|
|
|
+ * @param t
|
|
|
+ * Throwable to get the stacktrace from
|
|
|
+ * @return
|
|
|
+ * stack trace from Throwable as String
|
|
|
+ */
|
|
|
public static String getStackTrace(Throwable t)
|
|
|
{
|
|
|
StringWriter sw = new StringWriter();
|