From e0708458f95555453202ef8a09b5b2a6a3454396 Mon Sep 17 00:00:00 2001 From: Graham Date: Fri, 24 Jul 2020 20:16:01 +0100 Subject: [PATCH] Add use extension method for releasing reference-counted objects Signed-off-by: Graham --- buffer/build.gradle.kts | 25 +++++++++++++++ .../buffer/ReferenceCountedExtensions.kt | 11 +++++++ crypto/build.gradle.kts | 1 + .../test/java/dev/openrs2/crypto/RsaTest.kt | 31 +++++-------------- .../test/java/dev/openrs2/crypto/XteaTest.kt | 21 +++---------- settings.gradle.kts | 1 + 6 files changed, 50 insertions(+), 40 deletions(-) create mode 100644 buffer/build.gradle.kts create mode 100644 buffer/src/main/java/dev/openrs2/buffer/ReferenceCountedExtensions.kt diff --git a/buffer/build.gradle.kts b/buffer/build.gradle.kts new file mode 100644 index 0000000000..28cb511183 --- /dev/null +++ b/buffer/build.gradle.kts @@ -0,0 +1,25 @@ +plugins { + `maven-publish` + kotlin("jvm") +} + +dependencies { + api("io.netty:netty-buffer:${Versions.netty}") +} + +publishing { + publications.create("maven") { + from(components["java"]) + + pom { + packaging = "jar" + name.set("OpenRS2 Buffer") + description.set( + """ + Provides RuneScape-specific extension methods for Netty's + ByteBuf type. + """.trimIndent() + ) + } + } +} diff --git a/buffer/src/main/java/dev/openrs2/buffer/ReferenceCountedExtensions.kt b/buffer/src/main/java/dev/openrs2/buffer/ReferenceCountedExtensions.kt new file mode 100644 index 0000000000..e8e8a1ab13 --- /dev/null +++ b/buffer/src/main/java/dev/openrs2/buffer/ReferenceCountedExtensions.kt @@ -0,0 +1,11 @@ +package dev.openrs2.buffer + +import io.netty.util.ReferenceCounted + +inline fun T.use(block: (T) -> R): R { + try { + return block(this) + } finally { + release() + } +} diff --git a/crypto/build.gradle.kts b/crypto/build.gradle.kts index e2d42fd0de..dc63914b4f 100644 --- a/crypto/build.gradle.kts +++ b/crypto/build.gradle.kts @@ -11,6 +11,7 @@ dependencies { implementation(project(":util")) + testImplementation(project(":buffer")) testImplementation("com.google.jimfs:jimfs:${Versions.jimfs}") } diff --git a/crypto/src/test/java/dev/openrs2/crypto/RsaTest.kt b/crypto/src/test/java/dev/openrs2/crypto/RsaTest.kt index 08019fbc0c..c476e8c464 100644 --- a/crypto/src/test/java/dev/openrs2/crypto/RsaTest.kt +++ b/crypto/src/test/java/dev/openrs2/crypto/RsaTest.kt @@ -2,6 +2,7 @@ package dev.openrs2.crypto import com.google.common.jimfs.Configuration import com.google.common.jimfs.Jimfs +import dev.openrs2.buffer.use import dev.openrs2.util.io.useTempFile import io.netty.buffer.Unpooled import org.bouncycastle.crypto.params.RSAKeyParameters @@ -76,41 +77,23 @@ object RsaTest { @Test fun testEncryptByteBuf() { - val plaintext = Unpooled.wrappedBuffer(byteArrayOf(65)) - try { - val ciphertext = plaintext.rsaEncrypt(PUBLIC_KEY) - try { - val expectedCiphertext = Unpooled.wrappedBuffer(byteArrayOf(10, 230.toByte())) - try { + Unpooled.wrappedBuffer(byteArrayOf(65)).use { plaintext -> + plaintext.rsaEncrypt(PUBLIC_KEY).use { ciphertext -> + Unpooled.wrappedBuffer(byteArrayOf(10, 230.toByte())).use { expectedCiphertext -> assertEquals(expectedCiphertext, ciphertext) - } finally { - expectedCiphertext.release() } - } finally { - ciphertext.release() } - } finally { - plaintext.release() } } @Test fun testDecryptByteBuf() { - val ciphertext = Unpooled.wrappedBuffer(byteArrayOf(10, 230.toByte())) - try { - val plaintext = ciphertext.rsaDecrypt(PRIVATE_KEY) - try { - val expectedPlaintext = Unpooled.wrappedBuffer(byteArrayOf(65)) - try { + Unpooled.wrappedBuffer(byteArrayOf(10, 230.toByte())).use { ciphertext -> + ciphertext.rsaDecrypt(PRIVATE_KEY).use { plaintext -> + Unpooled.wrappedBuffer(byteArrayOf(65)).use { expectedPlaintext -> assertEquals(expectedPlaintext, plaintext) - } finally { - expectedPlaintext.release() } - } finally { - plaintext.release() } - } finally { - ciphertext.release() } } diff --git a/crypto/src/test/java/dev/openrs2/crypto/XteaTest.kt b/crypto/src/test/java/dev/openrs2/crypto/XteaTest.kt index 267809fc07..9f034c7571 100644 --- a/crypto/src/test/java/dev/openrs2/crypto/XteaTest.kt +++ b/crypto/src/test/java/dev/openrs2/crypto/XteaTest.kt @@ -1,5 +1,6 @@ package dev.openrs2.crypto +import dev.openrs2.buffer.use import io.netty.buffer.ByteBufUtil import io.netty.buffer.Unpooled import kotlin.test.Test @@ -39,18 +40,12 @@ object XteaTest { val header = ByteArray(i) { it.toByte() } val trailer = ByteArray(j) { it.toByte() } - val buffer = Unpooled.copiedBuffer(header, vector.plaintext, trailer) - try { + Unpooled.copiedBuffer(header, vector.plaintext, trailer).use { buffer -> buffer.xteaEncrypt(i, vector.plaintext.size, vector.key) - val expected = Unpooled.wrappedBuffer(header, vector.ciphertext, trailer) - try { + Unpooled.wrappedBuffer(header, vector.ciphertext, trailer).use { expected -> assertEquals(expected, buffer) - } finally { - expected.release() } - } finally { - buffer.release() } } } @@ -65,18 +60,12 @@ object XteaTest { val header = ByteArray(i) { it.toByte() } val trailer = ByteArray(j) { it.toByte() } - val buffer = Unpooled.copiedBuffer(header, vector.ciphertext, trailer) - try { + Unpooled.copiedBuffer(header, vector.ciphertext, trailer).use { buffer -> buffer.xteaDecrypt(i, vector.ciphertext.size, vector.key) - val expected = Unpooled.wrappedBuffer(header, vector.plaintext, trailer) - try { + Unpooled.wrappedBuffer(header, vector.plaintext, trailer).use { expected -> assertEquals(expected, buffer) - } finally { - expected.release() } - } finally { - buffer.release() } } } diff --git a/settings.gradle.kts b/settings.gradle.kts index e346414f7d..5c69d97148 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -3,6 +3,7 @@ rootProject.name = "openrs2" include( "all", "asm", + "buffer", "bundler", "compress", "compress-cli",