44
55/**
66 * Byte array operation helper.
7- * Without bounds check.
7+ * Without bounds check(for higher performance) .
88 *
99 * @author honhimW
1010 * @see java.nio.ByteBuffer something like that, but without checking.
@@ -26,55 +26,123 @@ public Bytes(byte[] bytes) {
2626 this .pos = 0 ;
2727 }
2828
29+ /**
30+ * Append int to current position.
31+ *
32+ * @param value int value
33+ * @return self
34+ */
2935 public Bytes putInt (int value ) {
3036 this .pos = putInt (this .bytes , this .pos , value );
3137 return this ;
3238 }
3339
40+ /**
41+ * Append long to current position.
42+ *
43+ * @param value long value
44+ * @return self
45+ */
3446 public Bytes putLong (long value ) {
3547 this .pos = putLong (this .bytes , this .pos , value );
3648 return this ;
3749 }
3850
51+ /**
52+ * Append short to current position.
53+ *
54+ * @param value short value
55+ * @return self
56+ */
3957 public Bytes putShort (short value ) {
4058 this .pos = putShort (this .bytes , this .pos , value );
4159 return this ;
4260 }
4361
62+ /**
63+ * Append byte to current position.
64+ *
65+ * @param value byte value
66+ * @return self
67+ */
4468 public Bytes put (byte value ) {
4569 this .pos = put (this .bytes , this .pos , value );
4670 return this ;
4771 }
4872
73+ /**
74+ * Append byte to target position.
75+ *
76+ * @param value int value
77+ * @return self
78+ */
4979 public Bytes put (int i , byte value ) {
5080 this .bytes [i ] = value ;
5181 return this ;
5282 }
5383
84+ /**
85+ * Append byte-array to current position.
86+ *
87+ * @param value byte array
88+ * @return self
89+ */
5490 public Bytes put (byte [] value ) {
5591 this .pos = put (this .bytes , this .pos , value );
5692 return this ;
5793 }
5894
95+ /**
96+ * Append byte-array to current position.
97+ *
98+ * @param value byte array
99+ * @param offset value offset
100+ * @param length value length after offset
101+ * @return self
102+ */
59103 public Bytes put (byte [] value , int offset , int length ) {
60104 this .pos = put (this .bytes , this .pos , value , offset , length );
61105 return this ;
62106 }
63107
108+ /**
109+ * Get result
110+ *
111+ * @return result
112+ */
64113 public byte [] unwrap () {
65114 return this .bytes ;
66115 }
67116
117+ /**
118+ * Get one byte by index
119+ *
120+ * @param i index
121+ * @return single byte
122+ */
68123 public byte get (int i ) {
69124 return this .bytes [i ];
70125 }
71126
127+ /**
128+ * Copy byte-array from offset with length
129+ *
130+ * @param offset start position
131+ * @param length read length
132+ * @return result
133+ */
72134 public byte [] get (int offset , int length ) {
73135 byte [] _bytes = new byte [length ];
74136 System .arraycopy (this .bytes , offset , _bytes , 0 , length );
75137 return _bytes ;
76138 }
77139
140+ /**
141+ * Read one int value from target index.
142+ *
143+ * @param i index
144+ * @return int value
145+ */
78146 public int getInt (int i ) {
79147 int v = 0 ;
80148 v |= Byte .toUnsignedInt (bytes [i ]) << 24 ;
@@ -84,6 +152,12 @@ public int getInt(int i) {
84152 return v ;
85153 }
86154
155+ /**
156+ * Read one long value from target index.
157+ *
158+ * @param i index
159+ * @return long value
160+ */
87161 public long getLong (int i ) {
88162 long v = 0 ;
89163 v |= Byte .toUnsignedLong (bytes [i ]) << 56 ;
@@ -97,17 +171,34 @@ public long getLong(int i) {
97171 return v ;
98172 }
99173
174+ /**
175+ * Read short int value from target index.
176+ *
177+ * @param i index
178+ * @return short value
179+ */
100180 public short getShort (int i ) {
101181 int v = 0 ;
102182 v |= Byte .toUnsignedInt (bytes [i ]) << 8 ;
103183 v |= Byte .toUnsignedInt (bytes [i + 1 ]);
104184 return (short ) v ;
105185 }
106186
187+ /**
188+ * Get current position
189+ *
190+ * @return position
191+ */
107192 public int position () {
108193 return this .pos ;
109194 }
110195
196+ /**
197+ * Set current index.
198+ *
199+ * @param index target index
200+ * @return self
201+ */
111202 public Bytes position (int index ) {
112203 this .pos = index ;
113204 return this ;
@@ -142,6 +233,14 @@ public String toString() {
142233 return sb .toString ();
143234 }
144235
236+ /**
237+ * Add int value into byte-array with index.
238+ *
239+ * @param dist target
240+ * @param index position
241+ * @param value value
242+ * @return next index
243+ */
145244 public static int putInt (byte [] dist , int index , int value ) {
146245 dist [index ] = (byte ) (value >> 24 );
147246 dist [index + 1 ] = (byte ) (value >> 16 );
@@ -150,12 +249,28 @@ public static int putInt(byte[] dist, int index, int value) {
150249 return index + 4 ;
151250 }
152251
252+ /**
253+ * Add short value into byte-array with index.
254+ *
255+ * @param dist target
256+ * @param index position
257+ * @param value value
258+ * @return next index
259+ */
153260 public static int putShort (byte [] dist , int index , short value ) {
154261 dist [index ] = (byte ) (value >> 8 );
155262 dist [index + 1 ] = (byte ) value ;
156263 return index + 2 ;
157264 }
158265
266+ /**
267+ * Add long value into byte-array with index.
268+ *
269+ * @param dist target
270+ * @param index position
271+ * @param value value
272+ * @return next index
273+ */
159274 public static int putLong (byte [] dist , int index , long value ) {
160275 dist [index ] = (byte ) (value >> 56 );
161276 dist [index + 1 ] = (byte ) (value >> 48 );
@@ -168,11 +283,27 @@ public static int putLong(byte[] dist, int index, long value) {
168283 return index + 8 ;
169284 }
170285
286+ /**
287+ * Add byte value into byte-array with index.
288+ *
289+ * @param dist target
290+ * @param index position
291+ * @param value value
292+ * @return next index
293+ */
171294 public static int put (byte [] dist , int index , byte value ) {
172295 dist [index ] = value ;
173296 return index + 1 ;
174297 }
175298
299+ /**
300+ * Add byte-array into byte-array with index.
301+ *
302+ * @param dist target
303+ * @param index position
304+ * @param value value
305+ * @return next index
306+ */
176307 public static int put (byte [] dist , int index , byte [] value ) {
177308 for (byte b : value ) {
178309 dist [index ] = b ;
@@ -181,6 +312,16 @@ public static int put(byte[] dist, int index, byte[] value) {
181312 return index ;
182313 }
183314
315+ /**
316+ * Add int value into byte-array with index.
317+ *
318+ * @param dist target
319+ * @param index position
320+ * @param value source
321+ * @param offset source offset
322+ * @param length source length
323+ * @return next index
324+ */
184325 public static int put (byte [] dist , int index , byte [] value , int offset , int length ) {
185326 for (int i = 0 ; i < length ; i ++) {
186327 dist [index ] = value [i + offset ];
@@ -189,6 +330,13 @@ public static int put(byte[] dist, int index, byte[] value, int offset, int leng
189330 return index ;
190331 }
191332
333+ /**
334+ * Copy byte-array from 0 into new array.
335+ *
336+ * @param src source
337+ * @param length read length
338+ * @return new array
339+ */
192340 public static byte [] copyOf (byte [] src , int length ) {
193341 byte [] dst = new byte [length ];
194342 System .arraycopy (src , 0 , dst , 0 , length );
0 commit comments