mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-03 16:49:11 +00:00
Optimise definition decoding for faster start-up.
This commit is contained in:
@@ -29,6 +29,7 @@ public final class CompressionUtil {
|
||||
*/
|
||||
public static byte[] bzip2(byte[] uncompressed) throws IOException {
|
||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
|
||||
try (BZip2CompressorOutputStream os = new BZip2CompressorOutputStream(bout, 1)) {
|
||||
os.write(uncompressed);
|
||||
os.finish();
|
||||
@@ -40,22 +41,6 @@ public final class CompressionUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gzips the specified array.
|
||||
*
|
||||
* @param uncompressed The uncompressed array.
|
||||
* @return The compressed array.
|
||||
* @throws IOException If there is an error compressing the array.
|
||||
*/
|
||||
public static byte[] gzip(byte[] uncompressed) throws IOException {
|
||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
try (DeflaterOutputStream os = new GZIPOutputStream(bout)) {
|
||||
os.write(uncompressed);
|
||||
os.finish();
|
||||
return bout.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Debzip2s the compressed array and places the result into the decompressed array.
|
||||
*
|
||||
@@ -90,27 +75,44 @@ public final class CompressionUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Degzips the compressed {@link ByteBuffer} and returns the result as a byte array.
|
||||
* Degzips <strong>all</strong> of the datain the specified {@link ByteBuffer}.
|
||||
*
|
||||
* @param compressed The compressed buffer.
|
||||
* @return The decompressed array.
|
||||
* @throws IOException If there is an error decompressing the buffer.
|
||||
*/
|
||||
public static byte[] degzip(ByteBuffer compressed) throws IOException {
|
||||
byte[] data = new byte[compressed.remaining()];
|
||||
compressed.get(data);
|
||||
try (InputStream is = new GZIPInputStream(new ByteArrayInputStream(compressed.array()));
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
|
||||
byte[] buffer = new byte[1024];
|
||||
|
||||
try (InputStream is = new GZIPInputStream(new ByteArrayInputStream(data)); ByteArrayOutputStream os = new ByteArrayOutputStream()) {
|
||||
while (true) {
|
||||
byte[] buf = new byte[1024];
|
||||
int read = is.read(buf, 0, buf.length);
|
||||
int read = is.read(buffer, 0, buffer.length);
|
||||
if (read == -1) {
|
||||
break;
|
||||
}
|
||||
os.write(buf, 0, read);
|
||||
|
||||
out.write(buffer, 0, read);
|
||||
}
|
||||
|
||||
return os.toByteArray();
|
||||
return out.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gzips the specified array.
|
||||
*
|
||||
* @param uncompressed The uncompressed array.
|
||||
* @return The compressed array.
|
||||
* @throws IOException If there is an error compressing the array.
|
||||
*/
|
||||
public static byte[] gzip(byte[] uncompressed) throws IOException {
|
||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
|
||||
try (DeflaterOutputStream os = new GZIPOutputStream(bout)) {
|
||||
os.write(uncompressed);
|
||||
os.finish();
|
||||
return bout.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,20 +27,20 @@ public final class ThreadUtil {
|
||||
private static final Logger LOGGER = Logger.getLogger(ThreadUtil.class.getSimpleName());
|
||||
|
||||
/**
|
||||
* The default {@link UncaughtExceptionHandler} which raises an error from the logger with the exception and name of
|
||||
* The default {@link UncaughtExceptionHandler} which raises an error from the logger with the exception and name
|
||||
* of
|
||||
* the specified thread the exception occurred in.
|
||||
*/
|
||||
private static final UncaughtExceptionHandler DEFAULT_EXCEPTION_HANDLER = (thread, exception) -> LOGGER.log(Level.SEVERE,
|
||||
"Exception occured in thread " + thread.getName(), exception);
|
||||
private static final UncaughtExceptionHandler DEFAULT_EXCEPTION_HANDLER =
|
||||
(thread, exception) -> LOGGER.log(Level.SEVERE, "Exception in thread " + thread.getName(), exception);
|
||||
|
||||
/**
|
||||
* Builds a {@link ThreadFactory} using the specified {@code String} name-format, normal thread priority and the
|
||||
* default {@link UncaughtExceptionHandler}.
|
||||
*
|
||||
* @see #DEFAULT_EXCEPTION_HANDLER
|
||||
*
|
||||
* @param name The name-format used when creating threads, may not be {@code null}.
|
||||
* @return A new {@link ThreadFactory} from the specified parameters, never {@code null}.
|
||||
* @param name The name-format used when creating threads. Must not be {@code null}.
|
||||
* @return The {@link ThreadFactory}. Will never be {@code null}.
|
||||
* @see #DEFAULT_EXCEPTION_HANDLER
|
||||
*/
|
||||
public static ThreadFactory create(String name) {
|
||||
return create(name, Thread.NORM_PRIORITY, DEFAULT_EXCEPTION_HANDLER);
|
||||
@@ -50,9 +50,9 @@ public final class ThreadUtil {
|
||||
* Builds a {@link ThreadFactory} using the specified {@code String} name-format, priority and the
|
||||
* {@link #DEFAULT_EXCEPTION_HANDLER}.
|
||||
*
|
||||
* @param name The name-format used when creating threads, may not be {@code null}.
|
||||
* @param name The name-format used when creating threads. Must not be {@code null}.
|
||||
* @param priority The priority used when creating threads.
|
||||
* @return A new {@link ThreadFactory} from the specified parameters, never {@code null}.
|
||||
* @return The {@link ThreadFactory}. Will never be {@code null}.
|
||||
*/
|
||||
public static ThreadFactory create(String name, int priority) {
|
||||
return create(name, priority, DEFAULT_EXCEPTION_HANDLER);
|
||||
@@ -65,10 +65,11 @@ public final class ThreadUtil {
|
||||
* @param name The name-format used when creating threads. Must not be {@code null}.
|
||||
* @param priority The priority used when creating threads.
|
||||
* @param handler The {@link UncaughtExceptionHandler} used when creating threads. Must not be {@code null}.
|
||||
* @return A new {@link ThreadFactory} using the specified parameters.
|
||||
* @return The {@link ThreadFactory}. Will never be {@code null}.
|
||||
*/
|
||||
public static ThreadFactory create(String name, int priority, UncaughtExceptionHandler handler) {
|
||||
Objects.requireNonNull(priority);
|
||||
Objects.requireNonNull(name, "ThreadFactory name must not be null.");
|
||||
Objects.requireNonNull(handler, "UncaughtExceptionHandler must not be null.");
|
||||
|
||||
ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
|
||||
builder.setNameFormat(name);
|
||||
|
||||
Reference in New Issue
Block a user