1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
37
37
*
38
38
* @author Arjen Poutsma
39
39
* @author Juergen Hoeller
40
+ * @author Brian Clozel
40
41
* @since 5.0
41
42
* @see DefaultDataBufferFactory
42
43
*/
@@ -99,8 +100,7 @@ public DefaultDataBufferFactory factory() {
99
100
100
101
@ Override
101
102
public int indexOf (IntPredicate predicate , int fromIndex ) {
102
- Assert .notNull (predicate , "'predicate' must not be null" );
103
-
103
+ Assert .notNull (predicate , "IntPredicate must not be null" );
104
104
if (fromIndex < 0 ) {
105
105
fromIndex = 0 ;
106
106
}
@@ -118,7 +118,7 @@ else if (fromIndex >= this.writePosition) {
118
118
119
119
@ Override
120
120
public int lastIndexOf (IntPredicate predicate , int fromIndex ) {
121
- Assert .notNull (predicate , "'predicate' must not be null" );
121
+ Assert .notNull (predicate , "IntPredicate must not be null" );
122
122
int i = Math .min (fromIndex , this .writePosition - 1 );
123
123
for (; i >= 0 ; i --) {
124
124
byte b = this .byteBuffer .get (i );
@@ -149,7 +149,6 @@ public DefaultDataBuffer readPosition(int readPosition) {
149
149
assertIndex (readPosition >= 0 , "'readPosition' %d must be >= 0" , readPosition );
150
150
assertIndex (readPosition <= this .writePosition , "'readPosition' %d must be <= %d" ,
151
151
readPosition , this .writePosition );
152
-
153
152
this .readPosition = readPosition ;
154
153
return this ;
155
154
}
@@ -165,7 +164,6 @@ public DefaultDataBuffer writePosition(int writePosition) {
165
164
writePosition , this .readPosition );
166
165
assertIndex (writePosition <= this .capacity , "'writePosition' %d must be <= %d" ,
167
166
writePosition , this .capacity );
168
-
169
167
this .writePosition = writePosition ;
170
168
return this ;
171
169
}
@@ -177,9 +175,9 @@ public int capacity() {
177
175
178
176
@ Override
179
177
public DefaultDataBuffer capacity (int newCapacity ) {
180
- Assert . isTrue (newCapacity > 0 ,
181
- String .format ("'newCapacity' %d must be higher than 0" , newCapacity ));
182
-
178
+ if (newCapacity <= 0 ) {
179
+ throw new IllegalArgumentException ( String .format ("'newCapacity' %d must be higher than 0" , newCapacity ));
180
+ }
183
181
int readPosition = readPosition ();
184
182
int writePosition = writePosition ();
185
183
int oldCapacity = capacity ();
@@ -225,15 +223,13 @@ public DataBuffer ensureCapacity(int length) {
225
223
}
226
224
227
225
private static ByteBuffer allocate (int capacity , boolean direct ) {
228
- return direct ? ByteBuffer .allocateDirect (capacity ) : ByteBuffer .allocate (capacity );
226
+ return ( direct ? ByteBuffer .allocateDirect (capacity ) : ByteBuffer .allocate (capacity ) );
229
227
}
230
228
231
229
@ Override
232
230
public byte getByte (int index ) {
233
231
assertIndex (index >= 0 , "index %d must be >= 0" , index );
234
- assertIndex (index <= this .writePosition - 1 , "index %d must be <= %d" ,
235
- index , this .writePosition - 1 );
236
-
232
+ assertIndex (index <= this .writePosition - 1 , "index %d must be <= %d" , index , this .writePosition - 1 );
237
233
return this .byteBuffer .get (index );
238
234
}
239
235
@@ -249,14 +245,14 @@ public byte read() {
249
245
250
246
@ Override
251
247
public DefaultDataBuffer read (byte [] destination ) {
252
- Assert .notNull (destination , "'destination' must not be null" );
248
+ Assert .notNull (destination , "Byte array must not be null" );
253
249
read (destination , 0 , destination .length );
254
250
return this ;
255
251
}
256
252
257
253
@ Override
258
254
public DefaultDataBuffer read (byte [] destination , int offset , int length ) {
259
- Assert .notNull (destination , "'destination' must not be null" );
255
+ Assert .notNull (destination , "Byte array must not be null" );
260
256
assertIndex (this .readPosition <= this .writePosition - length ,
261
257
"readPosition %d and length %d should be smaller than writePosition %d" ,
262
258
this .readPosition , length , this .writePosition );
@@ -281,14 +277,14 @@ public DefaultDataBuffer write(byte b) {
281
277
282
278
@ Override
283
279
public DefaultDataBuffer write (byte [] source ) {
284
- Assert .notNull (source , "'source' must not be null" );
280
+ Assert .notNull (source , "Byte array must not be null" );
285
281
write (source , 0 , source .length );
286
282
return this ;
287
283
}
288
284
289
285
@ Override
290
286
public DefaultDataBuffer write (byte [] source , int offset , int length ) {
291
- Assert .notNull (source , "'source' must not be null" );
287
+ Assert .notNull (source , "Byte array must not be null" );
292
288
ensureCapacity (length );
293
289
294
290
ByteBuffer tmp = this .byteBuffer .duplicate ();
@@ -309,11 +305,12 @@ public DefaultDataBuffer write(DataBuffer... buffers) {
309
305
}
310
306
311
307
@ Override
312
- public DefaultDataBuffer write (ByteBuffer ... byteBuffers ) {
313
- Assert .notEmpty (byteBuffers , "'byteBuffers' must not be empty" );
314
- int capacity = Arrays .stream (byteBuffers ).mapToInt (ByteBuffer ::remaining ).sum ();
315
- ensureCapacity (capacity );
316
- Arrays .stream (byteBuffers ).forEach (this ::write );
308
+ public DefaultDataBuffer write (ByteBuffer ... buffers ) {
309
+ if (!ObjectUtils .isEmpty (buffers )) {
310
+ int capacity = Arrays .stream (buffers ).mapToInt (ByteBuffer ::remaining ).sum ();
311
+ ensureCapacity (capacity );
312
+ Arrays .stream (buffers ).forEach (this ::write );
313
+ }
317
314
return this ;
318
315
}
319
316
@@ -442,7 +439,7 @@ private void checkIndex(int index, int length) {
442
439
assertIndex (length <= this .capacity , "length %d must be <= %d" , index , this .capacity );
443
440
}
444
441
445
- private static void assertIndex (boolean expression , String format , Object ... args ) {
442
+ private void assertIndex (boolean expression , String format , Object ... args ) {
446
443
if (!expression ) {
447
444
String message = String .format (format , args );
448
445
throw new IndexOutOfBoundsException (message );
0 commit comments