Add use extension method for releasing reference-counted objects

Signed-off-by: Graham <gpe@openrs2.dev>
Graham 4 years ago
parent 0eda020092
commit e0708458f9
  1. 25
      buffer/build.gradle.kts
  2. 11
      buffer/src/main/java/dev/openrs2/buffer/ReferenceCountedExtensions.kt
  3. 1
      crypto/build.gradle.kts
  4. 31
      crypto/src/test/java/dev/openrs2/crypto/RsaTest.kt
  5. 21
      crypto/src/test/java/dev/openrs2/crypto/XteaTest.kt
  6. 1
      settings.gradle.kts

@ -0,0 +1,25 @@
plugins {
`maven-publish`
kotlin("jvm")
}
dependencies {
api("io.netty:netty-buffer:${Versions.netty}")
}
publishing {
publications.create<MavenPublication>("maven") {
from(components["java"])
pom {
packaging = "jar"
name.set("OpenRS2 Buffer")
description.set(
"""
Provides RuneScape-specific extension methods for Netty's
ByteBuf type.
""".trimIndent()
)
}
}
}

@ -0,0 +1,11 @@
package dev.openrs2.buffer
import io.netty.util.ReferenceCounted
inline fun <T : ReferenceCounted, R> T.use(block: (T) -> R): R {
try {
return block(this)
} finally {
release()
}
}

@ -11,6 +11,7 @@ dependencies {
implementation(project(":util"))
testImplementation(project(":buffer"))
testImplementation("com.google.jimfs:jimfs:${Versions.jimfs}")
}

@ -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()
}
}

@ -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()
}
}
}

@ -3,6 +3,7 @@ rootProject.name = "openrs2"
include(
"all",
"asm",
"buffer",
"bundler",
"compress",
"compress-cli",

Loading…
Cancel
Save