Cache server CRCs

This commit is contained in:
atomicint
2015-04-13 16:11:34 -04:00
parent 802d41306a
commit ad6f377ff0
2 changed files with 24 additions and 6 deletions
+23
View File
@@ -7,6 +7,7 @@ import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.zip.CRC32;
import com.google.common.base.Preconditions;
@@ -24,6 +25,11 @@ public final class IndexedFileSystem implements Closeable {
*/
private ByteBuffer crcTable;
/**
* The {@link #crcTable} represented as an {@code int} array.
*/
private int[] crcs;
/**
* The data file.
*/
@@ -143,6 +149,23 @@ public final class IndexedFileSystem implements Closeable {
throw new IllegalStateException("Cannot get CRC table from a writable file system.");
}
/**
* Gets the CRC table as an {@code int} array.
*
* @return The CRC table as an {@code int} array.
* @throws IOException If there is an error accessing files to create the table.
*/
public int[] getCrcs() throws IOException {
if (crcs != null) {
return crcs;
}
ByteBuffer buffer = getCrcTable();
crcs = new int[(buffer.remaining() / Integer.BYTES) - 1];
Arrays.setAll(crcs, crc -> buffer.getInt());
return crcs;
}
/**
* Gets a file.
*
+1 -6
View File
@@ -3,7 +3,6 @@ package org.apollo.login;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -120,12 +119,8 @@ public final class LoginService extends Service {
return true;
}
ByteBuffer buffer = getContext().getFileSystem().getCrcTable();
int[] clientCrcs = request.getArchiveCrcs();
int[] serverCrcs = new int[clientCrcs.length];
Arrays.setAll(serverCrcs, crc -> buffer.getInt());
int[] serverCrcs = getContext().getFileSystem().getCrcs();
if (Arrays.equals(clientCrcs, serverCrcs)) {
return false;