diff --git a/buffer/src/main/java/io/netty/buffer/Unpooled.java b/buffer/src/main/java/io/netty/buffer/Unpooled.java index 6e1654e41c..51c4a32361 100644 --- a/buffer/src/main/java/io/netty/buffer/Unpooled.java +++ b/buffer/src/main/java/io/netty/buffer/Unpooled.java @@ -408,8 +408,16 @@ public final class Unpooled { * respectively. */ public static ByteBuf copiedBuffer(ByteBuf buffer) { - if (buffer.isReadable()) { - return buffer.copy(); + int readable = buffer.readableBytes(); + if (readable > 0) { + ByteBuf copy; + if (buffer.isDirect()) { + copy = directBuffer(readable); + } else { + copy = buffer(readable); + } + copy.writeBytes(buffer, buffer.readerIndex(), readable); + return copy; } else { return EMPTY_BUFFER; }