2323import java .util .Arrays ;
2424import java .util .stream .IntStream ;
2525
26- import io .netty .buffer .ByteBuf ;
2726import lombok .Getter ;
27+ import lombok .NonNull ;
2828import lombok .experimental .Delegate ;
2929import lombok .experimental .FieldDefaults ;
3030import lombok .experimental .NonFinal ;
3838@ FieldDefaults (level = PRIVATE , makeFinal = true )
3939public final class Bytes {
4040
41- public static Bytes wrap (byte [] bytes ) {
41+ public static Bytes wrap (@ NonNull byte [] bytes ) {
4242 val result = allocate (bytes .length );
43- return result .put (bytes );
44- }
45-
46- public static Bytes wrap (ByteBuffer buffer ) {
47- val clone = ByteBuffer .wrap (buffer .array ());
48- return new Bytes (clone );
43+ result .put (bytes );
44+ result .flip ();
45+ return result ;
4946 }
5047
51- public static Bytes wrap (ByteBuf buf ) {
52- ByteBuffer buffer ;
53- if (buf .isDirect ()) {
54- buffer = buf .nioBuffer ();
55- } else {
56- val bytes = new byte [buf .readableBytes ()];
57- buf .getBytes (buf .readerIndex (), bytes );
58- buffer = ByteBuffer .wrap (bytes );
59- }
60- return new Bytes (buffer );
48+ public static Bytes wrap (@ NonNull ByteBuffer buffer ) {
49+ return wrap (buffer .array ());
6150 }
6251
6352 public static Bytes allocate () {
@@ -79,15 +68,20 @@ public static Bytes allocate (int capacity) {
7968 @ Getter (value = PACKAGE )
8069 ByteBuffer buffer ;
8170
71+ @ NonFinal
72+ int limit ;
73+
8274 private Bytes (ByteBuffer buffer ) {
8375 puts = new BytesDelegatePuts (this );
8476 gets = new BytesDelegateGets (this );
8577 this .buffer = buffer ;
78+ limit = buffer .position ();
8679 }
8780
8881 public Bytes put (byte value ) {
8982 checkCapacity (1 );
9083 buffer .put (value );
84+ limit ++;
9185 return this ;
9286 }
9387
@@ -96,13 +90,14 @@ public Bytes put (int index, byte value) {
9690 return this ;
9791 }
9892
99- public Bytes put (byte [] bytes ) {
93+ public Bytes put (@ NonNull byte [] bytes ) {
10094 checkCapacity (bytes .length );
10195 buffer .put (bytes );
96+ limit += bytes .length ;
10297 return this ;
10398 }
10499
105- public Bytes put (int index , byte [] bytes ) {
100+ public Bytes put (int index , @ NonNull byte [] bytes ) {
106101 checkCapacity (index , bytes .length );
107102 IntStream .range (index , index + bytes .length ).forEach (it -> {
108103 buffer .put (it , bytes [it - index ]);
@@ -133,32 +128,39 @@ public byte[] getBytes (int offset, int length) {
133128 }
134129
135130 public byte [] array () {
136- return Arrays .copyOfRange (buffer .array (), 0 , buffer . position () );
131+ return Arrays .copyOfRange (buffer .array (), 0 , limit );
137132 }
138133
139134 public int limit () {
140- return buffer . limit () ;
135+ return limit ;
141136 }
142137
143138 public int remaining () {
144- return buffer .remaining ();
139+ return limit - buffer .position ();
145140 }
146141
147142 public int position () {
148143 return buffer .position ();
149144 }
150145
151146 public Bytes position (int position ) {
147+ if (position > limit || position < 0 ) {
148+ throw new IllegalArgumentException ();
149+ }
152150 buffer .position (position );
153151 return this ;
154152 }
155153
156- public void clear () {
154+ public Bytes clear () {
157155 buffer .clear ();
156+ limit = 0 ;
157+ return this ;
158158 }
159159
160- public void flip () {
160+ public Bytes flip () {
161+ limit = buffer .position ();
161162 buffer .flip ();
163+ return this ;
162164 }
163165
164166 private void checkCapacity (int size ) {
0 commit comments