From ba7530616904f15ca4d8c7b29af6e4b51f746843 Mon Sep 17 00:00:00 2001 From: Graham Date: Fri, 1 Apr 2022 20:38:56 +0100 Subject: [PATCH] Annotate cache methods with default parameters with @JvmOverloads This allows them to be used from Java. Signed-off-by: Graham --- cache/src/main/kotlin/org/openrs2/cache/Archive.kt | 9 +++++++++ cache/src/main/kotlin/org/openrs2/cache/Cache.kt | 11 +++++++++++ cache/src/main/kotlin/org/openrs2/cache/DiskStore.kt | 1 + .../main/kotlin/org/openrs2/cache/FlatFileStore.kt | 2 ++ cache/src/main/kotlin/org/openrs2/cache/JagArchive.kt | 2 ++ .../main/kotlin/org/openrs2/cache/Js5Compression.kt | 2 ++ .../main/kotlin/org/openrs2/cache/Js5MasterIndex.kt | 2 ++ cache/src/main/kotlin/org/openrs2/cache/Store.kt | 1 + 8 files changed, 30 insertions(+) diff --git a/cache/src/main/kotlin/org/openrs2/cache/Archive.kt b/cache/src/main/kotlin/org/openrs2/cache/Archive.kt index cd3db9bc..329701cb 100644 --- a/cache/src/main/kotlin/org/openrs2/cache/Archive.kt +++ b/cache/src/main/kotlin/org/openrs2/cache/Archive.kt @@ -181,6 +181,7 @@ public abstract class Archive internal constructor( return listNamed(group.krHashCode()) } + @JvmOverloads public fun read(group: Int, file: Int, key: XteaKey = XteaKey.ZERO): ByteBuf { require(group >= 0 && file >= 0) @@ -189,16 +190,19 @@ public abstract class Archive internal constructor( return unpacked.read(file) } + @JvmOverloads public fun readNamed(groupNameHash: Int, fileNameHash: Int, key: XteaKey = XteaKey.ZERO): ByteBuf { val entry = index.getNamed(groupNameHash) ?: throw FileNotFoundException() val unpacked = getUnpacked(entry, key) return unpacked.readNamed(fileNameHash) } + @JvmOverloads public fun read(group: String, file: String, key: XteaKey = XteaKey.ZERO): ByteBuf { return readNamed(group.krHashCode(), file.krHashCode(), key) } + @JvmOverloads public fun write(group: Int, file: Int, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) { require(group >= 0 && file >= 0) @@ -209,6 +213,7 @@ public abstract class Archive internal constructor( dirty = true } + @JvmOverloads public fun writeNamed(groupNameHash: Int, fileNameHash: Int, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) { val entry = index.createOrGetNamed(groupNameHash) val unpacked = createOrGetUnpacked(entry, key, isOverwritingNamed(entry, fileNameHash)) @@ -218,6 +223,7 @@ public abstract class Archive internal constructor( index.hasNames = true } + @JvmOverloads public fun write(group: String, file: String, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) { return writeNamed(group.krHashCode(), file.krHashCode(), buf, key) } @@ -244,6 +250,7 @@ public abstract class Archive internal constructor( return removeNamed(group.krHashCode()) } + @JvmOverloads public fun remove(group: Int, file: Int, key: XteaKey = XteaKey.ZERO) { require(group >= 0 && file >= 0) @@ -260,6 +267,7 @@ public abstract class Archive internal constructor( dirty = true } + @JvmOverloads public fun removeNamed(groupNameHash: Int, fileNameHash: Int, key: XteaKey = XteaKey.ZERO) { val entry = index.getNamed(groupNameHash) ?: return @@ -274,6 +282,7 @@ public abstract class Archive internal constructor( dirty = true } + @JvmOverloads public fun remove(group: String, file: String, key: XteaKey = XteaKey.ZERO) { return removeNamed(group.krHashCode(), file.krHashCode(), key) } diff --git a/cache/src/main/kotlin/org/openrs2/cache/Cache.kt b/cache/src/main/kotlin/org/openrs2/cache/Cache.kt index c2d29db4..26303a88 100644 --- a/cache/src/main/kotlin/org/openrs2/cache/Cache.kt +++ b/cache/src/main/kotlin/org/openrs2/cache/Cache.kt @@ -132,25 +132,30 @@ public class Cache private constructor( return listNamed(archive, group.krHashCode()) } + @JvmOverloads public fun read(archive: Int, group: Int, file: Int, key: XteaKey = XteaKey.ZERO): ByteBuf { checkArchive(archive) return archives[archive]?.read(group, file, key) ?: throw FileNotFoundException() } + @JvmOverloads public fun readNamed(archive: Int, groupNameHash: Int, fileNameHash: Int, key: XteaKey = XteaKey.ZERO): ByteBuf { checkArchive(archive) return archives[archive]?.readNamed(groupNameHash, fileNameHash, key) ?: throw FileNotFoundException() } + @JvmOverloads public fun read(archive: Int, group: String, file: String, key: XteaKey = XteaKey.ZERO): ByteBuf { return readNamed(archive, group.krHashCode(), file.krHashCode(), key) } + @JvmOverloads public fun write(archive: Int, group: Int, file: Int, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) { checkArchive(archive) createOrGetArchive(archive).write(group, file, buf, key) } + @JvmOverloads public fun writeNamed( archive: Int, groupNameHash: Int, @@ -162,6 +167,7 @@ public class Cache private constructor( createOrGetArchive(archive).writeNamed(groupNameHash, fileNameHash, buf, key) } + @JvmOverloads public fun write(archive: Int, group: String, file: String, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) { writeNamed(archive, group.krHashCode(), file.krHashCode(), buf, key) } @@ -195,16 +201,19 @@ public class Cache private constructor( return removeNamed(archive, group.krHashCode()) } + @JvmOverloads public fun remove(archive: Int, group: Int, file: Int, key: XteaKey = XteaKey.ZERO) { checkArchive(archive) archives[archive]?.remove(group, file, key) } + @JvmOverloads public fun removeNamed(archive: Int, groupNameHash: Int, fileNameHash: Int, key: XteaKey = XteaKey.ZERO) { checkArchive(archive) archives[archive]?.removeNamed(groupNameHash, fileNameHash, key) } + @JvmOverloads public fun remove(archive: Int, group: String, file: String, key: XteaKey = XteaKey.ZERO) { return removeNamed(archive, group.krHashCode(), file.krHashCode(), key) } @@ -240,6 +249,7 @@ public class Cache private constructor( public companion object { public const val MAX_ARCHIVE: Int = 254 + @JvmOverloads public fun open( root: Path, alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT, @@ -248,6 +258,7 @@ public class Cache private constructor( return open(Store.open(root, alloc), alloc, unpackedCacheSize) } + @JvmOverloads public fun open( store: Store, alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT, diff --git a/cache/src/main/kotlin/org/openrs2/cache/DiskStore.kt b/cache/src/main/kotlin/org/openrs2/cache/DiskStore.kt index 55d2f157..6e044cdc 100644 --- a/cache/src/main/kotlin/org/openrs2/cache/DiskStore.kt +++ b/cache/src/main/kotlin/org/openrs2/cache/DiskStore.kt @@ -500,6 +500,7 @@ public class DiskStore private constructor( return root.resolve("main_file_cache.idx$archive") } + @JvmOverloads public fun open(root: Path, alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT): Store { val js5DataPath = dataPath(root) val legacyDataPath = legacyDataPath(root) diff --git a/cache/src/main/kotlin/org/openrs2/cache/FlatFileStore.kt b/cache/src/main/kotlin/org/openrs2/cache/FlatFileStore.kt index fd184b46..d2cd76cd 100644 --- a/cache/src/main/kotlin/org/openrs2/cache/FlatFileStore.kt +++ b/cache/src/main/kotlin/org/openrs2/cache/FlatFileStore.kt @@ -128,6 +128,7 @@ public class FlatFileStore private constructor( private val GROUP_NAME = Regex("[0-9]+[.]dat") private const val GROUP_EXTENSION = ".dat" + @JvmOverloads public fun open(root: Path, alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT): Store { if (!Files.isDirectory(root)) { throw FileNotFoundException() @@ -136,6 +137,7 @@ public class FlatFileStore private constructor( return FlatFileStore(root, alloc) } + @JvmOverloads public fun create(root: Path, alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT): Store { Files.createDirectories(root) return FlatFileStore(root, alloc) diff --git a/cache/src/main/kotlin/org/openrs2/cache/JagArchive.kt b/cache/src/main/kotlin/org/openrs2/cache/JagArchive.kt index c8c85005..636849a8 100644 --- a/cache/src/main/kotlin/org/openrs2/cache/JagArchive.kt +++ b/cache/src/main/kotlin/org/openrs2/cache/JagArchive.kt @@ -129,6 +129,7 @@ public class JagArchive : Closeable { * @param alloc the allocator. * @return the compressed archive. */ + @JvmOverloads public fun pack(compressedArchive: Boolean, alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT): ByteBuf { alloc.buffer().use { output -> alloc.buffer().use { uncompressedArchiveBuf -> @@ -189,6 +190,7 @@ public class JagArchive : Closeable { * @param alloc the allocator. * @return the compressed archive. */ + @JvmOverloads public fun packBest(alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT): ByteBuf { pack(true, alloc).use { compressedArchive -> pack(false, alloc).use { compressedEntries -> diff --git a/cache/src/main/kotlin/org/openrs2/cache/Js5Compression.kt b/cache/src/main/kotlin/org/openrs2/cache/Js5Compression.kt index cbf4705a..cd8d6656 100644 --- a/cache/src/main/kotlin/org/openrs2/cache/Js5Compression.kt +++ b/cache/src/main/kotlin/org/openrs2/cache/Js5Compression.kt @@ -18,6 +18,7 @@ public object Js5Compression { private const val LZMA_PB_MAX = 4 private const val LZMA_PRESET_DICT_SIZE_MAX = 1 shl 26 + @JvmOverloads public fun compress(input: ByteBuf, type: Js5CompressionType, key: XteaKey = XteaKey.ZERO): ByteBuf { input.alloc().buffer().use { output -> output.writeByte(type.ordinal) @@ -107,6 +108,7 @@ public object Js5Compression { } } + @JvmOverloads public fun uncompress(input: ByteBuf, key: XteaKey = XteaKey.ZERO): ByteBuf { if (input.readableBytes() < 5) { throw IOException("Missing header") diff --git a/cache/src/main/kotlin/org/openrs2/cache/Js5MasterIndex.kt b/cache/src/main/kotlin/org/openrs2/cache/Js5MasterIndex.kt index d881ff76..876aa299 100644 --- a/cache/src/main/kotlin/org/openrs2/cache/Js5MasterIndex.kt +++ b/cache/src/main/kotlin/org/openrs2/cache/Js5MasterIndex.kt @@ -66,6 +66,7 @@ public data class Js5MasterIndex( } } + @JvmOverloads public fun write(buf: ByteBuf, key: RSAKeyParameters? = null) { val start = buf.writerIndex() @@ -179,6 +180,7 @@ public data class Js5MasterIndex( return masterIndex } + @JvmOverloads public fun read(buf: ByteBuf, format: MasterIndexFormat, key: RSAKeyParameters? = null): Js5MasterIndex { return read(buf, format, key, true) } diff --git a/cache/src/main/kotlin/org/openrs2/cache/Store.kt b/cache/src/main/kotlin/org/openrs2/cache/Store.kt index 29256c2d..4b1d3d1f 100644 --- a/cache/src/main/kotlin/org/openrs2/cache/Store.kt +++ b/cache/src/main/kotlin/org/openrs2/cache/Store.kt @@ -135,6 +135,7 @@ public interface Store : Flushable, Closeable { * groups read from the [Store] and for temporary internal use. * @throws IOException if an underlying I/O error occurs. */ + @JvmOverloads public fun open(root: Path, alloc: ByteBufAllocator = ByteBufAllocator.DEFAULT): Store { val hasDataFile = Files.isRegularFile(DiskStore.dataPath(root)) val hasLegacyDataFile = Files.isRegularFile(DiskStore.legacyDataPath(root))