Allocate buffer from ByteBufAllocator in ChunkedFile
This commit is contained in:
parent
3db3a9a2bf
commit
d8479a04df
@ -16,7 +16,6 @@
|
||||
package io.netty.handler.stream;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.FileRegion;
|
||||
|
||||
@ -148,9 +147,18 @@ public class ChunkedFile implements ChunkedInput<ByteBuf> {
|
||||
int chunkSize = (int) Math.min(this.chunkSize, endOffset - offset);
|
||||
// Check if the buffer is backed by an byte array. If so we can optimize it a bit an safe a copy
|
||||
|
||||
byte[] array = new byte[chunkSize];
|
||||
file.readFully(array);
|
||||
ByteBuf buf = ctx.alloc().heapBuffer(chunkSize);
|
||||
boolean release = true;
|
||||
try {
|
||||
file.readFully(buf.array(), buf.arrayOffset(), chunkSize);
|
||||
buf.writerIndex(chunkSize);
|
||||
this.offset = offset + chunkSize;
|
||||
return Unpooled.wrappedBuffer(array);
|
||||
release = false;
|
||||
return buf;
|
||||
} finally {
|
||||
if (release) {
|
||||
buf.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user