Minor common util cleanup.
This commit is contained in:
@@ -18,111 +18,39 @@ package org.l2jmobius.commons.util;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
|
||||
* @author luisantonioa
|
||||
*/
|
||||
public class Util
|
||||
public class CommonUtil
|
||||
{
|
||||
protected static final Logger LOGGER = Logger.getLogger(Util.class.getName());
|
||||
|
||||
public static boolean isInternalIP(String ipAddress)
|
||||
{
|
||||
return ipAddress.startsWith("192.168.") || ipAddress.startsWith("10.") || ipAddress.startsWith("127.0.0.1");
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
int counter = 0;
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
if ((counter % 16) == 0)
|
||||
{
|
||||
result.append(fillHex(i, 4) + ": ");
|
||||
}
|
||||
|
||||
result.append(fillHex(data[i] & 0xff, 2) + " ");
|
||||
counter++;
|
||||
if (counter == 16)
|
||||
{
|
||||
result.append(" ");
|
||||
int charpoint = i - 15;
|
||||
for (int a = 0; a < 16; a++)
|
||||
{
|
||||
final int t1 = data[charpoint++];
|
||||
if ((t1 > 0x1f) && (t1 < 0x80))
|
||||
{
|
||||
result.append((char) t1);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.append('.');
|
||||
}
|
||||
}
|
||||
|
||||
result.append('\n');
|
||||
counter = 0;
|
||||
}
|
||||
}
|
||||
|
||||
final int rest = data.length % 16;
|
||||
if (rest > 0)
|
||||
{
|
||||
for (int i = 0; i < (17 - rest); i++)
|
||||
{
|
||||
result.append(" ");
|
||||
}
|
||||
|
||||
int charpoint = data.length - rest;
|
||||
for (int a = 0; a < rest; a++)
|
||||
{
|
||||
final int t1 = data[charpoint++];
|
||||
if ((t1 > 0x1f) && (t1 < 0x80))
|
||||
{
|
||||
result.append((char) t1);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.append('.');
|
||||
}
|
||||
}
|
||||
|
||||
result.append('\n');
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public static String fillHex(int data, int digits)
|
||||
{
|
||||
String number = Integer.toHexString(data);
|
||||
for (int i = number.length(); i < digits; i++)
|
||||
{
|
||||
number = "0" + number;
|
||||
}
|
||||
return number;
|
||||
}
|
||||
|
||||
public static void printSection(String section)
|
||||
{
|
||||
String s = "=[ " + section + " ]";
|
||||
while (s.length() < 61)
|
||||
{
|
||||
s = "-" + s;
|
||||
}
|
||||
LOGGER.info(s);
|
||||
return new String(HexUtils.bArr2HexEdChars(data, len));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param raw
|
||||
* @return
|
||||
* This call is equivalent to Util.printData(data, data.length)
|
||||
* @see CommonUtil#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);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -135,18 +63,6 @@ public class Util
|
||||
return minutesToConvert * 60000;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
final StringWriter sw = new StringWriter();
|
||||
t.printStackTrace(new PrintWriter(sw));
|
||||
return sw.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to generate a random sequence of bytes returned as byte array
|
||||
* @param size number of random bytes to generate
|
||||
@@ -168,6 +84,18 @@ public class Util
|
||||
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)
|
||||
{
|
||||
final StringWriter sw = new StringWriter();
|
||||
t.printStackTrace(new PrintWriter(sw));
|
||||
return sw.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param <T>
|
||||
* @param array - the array to look into
|
@@ -0,0 +1,269 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.l2jmobius.commons.util;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author HorridoJoho
|
||||
*/
|
||||
public class HexUtils
|
||||
{
|
||||
// lookup table for hex characters
|
||||
private static final char[] _NIBBLE_CHAR_LOOKUP =
|
||||
{
|
||||
'0',
|
||||
'1',
|
||||
'2',
|
||||
'3',
|
||||
'4',
|
||||
'5',
|
||||
'6',
|
||||
'7',
|
||||
'8',
|
||||
'9',
|
||||
'A',
|
||||
'B',
|
||||
'C',
|
||||
'D',
|
||||
'E',
|
||||
'F'
|
||||
};
|
||||
private static final char[] _NEW_LINE_CHARS = System.getProperty("line.separator").toCharArray();
|
||||
|
||||
/**
|
||||
* Method to generate the hexadecimal character presentation of a byte<br>
|
||||
* This call is equivalent to {@link HexUtils#b2HexChars(byte, char[], int)} with parameters (data, null, 0)
|
||||
* @param data byte to generate the hexadecimal character presentation from
|
||||
* @return a new char array with exactly 2 elements
|
||||
*/
|
||||
public static char[] b2HexChars(byte data)
|
||||
{
|
||||
return b2HexChars(data, null, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to generate the hexadecimal character presentation of a byte
|
||||
* @param data byte to generate the hexadecimal character presentation from
|
||||
* @param dstHexCharsValue the char array the hexadecimal character presentation should be copied to, if this is null, dstOffset is ignored and a new char array with 2 elements is created
|
||||
* @param dstOffsetValue offset at which the hexadecimal character presentation is copied to dstHexChars
|
||||
* @return the char array the hexadecimal character presentation was copied to
|
||||
*/
|
||||
public static char[] b2HexChars(byte data, char[] dstHexCharsValue, int dstOffsetValue)
|
||||
{
|
||||
char[] dstHexChars = dstHexCharsValue;
|
||||
int dstOffset = dstOffsetValue;
|
||||
if (dstHexChars == null)
|
||||
{
|
||||
dstHexChars = new char[2];
|
||||
dstOffset = 0;
|
||||
}
|
||||
|
||||
// /////////////////////////////
|
||||
// NIBBLE LOOKUP
|
||||
dstHexChars[dstOffset] = _NIBBLE_CHAR_LOOKUP[(data & 0xF0) >> 4];
|
||||
dstHexChars[dstOffset + 1] = _NIBBLE_CHAR_LOOKUP[data & 0x0F];
|
||||
|
||||
return dstHexChars;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to generate the hexadecimal character presentation of an integer This call is equivalent to {@link HexUtils#int2HexChars(int, char[], int)} with parameters (data, null, 0)
|
||||
* @param data integer to generate the hexadecimal character presentation from
|
||||
* @return new char array with 8 elements
|
||||
*/
|
||||
public static char[] int2HexChars(int data)
|
||||
{
|
||||
return int2HexChars(data, new char[8], 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to generate the hexadecimal character presentation of an integer
|
||||
* @param data integer to generate the hexadecimal character presentation from
|
||||
* @param dstHexCharsValue the char array the hexadecimal character presentation should be copied to, if this is null, dstOffset is ignored and a new char array with 8 elements is created
|
||||
* @param dstOffsetValue offset at which the hexadecimal character presentation is copied to dstHexChars
|
||||
* @return the char array the hexadecimal character presentation was copied to
|
||||
*/
|
||||
public static char[] int2HexChars(int data, char[] dstHexCharsValue, int dstOffsetValue)
|
||||
{
|
||||
char[] dstHexChars = dstHexCharsValue;
|
||||
int dstOffset = dstOffsetValue;
|
||||
if (dstHexChars == null)
|
||||
{
|
||||
dstHexChars = new char[8];
|
||||
dstOffset = 0;
|
||||
}
|
||||
|
||||
b2HexChars((byte) ((data & 0xFF000000) >> 24), dstHexChars, dstOffset);
|
||||
b2HexChars((byte) ((data & 0x00FF0000) >> 16), dstHexChars, dstOffset + 2);
|
||||
b2HexChars((byte) ((data & 0x0000FF00) >> 8), dstHexChars, dstOffset + 4);
|
||||
b2HexChars((byte) (data & 0x000000FF), dstHexChars, dstOffset + 6);
|
||||
|
||||
return dstHexChars;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to generate the hexadecimal character presentation of a byte array<br>
|
||||
* This call is equivalent to {@link HexUtils#bArr2HexChars(byte[], int, int, char[], int)} with parameters (data, offset, len, null, 0)
|
||||
* @param data byte array to generate the hexadecimal character presentation from
|
||||
* @param offset offset where to start in data array
|
||||
* @param len number of bytes to generate the hexadecimal character presentation from
|
||||
* @return a new char array with len*2 elements
|
||||
*/
|
||||
public static char[] bArr2HexChars(byte[] data, int offset, int len)
|
||||
{
|
||||
return bArr2HexChars(data, offset, len, null, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to generate the hexadecimal character presentation of a byte array
|
||||
* @param data byte array to generate the hexadecimal character presentation from
|
||||
* @param offset offset where to start in data array
|
||||
* @param len number of bytes to generate the hexadecimal character presentation from
|
||||
* @param dstHexCharsValue the char array the hexadecimal character presentation should be copied to, if this is null, dstOffset is ignored and a new char array with len*2 elements is created
|
||||
* @param dstOffsetValue offset at which the hexadecimal character presentation is copied to dstHexChars
|
||||
* @return the char array the hexadecimal character presentation was copied to
|
||||
*/
|
||||
public static char[] bArr2HexChars(byte[] data, int offset, int len, char[] dstHexCharsValue, int dstOffsetValue)
|
||||
{
|
||||
char[] dstHexChars = dstHexCharsValue;
|
||||
int dstOffset = dstOffsetValue;
|
||||
if (dstHexChars == null)
|
||||
{
|
||||
dstHexChars = new char[len * 2];
|
||||
dstOffset = 0;
|
||||
}
|
||||
|
||||
for (int dataIdx = offset, charsIdx = dstOffset; dataIdx < (len + offset); ++dataIdx, ++charsIdx)
|
||||
{
|
||||
// /////////////////////////////
|
||||
// NIBBLE LOOKUP, we duplicate the code from b2HexChars here, we want to save a few cycles(for charsIdx increment)
|
||||
dstHexChars[charsIdx] = _NIBBLE_CHAR_LOOKUP[(data[dataIdx] & 0xF0) >> 4];
|
||||
dstHexChars[++charsIdx] = _NIBBLE_CHAR_LOOKUP[data[dataIdx] & 0x0F];
|
||||
}
|
||||
|
||||
return dstHexChars;
|
||||
}
|
||||
|
||||
public static char[] bArr2AsciiChars(byte[] data, int offset, int len)
|
||||
{
|
||||
return bArr2AsciiChars(data, offset, len, new char[len], 0);
|
||||
}
|
||||
|
||||
public static char[] bArr2AsciiChars(byte[] data, int offset, int len, char[] dstAsciiCharsValue, int dstOffsetValue)
|
||||
{
|
||||
char[] dstAsciiChars = dstAsciiCharsValue;
|
||||
int dstOffset = dstOffsetValue;
|
||||
if (dstAsciiChars == null)
|
||||
{
|
||||
dstAsciiChars = new char[len];
|
||||
dstOffset = 0;
|
||||
}
|
||||
|
||||
for (int dataIdx = offset, charsIdx = dstOffset; dataIdx < (len + offset); ++dataIdx, ++charsIdx)
|
||||
{
|
||||
if ((data[dataIdx] > 0x1f) && (data[dataIdx] < 0x80))
|
||||
{
|
||||
dstAsciiChars[charsIdx] = (char) data[dataIdx];
|
||||
}
|
||||
else
|
||||
{
|
||||
dstAsciiChars[charsIdx] = '.';
|
||||
}
|
||||
}
|
||||
|
||||
return dstAsciiChars;
|
||||
}
|
||||
|
||||
private static final int HEX_ED_BPL = 16;
|
||||
private static final int HEX_ED_CPB = 2;
|
||||
|
||||
/**
|
||||
* Method to generate the hexadecimal character representation of a byte array like in a hex editor<br>
|
||||
* Line Format: {OFFSET} {HEXADECIMAL} {ASCII}({NEWLINE})<br>
|
||||
* {OFFSET} = offset of the first byte in line(8 chars)<br>
|
||||
* {HEXADECIMAL} = hexadecimal character representation({@link #HEX_ED_BPL}*2 chars)<br>
|
||||
* {ASCII} = ascii character presentation({@link #HEX_ED_BPL} chars)
|
||||
* @param data byte array to generate the hexadecimal character representation
|
||||
* @param len the number of bytes to generate the hexadecimal character representation from
|
||||
* @return byte array which contains the hexadecimal character representation of the given byte array
|
||||
*/
|
||||
public static char[] bArr2HexEdChars(byte[] data, int len)
|
||||
{
|
||||
// {OFFSET} {HEXADECIMAL} {ASCII}{NEWLINE}
|
||||
final int lineLength = 9 + (HEX_ED_BPL * HEX_ED_CPB) + 1 + HEX_ED_BPL + _NEW_LINE_CHARS.length;
|
||||
final int lenBplMod = len % HEX_ED_BPL;
|
||||
// create text buffer
|
||||
// 1. don't allocate a full last line if not _HEX_ED_BPL bytes are shown in last line
|
||||
// 2. no new line at end of buffer
|
||||
// BUG: when the length is multiple of _HEX_ED_BPL we erase the whole ascii space with this
|
||||
// char[] textData = new char[lineLength * numLines - (_HEX_ED_BPL - (len % _HEX_ED_BPL)) - _NEW_LINE_CHARS.length];
|
||||
// FIXED HERE
|
||||
int numLines;
|
||||
char[] textData;
|
||||
if (lenBplMod == 0)
|
||||
{
|
||||
numLines = len / HEX_ED_BPL;
|
||||
textData = new char[(lineLength * numLines) - _NEW_LINE_CHARS.length];
|
||||
}
|
||||
else
|
||||
{
|
||||
numLines = (len / HEX_ED_BPL) + 1;
|
||||
textData = new char[(lineLength * numLines) - (HEX_ED_BPL - (lenBplMod)) - _NEW_LINE_CHARS.length];
|
||||
}
|
||||
|
||||
// performance penalty, only doing space filling in the loop is faster
|
||||
// Arrays.fill(textData, ' ');
|
||||
|
||||
int dataOffset;
|
||||
int dataLen;
|
||||
int lineStart;
|
||||
int lineHexDataStart;
|
||||
int lineAsciiDataStart;
|
||||
for (int i = 0; i < numLines; ++i)
|
||||
{
|
||||
dataOffset = i * HEX_ED_BPL;
|
||||
dataLen = Math.min(len - dataOffset, HEX_ED_BPL);
|
||||
lineStart = i * lineLength;
|
||||
lineHexDataStart = lineStart + 9;
|
||||
lineAsciiDataStart = lineHexDataStart + (HEX_ED_BPL * HEX_ED_CPB) + 1;
|
||||
|
||||
int2HexChars(dataOffset, textData, lineStart); // the offset of this line
|
||||
textData[lineHexDataStart - 1] = ' '; // separate
|
||||
bArr2HexChars(data, dataOffset, dataLen, textData, lineHexDataStart); // the data in hex
|
||||
bArr2AsciiChars(data, dataOffset, dataLen, textData, lineAsciiDataStart); // the data in ascii
|
||||
|
||||
if (i < (numLines - 1))
|
||||
{
|
||||
textData[lineAsciiDataStart - 1] = ' '; // separate
|
||||
System.arraycopy(_NEW_LINE_CHARS, 0, textData, lineAsciiDataStart + HEX_ED_BPL, _NEW_LINE_CHARS.length); // the new line
|
||||
}
|
||||
else if (dataLen < HEX_ED_BPL)
|
||||
{
|
||||
// last line which shows less than _HEX_ED_BPL bytes
|
||||
final int lineHexDataEnd = lineHexDataStart + (dataLen * HEX_ED_CPB);
|
||||
Arrays.fill(textData, lineHexDataEnd, lineHexDataEnd + ((HEX_ED_BPL - dataLen) * HEX_ED_CPB) + 1, ' '); // spaces, for the last line if there are not _HEX_ED_BPL bytes
|
||||
}
|
||||
else
|
||||
{
|
||||
// last line which shows _HEX_ED_BPL bytes
|
||||
textData[lineAsciiDataStart - 1] = ' '; // separate
|
||||
}
|
||||
}
|
||||
return textData;
|
||||
}
|
||||
}
|
@@ -64,26 +64,29 @@ public class IPSubnet
|
||||
}
|
||||
}
|
||||
}
|
||||
// check for embedded v4 in v6 addr (not done !)
|
||||
else if (_isIPv4)
|
||||
{
|
||||
// my V4 vs V6
|
||||
for (int i = 0; i < _addr.length; i++)
|
||||
{
|
||||
if ((addr[i + 12] & _mask[i]) != _addr[i])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// my V6 vs V4
|
||||
for (int i = 0; i < _addr.length; i++)
|
||||
// check for embedded v4 in v6 addr (not done !)
|
||||
if (_isIPv4)
|
||||
{
|
||||
if ((addr[i] & _mask[i + 12]) != _addr[i + 12])
|
||||
// my V4 vs V6
|
||||
for (int i = 0; i < _addr.length; i++)
|
||||
{
|
||||
return false;
|
||||
if ((addr[i + 12] & _mask[i]) != _addr[i])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// my V6 vs V4
|
||||
for (int i = 0; i < _addr.length; i++)
|
||||
{
|
||||
if ((addr[i] & _mask[i + 12]) != _addr[i + 12])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -120,7 +123,11 @@ public class IPSubnet
|
||||
{
|
||||
return applyMask(((IPSubnet) o).getAddress());
|
||||
}
|
||||
return (o instanceof InetAddress) && applyMask(((InetAddress) o).getAddress());
|
||||
else if (o instanceof InetAddress)
|
||||
{
|
||||
return applyMask(((InetAddress) o).getAddress());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static byte[] getMask(int n, int maxLength) throws UnknownHostException
|
||||
|
@@ -25,10 +25,14 @@ import java.util.Locale;
|
||||
import org.l2jmobius.Config;
|
||||
|
||||
/**
|
||||
* String utilities optimized for the best performance.
|
||||
* String utilities optimized for the best performance.<br>
|
||||
* <h1>How to Use It</h1>
|
||||
* <h2>concat() or append()</h2> If concatenating strings in single call, use StringUtil.concat(), otherwise use StringUtil.append() and its variants.
|
||||
* <h2>Minimum Calls</h2> Bad:
|
||||
* <h2>concat() or append()</h2> If concatenating strings<br>
|
||||
* in single call, use StringUtil.concat(), otherwise use StringUtil.append()<br>
|
||||
* and its variants.<br>
|
||||
* <br>
|
||||
* <h2>Minimum Calls</h2><br>
|
||||
* Bad:
|
||||
*
|
||||
* <pre>
|
||||
* final StringBuilder sbString = new StringBuilder();
|
||||
@@ -44,8 +48,12 @@ import org.l2jmobius.Config;
|
||||
* </pre>
|
||||
*
|
||||
* Why?<br/>
|
||||
* Because the less calls you do, the less memory re-allocations have to be done so the whole text fits into the memory and less array copy tasks has to be performed. So if using less calls, less memory is used and string concatenation is faster.
|
||||
* <h2>Size Hints for Loops</h2> Bad:
|
||||
* Because the less calls you do, the less memory re-allocations have to be done<br>
|
||||
* so the whole text fits into the memory and less array copy tasks has to be<br>
|
||||
* performed. So if using less calls, less memory is used and string concatenation is faster.<br>
|
||||
* <br>
|
||||
* <h2>Size Hints for Loops</h2><br>
|
||||
* Bad:
|
||||
*
|
||||
* <pre>
|
||||
* final StringBuilder sbString = new StringBuilder();
|
||||
@@ -71,8 +79,10 @@ import org.l2jmobius.Config;
|
||||
* because new memory has not to be allocated on each cycle. Also it is much faster if no string copy tasks has to be performed. So if concatenating strings in a loop, count approximately the size and set it as the hint for the string builder size. It's better to make the size hint little bit larger
|
||||
* rather than smaller.<br/>
|
||||
* In case there is no text appended before the cycle, just use <code>new
|
||||
* StringBuilder(1300)</code>.
|
||||
* <h2>Concatenation and Constants</h2> Bad:
|
||||
* StringBuilder(1300)</code>.<br>
|
||||
* <br>
|
||||
* <h2>Concatenation and Constants</h2><br>
|
||||
* Bad:
|
||||
*
|
||||
* <pre>
|
||||
* StringUtil.concat("text 1 ", "text 2", String.valueOf(npcId));
|
||||
@@ -91,7 +101,7 @@ import org.l2jmobius.Config;
|
||||
* </pre>
|
||||
*
|
||||
* Why?<br/>
|
||||
* It saves some cycles when determining size of memory that needs to be allocated because less strings are passed to concat() method. But do not use + for concatenation of non-constant strings, that degrades performance and makes extra memory allocations needed.
|
||||
* It saves some cycles when determining size of memory that needs to be allocated because less strings are passed to concat() method. But do not use + for concatenation of non-constant strings, that degrades performance and makes extra memory allocations needed.<br>
|
||||
* <h2>Concatenation and Constant Variables</h2> Bad:
|
||||
*
|
||||
* <pre>
|
||||
@@ -108,8 +118,10 @@ import org.l2jmobius.Config;
|
||||
*
|
||||
* Why? Because when using <code>final</code> keyword, the <code>glue</code> is marked as constant string and compiler treats it as a constant string so it is able to create string "text1some gluetext2some glue" during the compilation. But this only works in case the value is known at compilation
|
||||
* time, so this cannot be used for cases like <code>final String objectIdString =
|
||||
* String.valueOf(getObjectId)</code>.
|
||||
* <h2>StringBuilder Reuse</h2> Bad:
|
||||
* String.valueOf(getObjectId)</code>.<br>
|
||||
* <br>
|
||||
* <h2>StringBuilder Reuse</h2><br>
|
||||
* Bad:
|
||||
*
|
||||
* <pre>
|
||||
* final StringBuilder sbString1 = new StringBuilder();
|
||||
@@ -132,10 +144,12 @@ import org.l2jmobius.Config;
|
||||
* Why?</br>
|
||||
* In first case, new memory has to be allocated for the second string. In second case already allocated memory is reused, but only in case the new string is not longer than the previously allocated string. Anyway, the second way is better because the string either fits in the memory and some memory
|
||||
* is saved, or it does not fit in the memory, and in that case it works as in the first case.
|
||||
* <h2>Primitives to Strings</h2> To convert primitives to string, use String.valueOf().
|
||||
* <h2>How much faster is it?</h2> Here are some results of my tests. Count is number of strings concatenated. Don't take the numbers as 100% true as the numbers are affected by other programs running on my computer at the same time. Anyway, from the results it is obvious that using StringBuilder
|
||||
* with predefined size is the fastest (and also most memory efficient) solution. It is about 5 times faster when concatenating 7 strings, compared to StringBuilder. Also, with more strings concatenated, the difference between StringBuilder and StringBuilder gets larger. In code, there are many
|
||||
* cases, where there are concatenated 50+ strings so the time saving is even greater.
|
||||
* <h2>Primitives to Strings</h2> To convert primitives to string, use String.valueOf().<br>
|
||||
* <br>
|
||||
* <h2>How much faster is it?</h2><br>
|
||||
* Here are some results of my tests. Count is number of strings concatenated. Don't take the numbers as 100% true as the numbers are affected by other programs running on my computer at the same time. Anyway, from the results it is obvious that using StringBuilder with predefined size is the
|
||||
* fastest (and also most memory efficient) solution. It is about 5 times faster when concatenating 7 strings, compared to TextBuilder. Also, with more strings concatenated, the difference between StringBuilder and TextBuilder gets larger. In code, there are many cases, where there are concatenated
|
||||
* 50+ strings so the time saving is even greater.<br>
|
||||
*
|
||||
* <pre>
|
||||
* Count: 2
|
||||
@@ -188,7 +202,6 @@ public class StringUtil
|
||||
* Concatenates strings.
|
||||
* @param strings strings to be concatenated
|
||||
* @return concatenated string
|
||||
* @see StringUtil
|
||||
*/
|
||||
public static String concat(String... strings)
|
||||
{
|
||||
@@ -205,7 +218,6 @@ public class StringUtil
|
||||
* @param sizeHint hint for string builder size allocation
|
||||
* @param strings strings to be appended
|
||||
* @return created string builder
|
||||
* @see StringUtil
|
||||
*/
|
||||
public static StringBuilder startAppend(int sizeHint, String... strings)
|
||||
{
|
||||
@@ -222,7 +234,6 @@ public class StringUtil
|
||||
* Appends strings to existing string builder.
|
||||
* @param sbString string builder
|
||||
* @param strings strings to be appended
|
||||
* @see StringUtil
|
||||
*/
|
||||
public static void append(StringBuilder sbString, String... strings)
|
||||
{
|
||||
@@ -251,19 +262,12 @@ public class StringUtil
|
||||
* @param strings array of strings
|
||||
* @return total length of all the strings
|
||||
*/
|
||||
private static int getLength(String[] strings)
|
||||
public static int getLength(String[] strings)
|
||||
{
|
||||
int length = 0;
|
||||
for (String string : strings)
|
||||
{
|
||||
if (string == null)
|
||||
{
|
||||
length += 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
length += string.length();
|
||||
}
|
||||
length += (string == null) ? 4 : string.length();
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
@@ -33,7 +33,6 @@ import org.l2jmobius.commons.enums.ServerMode;
|
||||
import org.l2jmobius.commons.util.Chronos;
|
||||
import org.l2jmobius.commons.util.DeadLockDetector;
|
||||
import org.l2jmobius.commons.util.PropertiesParser;
|
||||
import org.l2jmobius.commons.util.Util;
|
||||
import org.l2jmobius.gameserver.cache.CrestCache;
|
||||
import org.l2jmobius.gameserver.cache.HtmCache;
|
||||
import org.l2jmobius.gameserver.communitybbs.Manager.ForumsBBSManager;
|
||||
@@ -170,13 +169,13 @@ public class GameServer
|
||||
// Initialize config
|
||||
Config.load(ServerMode.GAME);
|
||||
|
||||
Util.printSection("Database");
|
||||
printSection("Database");
|
||||
DatabaseFactory.init();
|
||||
|
||||
Util.printSection("ThreadPool");
|
||||
printSection("ThreadPool");
|
||||
ThreadPool.init();
|
||||
|
||||
Util.printSection("IdManager");
|
||||
printSection("IdManager");
|
||||
IdManager.getInstance();
|
||||
if (!IdManager.hasInitialized())
|
||||
{
|
||||
@@ -192,7 +191,7 @@ public class GameServer
|
||||
CrestCache.getInstance();
|
||||
ScriptEngineManager.getInstance();
|
||||
|
||||
Util.printSection("World");
|
||||
printSection("World");
|
||||
World.getInstance();
|
||||
MapRegionData.getInstance();
|
||||
AnnouncementsTable.getInstance();
|
||||
@@ -205,7 +204,7 @@ public class GameServer
|
||||
CharNameTable.getInstance();
|
||||
ExperienceData.getInstance();
|
||||
|
||||
Util.printSection("Players");
|
||||
printSection("Players");
|
||||
PlayerTemplateData.getInstance();
|
||||
if (Config.ENABLE_CLASS_DAMAGE_SETTINGS)
|
||||
{
|
||||
@@ -217,7 +216,7 @@ public class GameServer
|
||||
ForumsBBSManager.getInstance().initRoot();
|
||||
}
|
||||
|
||||
Util.printSection("Skills");
|
||||
printSection("Skills");
|
||||
if (!SkillTable.getInstance().isInitialized())
|
||||
{
|
||||
LOGGER.info("Could not find the extraced files. Please Check Your Data.");
|
||||
@@ -233,7 +232,7 @@ public class GameServer
|
||||
}
|
||||
LOGGER.info("Skills: All skills loaded.");
|
||||
|
||||
Util.printSection("Items");
|
||||
printSection("Items");
|
||||
ItemTable.getInstance();
|
||||
ArmorSetData.getInstance();
|
||||
ExtractableItemData.getInstance();
|
||||
@@ -244,7 +243,7 @@ public class GameServer
|
||||
FishData.getInstance();
|
||||
}
|
||||
|
||||
Util.printSection("Npc");
|
||||
printSection("Npc");
|
||||
SchemeBufferTable.getInstance();
|
||||
WalkerRouteData.getInstance();
|
||||
if (!NpcTable.getInstance().isInitialized())
|
||||
@@ -253,24 +252,24 @@ public class GameServer
|
||||
throw new Exception("Could not initialize the npc table");
|
||||
}
|
||||
|
||||
Util.printSection("Geodata");
|
||||
printSection("Geodata");
|
||||
GeoEngine.getInstance();
|
||||
|
||||
Util.printSection("Economy");
|
||||
printSection("Economy");
|
||||
TradeManager.getInstance();
|
||||
MultisellData.getInstance();
|
||||
|
||||
Util.printSection("Clan Halls");
|
||||
printSection("Clan Halls");
|
||||
ClanHallManager.getInstance();
|
||||
FortressOfResistance.getInstance();
|
||||
DevastatedCastle.getInstance();
|
||||
BanditStrongholdSiege.getInstance();
|
||||
AuctionManager.getInstance();
|
||||
|
||||
Util.printSection("Zone");
|
||||
printSection("Zone");
|
||||
ZoneData.getInstance();
|
||||
|
||||
Util.printSection("Spawnlist");
|
||||
printSection("Spawnlist");
|
||||
if (!Config.ALT_DEV_NO_SPAWNS)
|
||||
{
|
||||
SpawnTable.getInstance();
|
||||
@@ -291,10 +290,10 @@ public class GameServer
|
||||
}
|
||||
DayNightSpawnManager.getInstance().notifyChangeMode();
|
||||
|
||||
Util.printSection("Dimensional Rift");
|
||||
printSection("Dimensional Rift");
|
||||
DimensionalRiftManager.getInstance();
|
||||
|
||||
Util.printSection("Misc");
|
||||
printSection("Misc");
|
||||
RecipeData.getInstance();
|
||||
RecipeManager.getInstance();
|
||||
EventDroplist.getInstance();
|
||||
@@ -313,41 +312,41 @@ public class GameServer
|
||||
ItemsAutoDestroyTaskManager.getInstance();
|
||||
}
|
||||
|
||||
Util.printSection("Manor");
|
||||
printSection("Manor");
|
||||
ManorSeedData.getInstance();
|
||||
CastleManorManager.getInstance();
|
||||
|
||||
Util.printSection("Castles");
|
||||
printSection("Castles");
|
||||
CastleManager.getInstance();
|
||||
SiegeManager.getInstance();
|
||||
FortManager.getInstance();
|
||||
FortSiegeManager.getInstance();
|
||||
CrownManager.getInstance();
|
||||
|
||||
Util.printSection("Boat");
|
||||
printSection("Boat");
|
||||
BoatData.getInstance();
|
||||
|
||||
Util.printSection("Doors");
|
||||
printSection("Doors");
|
||||
DoorData.getInstance().load();
|
||||
FenceData.getInstance();
|
||||
|
||||
Util.printSection("Four Sepulchers");
|
||||
printSection("Four Sepulchers");
|
||||
FourSepulchersManager.getInstance();
|
||||
|
||||
Util.printSection("Seven Signs");
|
||||
printSection("Seven Signs");
|
||||
SevenSigns.getInstance();
|
||||
SevenSignsFestival.getInstance();
|
||||
AutoSpawnHandler.getInstance();
|
||||
AutoChatHandler.getInstance();
|
||||
|
||||
Util.printSection("Olympiad System");
|
||||
printSection("Olympiad System");
|
||||
Olympiad.getInstance();
|
||||
Hero.getInstance();
|
||||
|
||||
Util.printSection("Access Levels");
|
||||
printSection("Access Levels");
|
||||
AdminData.getInstance();
|
||||
|
||||
Util.printSection("Handlers");
|
||||
printSection("Handlers");
|
||||
ItemHandler.getInstance();
|
||||
SkillHandler.getInstance();
|
||||
AdminCommandHandler.getInstance();
|
||||
@@ -367,7 +366,7 @@ public class GameServer
|
||||
CustomMailManager.getInstance();
|
||||
}
|
||||
|
||||
Util.printSection("Scripts");
|
||||
printSection("Scripts");
|
||||
if (!Config.ALT_DEV_NO_SCRIPT)
|
||||
{
|
||||
LOGGER.info("ScriptEngineManager: Loading server scripts:");
|
||||
@@ -380,7 +379,7 @@ public class GameServer
|
||||
}
|
||||
|
||||
/* QUESTS */
|
||||
Util.printSection("Quests");
|
||||
printSection("Quests");
|
||||
if (!Config.ALT_DEV_NO_QUESTS)
|
||||
{
|
||||
if (QuestManager.getInstance().getQuests().size() == 0)
|
||||
@@ -397,7 +396,7 @@ public class GameServer
|
||||
QuestManager.getInstance().unloadAllQuests();
|
||||
}
|
||||
|
||||
Util.printSection("Game Server");
|
||||
printSection("Game Server");
|
||||
|
||||
LOGGER.info("IdFactory: Free ObjectID's remaining: " + IdManager.size());
|
||||
|
||||
@@ -411,7 +410,7 @@ public class GameServer
|
||||
ThreadPool.scheduleAtFixedRate(PcPoint.getInstance(), Config.PCB_INTERVAL * 1000, Config.PCB_INTERVAL * 1000);
|
||||
}
|
||||
|
||||
Util.printSection("EventManager");
|
||||
printSection("EventManager");
|
||||
EventManager.getInstance().startEventRegistration();
|
||||
|
||||
if (EventManager.TVT_EVENT_ENABLED || EventManager.CTF_EVENT_ENABLED || EventManager.DM_EVENT_ENABLED)
|
||||
@@ -439,7 +438,7 @@ public class GameServer
|
||||
OfflineTraderTable.restoreOfflineTraders();
|
||||
}
|
||||
|
||||
Util.printSection("Protection");
|
||||
printSection("Protection");
|
||||
|
||||
if (Config.CHECK_SKILLS_ON_ENTER)
|
||||
{
|
||||
@@ -489,7 +488,7 @@ public class GameServer
|
||||
deadDetectThread.start();
|
||||
}
|
||||
|
||||
Util.printSection("Status");
|
||||
printSection("Status");
|
||||
|
||||
if (Config.IS_TELNET_ENABLED)
|
||||
{
|
||||
@@ -515,6 +514,16 @@ public class GameServer
|
||||
INSTANCE = new GameServer();
|
||||
}
|
||||
|
||||
private void printSection(String section)
|
||||
{
|
||||
String s = "=[ " + section + " ]";
|
||||
while (s.length() < 61)
|
||||
{
|
||||
s = "-" + s;
|
||||
}
|
||||
LOGGER.info(s);
|
||||
}
|
||||
|
||||
public static GameServer getInstance()
|
||||
{
|
||||
return INSTANCE;
|
||||
|
@@ -16,7 +16,7 @@
|
||||
*/
|
||||
package org.l2jmobius.gameserver.model.items;
|
||||
|
||||
import org.l2jmobius.commons.util.Util;
|
||||
import org.l2jmobius.commons.util.CommonUtil;
|
||||
import org.l2jmobius.gameserver.model.StatSet;
|
||||
import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
|
||||
|
||||
@@ -108,6 +108,6 @@ public class Henna
|
||||
*/
|
||||
public boolean canBeUsedBy(PlayerInstance player)
|
||||
{
|
||||
return Util.contains(_classes, player.getClassId().getId());
|
||||
return CommonUtil.contains(_classes, player.getClassId().getId());
|
||||
}
|
||||
}
|
@@ -23,7 +23,7 @@ import java.util.logging.LogRecord;
|
||||
|
||||
import org.l2jmobius.Config;
|
||||
import org.l2jmobius.commons.util.StringUtil;
|
||||
import org.l2jmobius.commons.util.Util;
|
||||
import org.l2jmobius.commons.util.CommonUtil;
|
||||
|
||||
public class ConsoleLogFormatter extends Formatter
|
||||
{
|
||||
@@ -39,7 +39,7 @@ public class ConsoleLogFormatter extends Formatter
|
||||
{
|
||||
try
|
||||
{
|
||||
StringUtil.append(output, Util.getStackTrace(record.getThrown()), Config.EOL);
|
||||
StringUtil.append(output, CommonUtil.getStackTrace(record.getThrown()), Config.EOL);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
Reference in New Issue
Block a user