From 5e447f31d604580b8be59b23f4f0eeb8f3ba0784 Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 30 Aug 2020 08:55:06 +0100 Subject: [PATCH] Free readBuffer if the writeBuffer allocation fails Signed-off-by: Graham --- .../java/dev/openrs2/cache/BufferedFileChannel.kt | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/cache/src/main/java/dev/openrs2/cache/BufferedFileChannel.kt b/cache/src/main/java/dev/openrs2/cache/BufferedFileChannel.kt index 80a26f2c..c6010163 100644 --- a/cache/src/main/java/dev/openrs2/cache/BufferedFileChannel.kt +++ b/cache/src/main/java/dev/openrs2/cache/BufferedFileChannel.kt @@ -17,12 +17,22 @@ public class BufferedFileChannel( ) : Flushable, Closeable { private var size = channel.size() - private val readBuffer = alloc.buffer(readBufferSize, readBufferSize) + private val readBuffer: ByteBuf private var readPos = -1L - private val writeBuffer = alloc.buffer(writeBufferSize, writeBufferSize) + private val writeBuffer: ByteBuf private var writePos = -1L + init { + val buf = alloc.buffer(readBufferSize, readBufferSize) + try { + writeBuffer = alloc.buffer(writeBufferSize, writeBufferSize) + readBuffer = buf.retain() + } finally { + buf.release() + } + } + public fun read(pos: Long, dest: ByteBuf, len: Int) { require(pos >= 0) require(len <= dest.writableBytes())