Change some incorrect occurrences of 'uncompressed' to 'decompressed' in CompressionUtil.

This commit is contained in:
Major-
2014-09-12 14:42:29 +01:00
parent 3ce2d2386a
commit f27aa410c7
3 changed files with 57 additions and 52 deletions
+31 -26
View File
@@ -19,7 +19,7 @@ public final class Archive {
* *
* @param buffer The buffer. * @param buffer The buffer.
* @return The archive. * @return The archive.
* @throws IOException If an I/O error occurs. * @throws IOException If there is an error decompressing the archive.
*/ */
public static Archive decode(ByteBuffer buffer) throws IOException { public static Archive decode(ByteBuffer buffer) throws IOException {
int extractedSize = BufferUtil.readUnsignedTriByte(buffer); int extractedSize = BufferUtil.readUnsignedTriByte(buffer);
@@ -28,41 +28,41 @@ public final class Archive {
if (size != extractedSize) { if (size != extractedSize) {
byte[] compressed = new byte[size]; byte[] compressed = new byte[size];
byte[] uncompressed = new byte[extractedSize]; byte[] decompressed = new byte[extractedSize];
buffer.get(compressed); buffer.get(compressed);
CompressionUtil.unbzip2(compressed, uncompressed); CompressionUtil.debzip2(compressed, decompressed);
buffer = ByteBuffer.wrap(uncompressed); buffer = ByteBuffer.wrap(decompressed);
extracted = true; extracted = true;
} }
int entries = buffer.getShort() & 0xFFFF; int entryCount = buffer.getShort() & 0xFFFF;
int[] identifiers = new int[entries]; int[] identifiers = new int[entryCount];
int[] extractedSizes = new int[entries]; int[] extractedSizes = new int[entryCount];
int[] sizes = new int[entries]; int[] sizes = new int[entryCount];
for (int i = 0; i < entries; i++) { for (int i = 0; i < entryCount; i++) {
identifiers[i] = buffer.getInt(); identifiers[i] = buffer.getInt();
extractedSizes[i] = BufferUtil.readUnsignedTriByte(buffer); extractedSizes[i] = BufferUtil.readUnsignedTriByte(buffer);
sizes[i] = BufferUtil.readUnsignedTriByte(buffer); sizes[i] = BufferUtil.readUnsignedTriByte(buffer);
} }
ArchiveEntry[] entry = new ArchiveEntry[entries]; ArchiveEntry[] entries = new ArchiveEntry[entryCount];
for (int i = 0; i < entries; i++) { for (int entry = 0; entry < entryCount; entry++) {
ByteBuffer entryBuffer; ByteBuffer entryBuffer;
if (!extracted) { if (!extracted) {
byte[] compressed = new byte[sizes[i]]; byte[] compressed = new byte[sizes[entry]];
byte[] uncompressed = new byte[extractedSizes[i]]; byte[] decompressed = new byte[extractedSizes[entry]];
buffer.get(compressed); buffer.get(compressed);
CompressionUtil.unbzip2(compressed, uncompressed); CompressionUtil.debzip2(compressed, decompressed);
entryBuffer = ByteBuffer.wrap(uncompressed); entryBuffer = ByteBuffer.wrap(decompressed);
} else { } else {
byte[] buf = new byte[extractedSizes[i]]; byte[] buf = new byte[extractedSizes[entry]];
buffer.get(buf); buffer.get(buf);
entryBuffer = ByteBuffer.wrap(buf); entryBuffer = ByteBuffer.wrap(buf);
} }
entry[i] = new ArchiveEntry(identifiers[i], entryBuffer); entries[entry] = new ArchiveEntry(identifiers[entry], entryBuffer);
} }
return new Archive(entry); return new Archive(entries);
} }
/** /**
@@ -80,19 +80,14 @@ public final class Archive {
} }
/** /**
* Gets an entry by its name. * Gets an {@link ArchiveEntry} by its name.
* *
* @param name The name. * @param name The name.
* @return The entry. * @return The entry.
* @throws FileNotFoundException If the file could not be found. * @throws FileNotFoundException If the entry could not be found.
*/ */
public ArchiveEntry getEntry(String name) throws FileNotFoundException { public ArchiveEntry getEntry(String name) throws FileNotFoundException {
int hash = 0; int hash = hash(name);
name = name.toUpperCase();
for (int i = 0; i < name.length(); i++) {
hash = hash * 61 + name.charAt(i) - 32;
}
for (ArchiveEntry entry : entries) { for (ArchiveEntry entry : entries) {
if (entry.getIdentifier() == hash) { if (entry.getIdentifier() == hash) {
@@ -102,4 +97,14 @@ public final class Archive {
throw new FileNotFoundException("Could not find entry: " + name + "."); throw new FileNotFoundException("Could not find entry: " + name + ".");
} }
/**
* Hashes the specified string into an integer used to identify an {@link ArchiveEntry}.
*
* @param name The name of the entry.
* @return The hash.
*/
public static int hash(String name) {
return name.toUpperCase().chars().reduce(0, (hash, character) -> hash * 61 + character - 32);
}
} }
@@ -64,7 +64,7 @@ public final class GameObjectDecoder {
for (int i = 0; i < indices; i++) { for (int i = 0; i < indices; i++) {
ByteBuffer compressed = fs.getFile(4, landscapes[i]); ByteBuffer compressed = fs.getFile(4, landscapes[i]);
ByteBuffer uncompressed = ByteBuffer.wrap(CompressionUtil.ungzip(compressed)); ByteBuffer uncompressed = ByteBuffer.wrap(CompressionUtil.degzip(compressed));
Collection<GameObject> areaObjects = parseArea(areas[i], uncompressed); Collection<GameObject> areaObjects = parseArea(areas[i], uncompressed);
objects.addAll(areaObjects); objects.addAll(areaObjects);
+25 -25
View File
@@ -21,19 +21,20 @@ import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream
public final class CompressionUtil { public final class CompressionUtil {
/** /**
* Bzip2s the specified array. * Bzip2s the specified array, removing the header.
* *
* @param bytes The uncompressed array. * @param uncompressed The uncompressed array.
* @return The compressed array. * @return The compressed array.
* @throws IOException If an I/O error occurs. * @throws IOException If there is an error compressing the array.
*/ */
public static byte[] bzip2(byte[] bytes) throws IOException { public static byte[] bzip2(byte[] uncompressed) throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream(); ByteArrayOutputStream bout = new ByteArrayOutputStream();
try (BZip2CompressorOutputStream os = new BZip2CompressorOutputStream(bout, 1)) { try (BZip2CompressorOutputStream os = new BZip2CompressorOutputStream(bout, 1)) {
os.write(bytes); os.write(uncompressed);
os.finish(); os.finish();
byte[] compressed = bout.toByteArray(); byte[] compressed = bout.toByteArray();
byte[] newCompressed = new byte[compressed.length - 4]; byte[] newCompressed = new byte[compressed.length - 4]; // Strip the header
System.arraycopy(compressed, 4, newCompressed, 0, newCompressed.length); System.arraycopy(compressed, 4, newCompressed, 0, newCompressed.length);
return newCompressed; return newCompressed;
} }
@@ -42,27 +43,27 @@ public final class CompressionUtil {
/** /**
* Gzips the specified array. * Gzips the specified array.
* *
* @param bytes The uncompressed array. * @param uncompressed The uncompressed array.
* @return The compressed array. * @return The compressed array.
* @throws IOException If an I/O error occurs. * @throws IOException If there is an error compressing the array.
*/ */
public static byte[] gzip(byte[] bytes) throws IOException { public static byte[] gzip(byte[] uncompressed) throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream(); ByteArrayOutputStream bout = new ByteArrayOutputStream();
try (DeflaterOutputStream os = new GZIPOutputStream(bout)) { try (DeflaterOutputStream os = new GZIPOutputStream(bout)) {
os.write(bytes); os.write(uncompressed);
os.finish(); os.finish();
return bout.toByteArray(); return bout.toByteArray();
} }
} }
/** /**
* Unbzip2s the compressed array and places the result into the decompressed array. * Debzip2s the compressed array and places the result into the decompressed array.
* *
* @param compressed The compressed array. * @param compressed The compressed array, <strong>without</strong> the header.
* @param uncompressed The decompressed array. * @param decompressed The decompressed array.
* @throws IOException If an I/O error occurs. * @throws IOException If there is an error decompressing the array.
*/ */
public static void unbzip2(byte[] compressed, byte[] uncompressed) throws IOException { public static void debzip2(byte[] compressed, byte[] decompressed) throws IOException {
byte[] newCompressed = new byte[compressed.length + 4]; byte[] newCompressed = new byte[compressed.length + 4];
newCompressed[0] = 'B'; newCompressed[0] = 'B';
newCompressed[1] = 'Z'; newCompressed[1] = 'Z';
@@ -70,33 +71,32 @@ public final class CompressionUtil {
newCompressed[3] = '1'; newCompressed[3] = '1';
System.arraycopy(compressed, 0, newCompressed, 4, compressed.length); System.arraycopy(compressed, 0, newCompressed, 4, compressed.length);
try (DataInputStream is = new DataInputStream(new BZip2CompressorInputStream(new ByteArrayInputStream( try (DataInputStream is = new DataInputStream(new BZip2CompressorInputStream(new ByteArrayInputStream(newCompressed)))) {
newCompressed)))) { is.readFully(decompressed);
is.readFully(uncompressed);
} }
} }
/** /**
* Ungzips the compressed array and places the results into the decompressed array. * Degzips the compressed array and places the results into the decompressed array.
* *
* @param compressed The compressed array. * @param compressed The compressed array.
* @param uncompressed The decompressed array. * @param decompressed The decompressed array.
* @throws IOException If an I/O error occurs. * @throws IOException If an I/O error occurs.
*/ */
public static void ungzip(byte[] compressed, byte[] uncompressed) throws IOException { public static void degzip(byte[] compressed, byte[] decompressed) throws IOException {
try (DataInputStream is = new DataInputStream(new GZIPInputStream(new ByteArrayInputStream(compressed)))) { try (DataInputStream is = new DataInputStream(new GZIPInputStream(new ByteArrayInputStream(compressed)))) {
is.readFully(uncompressed); is.readFully(decompressed);
} }
} }
/** /**
* Ungzips the compressed buffer and places the results into the returning array. * Degzips the compressed {@link ByteBuffer} and returns the result as a byte array.
* *
* @param compressed The compressed buffer. * @param compressed The compressed buffer.
* @return The decompressed array. * @return The decompressed array.
* @throws IOException If an I/O error occurs. * @throws IOException If there is an error decompressing the buffer.
*/ */
public static byte[] ungzip(ByteBuffer compressed) throws IOException { public static byte[] degzip(ByteBuffer compressed) throws IOException {
byte[] data = new byte[compressed.remaining()]; byte[] data = new byte[compressed.remaining()];
compressed.get(data); compressed.get(data);