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")) implementation(project(":util"))
testImplementation(project(":buffer"))
testImplementation("com.google.jimfs:jimfs:${Versions.jimfs}") 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.Configuration
import com.google.common.jimfs.Jimfs import com.google.common.jimfs.Jimfs
import dev.openrs2.buffer.use
import dev.openrs2.util.io.useTempFile import dev.openrs2.util.io.useTempFile
import io.netty.buffer.Unpooled import io.netty.buffer.Unpooled
import org.bouncycastle.crypto.params.RSAKeyParameters import org.bouncycastle.crypto.params.RSAKeyParameters
@ -76,41 +77,23 @@ object RsaTest {
@Test @Test
fun testEncryptByteBuf() { fun testEncryptByteBuf() {
val plaintext = Unpooled.wrappedBuffer(byteArrayOf(65)) Unpooled.wrappedBuffer(byteArrayOf(65)).use { plaintext ->
try { plaintext.rsaEncrypt(PUBLIC_KEY).use { ciphertext ->
val ciphertext = plaintext.rsaEncrypt(PUBLIC_KEY) Unpooled.wrappedBuffer(byteArrayOf(10, 230.toByte())).use { expectedCiphertext ->
try {
val expectedCiphertext = Unpooled.wrappedBuffer(byteArrayOf(10, 230.toByte()))
try {
assertEquals(expectedCiphertext, ciphertext) assertEquals(expectedCiphertext, ciphertext)
} finally {
expectedCiphertext.release()
} }
} finally {
ciphertext.release()
} }
} finally {
plaintext.release()
} }
} }
@Test @Test
fun testDecryptByteBuf() { fun testDecryptByteBuf() {
val ciphertext = Unpooled.wrappedBuffer(byteArrayOf(10, 230.toByte())) Unpooled.wrappedBuffer(byteArrayOf(10, 230.toByte())).use { ciphertext ->
try { ciphertext.rsaDecrypt(PRIVATE_KEY).use { plaintext ->
val plaintext = ciphertext.rsaDecrypt(PRIVATE_KEY) Unpooled.wrappedBuffer(byteArrayOf(65)).use { expectedPlaintext ->
try {
val expectedPlaintext = Unpooled.wrappedBuffer(byteArrayOf(65))
try {
assertEquals(expectedPlaintext, plaintext) assertEquals(expectedPlaintext, plaintext)
} finally {
expectedPlaintext.release()
} }
} finally {
plaintext.release()
} }
} finally {
ciphertext.release()
} }
} }

@ -1,5 +1,6 @@
package dev.openrs2.crypto package dev.openrs2.crypto
import dev.openrs2.buffer.use
import io.netty.buffer.ByteBufUtil import io.netty.buffer.ByteBufUtil
import io.netty.buffer.Unpooled import io.netty.buffer.Unpooled
import kotlin.test.Test import kotlin.test.Test
@ -39,18 +40,12 @@ object XteaTest {
val header = ByteArray(i) { it.toByte() } val header = ByteArray(i) { it.toByte() }
val trailer = ByteArray(j) { it.toByte() } val trailer = ByteArray(j) { it.toByte() }
val buffer = Unpooled.copiedBuffer(header, vector.plaintext, trailer) Unpooled.copiedBuffer(header, vector.plaintext, trailer).use { buffer ->
try {
buffer.xteaEncrypt(i, vector.plaintext.size, vector.key) buffer.xteaEncrypt(i, vector.plaintext.size, vector.key)
val expected = Unpooled.wrappedBuffer(header, vector.ciphertext, trailer) Unpooled.wrappedBuffer(header, vector.ciphertext, trailer).use { expected ->
try {
assertEquals(expected, buffer) assertEquals(expected, buffer)
} finally {
expected.release()
} }
} finally {
buffer.release()
} }
} }
} }
@ -65,18 +60,12 @@ object XteaTest {
val header = ByteArray(i) { it.toByte() } val header = ByteArray(i) { it.toByte() }
val trailer = ByteArray(j) { it.toByte() } val trailer = ByteArray(j) { it.toByte() }
val buffer = Unpooled.copiedBuffer(header, vector.ciphertext, trailer) Unpooled.copiedBuffer(header, vector.ciphertext, trailer).use { buffer ->
try {
buffer.xteaDecrypt(i, vector.ciphertext.size, vector.key) buffer.xteaDecrypt(i, vector.ciphertext.size, vector.key)
val expected = Unpooled.wrappedBuffer(header, vector.plaintext, trailer) Unpooled.wrappedBuffer(header, vector.plaintext, trailer).use { expected ->
try {
assertEquals(expected, buffer) assertEquals(expected, buffer)
} finally {
expected.release()
} }
} finally {
buffer.release()
} }
} }
} }

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

Loading…
Cancel
Save