|
33 | 33 | class ByteBufStreamInput extends StreamInput {
|
34 | 34 |
|
35 | 35 | private final ByteBuf buffer;
|
36 |
| - private final int startIndex; |
37 | 36 | private final int endIndex;
|
38 | 37 |
|
39 | 38 | ByteBufStreamInput(ByteBuf buffer, int length) {
|
40 | 39 | if (length > buffer.readableBytes()) {
|
41 | 40 | throw new IndexOutOfBoundsException();
|
42 | 41 | }
|
43 | 42 | this.buffer = buffer;
|
44 |
| - startIndex = buffer.readerIndex(); |
| 43 | + int startIndex = buffer.readerIndex(); |
45 | 44 | endIndex = startIndex + length;
|
46 | 45 | buffer.markReaderIndex();
|
47 | 46 | }
|
48 | 47 |
|
49 | 48 | @Override
|
50 | 49 | public BytesReference readBytesReference(int length) throws IOException {
|
51 |
| - BytesReference ref = Netty4Utils.toBytesReference(buffer.slice(buffer.readerIndex(), length)); |
52 |
| - buffer.skipBytes(length); |
53 |
| - return ref; |
| 50 | + // NOTE: It is unsafe to share a reference of the internal structure, so we |
| 51 | + // use the default implementation which will copy the bytes. It is unsafe because |
| 52 | + // a netty ByteBuf might be pooled which requires a manual release to prevent |
| 53 | + // memory leaks. |
| 54 | + return super.readBytesReference(length); |
54 | 55 | }
|
55 | 56 |
|
56 | 57 | @Override
|
57 | 58 | public BytesRef readBytesRef(int length) throws IOException {
|
58 |
| - if (!buffer.hasArray()) { |
59 |
| - return super.readBytesRef(length); |
60 |
| - } |
61 |
| - BytesRef bytesRef = new BytesRef(buffer.array(), buffer.arrayOffset() + buffer.readerIndex(), length); |
62 |
| - buffer.skipBytes(length); |
63 |
| - return bytesRef; |
| 59 | + // NOTE: It is unsafe to share a reference of the internal structure, so we |
| 60 | + // use the default implementation which will copy the bytes. It is unsafe because |
| 61 | + // a netty ByteBuf might be pooled which requires a manual release to prevent |
| 62 | + // memory leaks. |
| 63 | + return super.readBytesRef(length); |
64 | 64 | }
|
65 | 65 |
|
66 | 66 | @Override
|
|
0 commit comments