diff --git a/src/main/java/io/github/nstdio/http/ext/Buffers.java b/src/main/java/io/github/nstdio/http/ext/Buffers.java index 2ce9ff3..7d42a23 100644 --- a/src/main/java/io/github/nstdio/http/ext/Buffers.java +++ b/src/main/java/io/github/nstdio/http/ext/Buffers.java @@ -26,6 +26,10 @@ private Buffers() { } static ByteBuffer duplicate(ByteBuffer buf) { + if (buf.capacity() == 0) { + return buf; + } + var dup = buf.asReadOnlyBuffer(); return dup.hasRemaining() ? dup : dup.flip(); } diff --git a/src/test/kotlin/io/github/nstdio/http/ext/BuffersTest.kt b/src/test/kotlin/io/github/nstdio/http/ext/BuffersTest.kt index df65bc0..07975e7 100644 --- a/src/test/kotlin/io/github/nstdio/http/ext/BuffersTest.kt +++ b/src/test/kotlin/io/github/nstdio/http/ext/BuffersTest.kt @@ -15,6 +15,7 @@ */ package io.github.nstdio.http.ext +import io.kotest.matchers.types.shouldBeSameInstanceAs import io.kotest.property.Arb import io.kotest.property.arbitrary.int import io.kotest.property.arbitrary.map @@ -54,6 +55,18 @@ internal class BuffersTest { assertThat(actual.position()).isZero } + @Test + fun shouldNotCreateNewBufferIfInputIsEmpty() { + //given + val buffer = ByteBuffer.allocate(0) + + //when + val actual = Buffers.duplicate(buffer) + + //then + actual.shouldBeSameInstanceAs(buffer) + } + companion object { @JvmStatic fun listBuffersData(): Stream> {