From f27aa410c74487ca644b3e671edc70fbc7756b32 Mon Sep 17 00:00:00 2001 From: Major- Date: Fri, 12 Sep 2014 14:42:29 +0100 Subject: [PATCH] Change some incorrect occurrences of 'uncompressed' to 'decompressed' in CompressionUtil. --- src/org/apollo/fs/archive/Archive.java | 57 ++++++++++--------- .../apollo/fs/decoder/GameObjectDecoder.java | 2 +- src/org/apollo/util/CompressionUtil.java | 50 ++++++++-------- 3 files changed, 57 insertions(+), 52 deletions(-) diff --git a/src/org/apollo/fs/archive/Archive.java b/src/org/apollo/fs/archive/Archive.java index 5ed7ede8..c43f391f 100644 --- a/src/org/apollo/fs/archive/Archive.java +++ b/src/org/apollo/fs/archive/Archive.java @@ -19,7 +19,7 @@ public final class Archive { * * @param buffer The buffer. * @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 { int extractedSize = BufferUtil.readUnsignedTriByte(buffer); @@ -28,41 +28,41 @@ public final class Archive { if (size != extractedSize) { byte[] compressed = new byte[size]; - byte[] uncompressed = new byte[extractedSize]; + byte[] decompressed = new byte[extractedSize]; buffer.get(compressed); - CompressionUtil.unbzip2(compressed, uncompressed); - buffer = ByteBuffer.wrap(uncompressed); + CompressionUtil.debzip2(compressed, decompressed); + buffer = ByteBuffer.wrap(decompressed); extracted = true; } - int entries = buffer.getShort() & 0xFFFF; - int[] identifiers = new int[entries]; - int[] extractedSizes = new int[entries]; - int[] sizes = new int[entries]; + int entryCount = buffer.getShort() & 0xFFFF; + int[] identifiers = new int[entryCount]; + int[] extractedSizes = new int[entryCount]; + int[] sizes = new int[entryCount]; - for (int i = 0; i < entries; i++) { + for (int i = 0; i < entryCount; i++) { identifiers[i] = buffer.getInt(); extractedSizes[i] = BufferUtil.readUnsignedTriByte(buffer); sizes[i] = BufferUtil.readUnsignedTriByte(buffer); } - ArchiveEntry[] entry = new ArchiveEntry[entries]; - for (int i = 0; i < entries; i++) { + ArchiveEntry[] entries = new ArchiveEntry[entryCount]; + for (int entry = 0; entry < entryCount; entry++) { ByteBuffer entryBuffer; if (!extracted) { - byte[] compressed = new byte[sizes[i]]; - byte[] uncompressed = new byte[extractedSizes[i]]; + byte[] compressed = new byte[sizes[entry]]; + byte[] decompressed = new byte[extractedSizes[entry]]; buffer.get(compressed); - CompressionUtil.unbzip2(compressed, uncompressed); - entryBuffer = ByteBuffer.wrap(uncompressed); + CompressionUtil.debzip2(compressed, decompressed); + entryBuffer = ByteBuffer.wrap(decompressed); } else { - byte[] buf = new byte[extractedSizes[i]]; + byte[] buf = new byte[extractedSizes[entry]]; buffer.get(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. * @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 { - int hash = 0; - name = name.toUpperCase(); - - for (int i = 0; i < name.length(); i++) { - hash = hash * 61 + name.charAt(i) - 32; - } + int hash = hash(name); for (ArchiveEntry entry : entries) { if (entry.getIdentifier() == hash) { @@ -102,4 +97,14 @@ public final class Archive { 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); + } + } \ No newline at end of file diff --git a/src/org/apollo/fs/decoder/GameObjectDecoder.java b/src/org/apollo/fs/decoder/GameObjectDecoder.java index 9b2f05af..e1f55dcd 100644 --- a/src/org/apollo/fs/decoder/GameObjectDecoder.java +++ b/src/org/apollo/fs/decoder/GameObjectDecoder.java @@ -64,7 +64,7 @@ public final class GameObjectDecoder { for (int i = 0; i < indices; i++) { ByteBuffer compressed = fs.getFile(4, landscapes[i]); - ByteBuffer uncompressed = ByteBuffer.wrap(CompressionUtil.ungzip(compressed)); + ByteBuffer uncompressed = ByteBuffer.wrap(CompressionUtil.degzip(compressed)); Collection areaObjects = parseArea(areas[i], uncompressed); objects.addAll(areaObjects); diff --git a/src/org/apollo/util/CompressionUtil.java b/src/org/apollo/util/CompressionUtil.java index 16029374..4036fefc 100644 --- a/src/org/apollo/util/CompressionUtil.java +++ b/src/org/apollo/util/CompressionUtil.java @@ -21,19 +21,20 @@ import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream 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. - * @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(); try (BZip2CompressorOutputStream os = new BZip2CompressorOutputStream(bout, 1)) { - os.write(bytes); + os.write(uncompressed); os.finish(); + 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); return newCompressed; } @@ -42,27 +43,27 @@ public final class CompressionUtil { /** * Gzips the specified array. * - * @param bytes The uncompressed array. + * @param uncompressed The uncompressed 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(); try (DeflaterOutputStream os = new GZIPOutputStream(bout)) { - os.write(bytes); + os.write(uncompressed); os.finish(); 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 uncompressed The decompressed array. - * @throws IOException If an I/O error occurs. + * @param compressed The compressed array, without the header. + * @param decompressed The decompressed array. + * @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]; newCompressed[0] = 'B'; newCompressed[1] = 'Z'; @@ -70,33 +71,32 @@ public final class CompressionUtil { newCompressed[3] = '1'; System.arraycopy(compressed, 0, newCompressed, 4, compressed.length); - try (DataInputStream is = new DataInputStream(new BZip2CompressorInputStream(new ByteArrayInputStream( - newCompressed)))) { - is.readFully(uncompressed); + try (DataInputStream is = new DataInputStream(new BZip2CompressorInputStream(new ByteArrayInputStream(newCompressed)))) { + is.readFully(decompressed); } } /** - * 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 uncompressed The decompressed array. + * @param decompressed The decompressed array. * @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)))) { - 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. * @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()]; compressed.get(data);