From eac525f3b682b357b031f8f25feeca76f1ce7f13 Mon Sep 17 00:00:00 2001 From: Maffia Date: Mon, 20 Aug 2018 22:45:58 +0200 Subject: [PATCH] Addded a few I/O methods. --- src/org/displee/CacheLibrary.java | 33 +++++++---------------- src/org/displee/cache/index/Index.java | 6 ++--- src/org/displee/io/Stream.java | 18 +++++++++++++ src/org/displee/io/impl/InputStream.java | 21 ++++++++++++++- src/org/displee/io/impl/OutputStream.java | 16 +++++++++++ 5 files changed, 66 insertions(+), 28 deletions(-) diff --git a/src/org/displee/CacheLibrary.java b/src/org/displee/CacheLibrary.java index ca935ed..bdf0260 100644 --- a/src/org/displee/CacheLibrary.java +++ b/src/org/displee/CacheLibrary.java @@ -2,6 +2,7 @@ import java.io.File; import java.io.FileNotFoundException; +import java.io.IOException; import java.io.RandomAccessFile; import java.util.Arrays; @@ -19,11 +20,6 @@ */ public class CacheLibrary { - /** - * The revision of this cache library - */ - private final int revision; - /** * An array of indices of this cache. */ @@ -58,26 +54,23 @@ public class CacheLibrary { * Initialize this cache library. * @param path The path to the cache files. * @param mode The cache library mode. - * @param revision The revision of the cache files. - * @throws Exception + * @throws IOException If it failed to read the cache files. */ - public CacheLibrary(String path, CacheLibraryMode mode, int revision) throws Exception { - this(path, mode, revision, null); + public CacheLibrary(String path, CacheLibraryMode mode) throws IOException { + this(path, mode, null); } /** * Initialize this cache library. * @param path The path to the cache files. * @param mode The cache library mode. - * @param revision The revision of the cache files. * @param listener The progress listener. - * @throws Exception + * @throws IOException If it failed to read the cache files. */ - public CacheLibrary(String path, CacheLibraryMode mode, int revision, ProgressListener listener) throws Exception { + public CacheLibrary(String path, CacheLibraryMode mode, ProgressListener listener) throws IOException { if (path == null) { throw new FileNotFoundException("The path to the cache is incorrect."); } - this.revision = revision; this.path = path; this.mode = mode; final File main = new File(path + "main_file_cache.dat2"); @@ -162,9 +155,9 @@ public void removeLastIndex() { checksumTable.getRandomAccessFile().setLength(id * Constants.INDEX_SIZE); int offset = 0; final Index[] newIndices = new Index[id]; - for(int i = 0; i < indices.length; i++) { - if (indices[i].getId() != id) { - newIndices[offset++] = indices[i]; + for (Index index : indices) { + if (index.getId() != id) { + newIndices[offset++] = index; } } indices = newIndices; @@ -247,14 +240,6 @@ public ChecksumTable getChecksumTable() { return checksumTable; } - /** - * Get the revision of this cache library. - * @return {@code revision} - */ - public int getRevision() { - return revision; - } - /** * Get the path. * @return {@code path} diff --git a/src/org/displee/cache/index/Index.java b/src/org/displee/cache/index/Index.java index 1c7f89e..d00fc21 100644 --- a/src/org/displee/cache/index/Index.java +++ b/src/org/displee/cache/index/Index.java @@ -163,7 +163,7 @@ public ArchiveInformation getArchiveInformation(int id) { synchronized (super.origin.getMainFile()) { try { int type = 0; - if (origin.getRevision() >= 670 && id > 65535) { + if (id > 65535) { type = 1; } if (super.origin.getMainFile().length() < (Constants.INDEX_SIZE * id + Constants.INDEX_SIZE)) { @@ -264,7 +264,7 @@ public boolean writeArchiveInformation(int id, byte[] data) { int chunk = 0; int archiveDataSize = Constants.ARCHIVE_DATA_SIZE; int archiveHeaderSize = Constants.ARCHIVE_HEADER_SIZE; - if (super.origin.getRevision() >= 670 && id > 65535) { + if (id > 65535) { archiveDataSize -= 2; archiveHeaderSize += 2; } @@ -337,7 +337,7 @@ public boolean cache(int[][] xteas) { if (!cached) { for (final int archive : super.archiveIds) { try { - super.getArchive(archive, xteas == null ? null : xteas[archive] == null ? null : xteas[archive]); + super.getArchive(archive, xteas == null ? null : xteas[archive]); } catch(Throwable t) { t.printStackTrace(); } diff --git a/src/org/displee/io/Stream.java b/src/org/displee/io/Stream.java index b36a46a..38719e4 100644 --- a/src/org/displee/io/Stream.java +++ b/src/org/displee/io/Stream.java @@ -1,5 +1,7 @@ package org.displee.io; +import java.util.Arrays; + /** * An extendible class I/O streaming serving as a base for both I/O stream. * @author Apache Ah64 @@ -83,6 +85,14 @@ public int readByte() { return getRemaining() > 0 ? buffer[offset++] : 0; } + /** + * Read an unsigned byte. + * @return The unsigned byte. + */ + public int readUnsignedByte() { + return readByte() & 0xFF; + } + public int smf_gvlength() { int i_20_ = readByte(); int i_21_ = 0; @@ -118,6 +128,14 @@ public void writeByte(byte b, int index) { buffer[index] = b; } + /** + * Write a boolean. + * @param bool The condition. + */ + public void writeBoolean(boolean bool) { + writeByte(bool ? 1 : 0); + } + /** * Expend the capacity. */ diff --git a/src/org/displee/io/impl/InputStream.java b/src/org/displee/io/impl/InputStream.java index 24eed6c..b2b07af 100644 --- a/src/org/displee/io/impl/InputStream.java +++ b/src/org/displee/io/impl/InputStream.java @@ -109,6 +109,15 @@ public int readUnsignedSmart() { return (readShort() & 0xFFFF) - 49152; } + /** + * Reads a smart value from the buffer (supports -1). + * + * @return the read smart value. + */ + public int readSmartNS() { + return readSmart() - 1; + } + /** * Read a signed smart. * @return The smart value. @@ -123,7 +132,7 @@ public int readSmart() { public int readBigSmart(boolean old) { if (old) { - return readShort() & 0xFFFF; + return readUnsignedShort(); } else { return readBigSmart(); } @@ -156,6 +165,16 @@ public int readShort() { return s; } + /** + * Read a short unsigned. + * @return The unsigned short. + */ + public int readUnsignedShort() { + /*int i = readUnsignedByte(); + return readUnsignedByte() + (i << 8);*/ + return readShort() & 0xFFFF; + } + /** * Read a short LE. * @return The short. diff --git a/src/org/displee/io/impl/OutputStream.java b/src/org/displee/io/impl/OutputStream.java index 2fa647c..7f737ca 100644 --- a/src/org/displee/io/impl/OutputStream.java +++ b/src/org/displee/io/impl/OutputStream.java @@ -110,6 +110,22 @@ public void writeSmart2(int i) { writeSmart(i); } + /** + * Writes an unsigned smart value to the buffer. + * @param value The value to write. + */ + public void writeUnsignedSmart(int value) { + if (value < 64 && value >= -64) { + writeByte(value + 64); + return; + } + if (value < 16384 && value >= -16384) { + writeShort(value + 49152); + } else { + System.err.println("Error psmart out of range: " + value); + } + } + /** * Write a big smart. * @param i