|
16 | 16 |
|
17 | 17 | package io.appulse.utils; |
18 | 18 |
|
19 | | -import static lombok.AccessLevel.PACKAGE; |
20 | 19 | import static lombok.AccessLevel.PRIVATE; |
21 | 20 |
|
| 21 | +import java.nio.ByteBuffer; |
22 | 22 | import java.nio.charset.Charset; |
| 23 | +import java.util.Arrays; |
23 | 24 |
|
24 | 25 | import io.netty.buffer.ByteBuf; |
| 26 | +import io.netty.buffer.Unpooled; |
25 | 27 | import lombok.NonNull; |
26 | | -import lombok.RequiredArgsConstructor; |
27 | 28 | import lombok.experimental.FieldDefaults; |
28 | 29 | import lombok.val; |
29 | 30 |
|
30 | 31 | @SuppressWarnings("PMD.LinguisticNaming") |
31 | | -@RequiredArgsConstructor(access = PACKAGE) |
32 | | -@FieldDefaults(level = PRIVATE, makeFinal = true) |
33 | | -class BytesByteBuf extends BytesAbstract { |
| 32 | +@FieldDefaults(level = PRIVATE) |
| 33 | +public final class BytesByteBuf extends BytesAbstract { |
34 | 34 |
|
35 | | - static BytesByteBuf copy (@NonNull ByteBuf buffer) { |
| 35 | + public static BytesByteBuf allocate (int size) { |
| 36 | + val buffer = Unpooled.buffer(size); |
| 37 | + buffer.clear(); |
| 38 | + return new BytesByteBuf(buffer); |
| 39 | + } |
| 40 | + |
| 41 | + public static BytesByteBuf wrap (@NonNull byte[] bytes) { |
| 42 | + val wrapped = Unpooled.wrappedBuffer(bytes); |
| 43 | + wrapped.clear(); |
| 44 | + return new BytesByteBuf(wrapped); |
| 45 | + } |
| 46 | + |
| 47 | + public static BytesByteBuf wrap (@NonNull ByteBuffer buffer) { |
| 48 | + val wrapped = Unpooled.wrappedBuffer(buffer); |
| 49 | + return new BytesByteBuf(wrapped); |
| 50 | + } |
| 51 | + |
| 52 | + public static BytesByteBuf wrap (@NonNull ByteBuf buffer) { |
| 53 | + val wrapped = Unpooled.wrappedBuffer(buffer); |
| 54 | + return new BytesByteBuf(wrapped); |
| 55 | + } |
| 56 | + |
| 57 | + public static BytesByteBuf copy (@NonNull byte[] bytes) { |
| 58 | + val copy = Unpooled.copiedBuffer(bytes); |
| 59 | + return new BytesByteBuf(copy); |
| 60 | + } |
| 61 | + |
| 62 | + public static BytesByteBuf copy (@NonNull ByteBuffer buffer) { |
| 63 | + val copy = Unpooled.copiedBuffer(buffer); |
| 64 | + return new BytesByteBuf(copy); |
| 65 | + } |
| 66 | + |
| 67 | + public static BytesByteBuf copy (@NonNull ByteBuf buffer) { |
36 | 68 | val copy = buffer.copy(); |
37 | 69 | return new BytesByteBuf(copy); |
38 | 70 | } |
39 | 71 |
|
40 | | - @NonNull |
41 | 72 | ByteBuf buffer; |
42 | 73 |
|
| 74 | + private BytesByteBuf (@NonNull ByteBuf buffer) { |
| 75 | + super(); |
| 76 | + this.buffer = buffer; |
| 77 | + } |
| 78 | + |
43 | 79 | @Override |
44 | 80 | public boolean isResizable () { |
45 | 81 | return false; |
@@ -199,6 +235,21 @@ public int capacity () { |
199 | 235 | return buffer.capacity(); |
200 | 236 | } |
201 | 237 |
|
| 238 | + @Override |
| 239 | + public void capacity (int bytes) { |
| 240 | + if (capacity() == bytes) { |
| 241 | + return; |
| 242 | + } |
| 243 | + |
| 244 | + val oldReaderIndex = buffer.readerIndex(); |
| 245 | + val oldWriterIndex = buffer.writerIndex(); |
| 246 | + val newByteArray = Arrays.copyOf(buffer.array(), bytes); |
| 247 | + |
| 248 | + buffer = Unpooled.wrappedBuffer(newByteArray); |
| 249 | + buffer.readerIndex(Math.min(oldReaderIndex, bytes - 1)); |
| 250 | + buffer.writerIndex(Math.min(oldWriterIndex, bytes - 1)); |
| 251 | + } |
| 252 | + |
202 | 253 | @Override |
203 | 254 | public int writerIndex () { |
204 | 255 | return buffer.writerIndex(); |
|
0 commit comments