|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2018 the original author or authors. |
|
|
|
* Copyright 2002-2019 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -37,6 +37,7 @@ import org.springframework.util.ObjectUtils; |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Arjen Poutsma |
|
|
|
* @author Arjen Poutsma |
|
|
|
* @author Juergen Hoeller |
|
|
|
* @author Juergen Hoeller |
|
|
|
|
|
|
|
* @author Brian Clozel |
|
|
|
* @since 5.0 |
|
|
|
* @since 5.0 |
|
|
|
* @see DefaultDataBufferFactory |
|
|
|
* @see DefaultDataBufferFactory |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@ -99,8 +100,7 @@ public class DefaultDataBuffer implements DataBuffer { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public int indexOf(IntPredicate predicate, int fromIndex) { |
|
|
|
public int indexOf(IntPredicate predicate, int fromIndex) { |
|
|
|
Assert.notNull(predicate, "'predicate' must not be null"); |
|
|
|
Assert.notNull(predicate, "IntPredicate must not be null"); |
|
|
|
|
|
|
|
|
|
|
|
if (fromIndex < 0) { |
|
|
|
if (fromIndex < 0) { |
|
|
|
fromIndex = 0; |
|
|
|
fromIndex = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -118,7 +118,7 @@ public class DefaultDataBuffer implements DataBuffer { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public int lastIndexOf(IntPredicate predicate, int fromIndex) { |
|
|
|
public int lastIndexOf(IntPredicate predicate, int fromIndex) { |
|
|
|
Assert.notNull(predicate, "'predicate' must not be null"); |
|
|
|
Assert.notNull(predicate, "IntPredicate must not be null"); |
|
|
|
int i = Math.min(fromIndex, this.writePosition - 1); |
|
|
|
int i = Math.min(fromIndex, this.writePosition - 1); |
|
|
|
for (; i >= 0; i--) { |
|
|
|
for (; i >= 0; i--) { |
|
|
|
byte b = this.byteBuffer.get(i); |
|
|
|
byte b = this.byteBuffer.get(i); |
|
|
|
@ -149,7 +149,6 @@ public class DefaultDataBuffer implements DataBuffer { |
|
|
|
assertIndex(readPosition >= 0, "'readPosition' %d must be >= 0", readPosition); |
|
|
|
assertIndex(readPosition >= 0, "'readPosition' %d must be >= 0", readPosition); |
|
|
|
assertIndex(readPosition <= this.writePosition, "'readPosition' %d must be <= %d", |
|
|
|
assertIndex(readPosition <= this.writePosition, "'readPosition' %d must be <= %d", |
|
|
|
readPosition, this.writePosition); |
|
|
|
readPosition, this.writePosition); |
|
|
|
|
|
|
|
|
|
|
|
this.readPosition = readPosition; |
|
|
|
this.readPosition = readPosition; |
|
|
|
return this; |
|
|
|
return this; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -165,7 +164,6 @@ public class DefaultDataBuffer implements DataBuffer { |
|
|
|
writePosition, this.readPosition); |
|
|
|
writePosition, this.readPosition); |
|
|
|
assertIndex(writePosition <= this.capacity, "'writePosition' %d must be <= %d", |
|
|
|
assertIndex(writePosition <= this.capacity, "'writePosition' %d must be <= %d", |
|
|
|
writePosition, this.capacity); |
|
|
|
writePosition, this.capacity); |
|
|
|
|
|
|
|
|
|
|
|
this.writePosition = writePosition; |
|
|
|
this.writePosition = writePosition; |
|
|
|
return this; |
|
|
|
return this; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -177,9 +175,9 @@ public class DefaultDataBuffer implements DataBuffer { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public DefaultDataBuffer capacity(int newCapacity) { |
|
|
|
public DefaultDataBuffer capacity(int newCapacity) { |
|
|
|
Assert.isTrue(newCapacity > 0, |
|
|
|
if (newCapacity <= 0) { |
|
|
|
String.format("'newCapacity' %d must be higher than 0", newCapacity)); |
|
|
|
throw new IllegalArgumentException(String.format("'newCapacity' %d must be higher than 0", newCapacity)); |
|
|
|
|
|
|
|
} |
|
|
|
int readPosition = readPosition(); |
|
|
|
int readPosition = readPosition(); |
|
|
|
int writePosition = writePosition(); |
|
|
|
int writePosition = writePosition(); |
|
|
|
int oldCapacity = capacity(); |
|
|
|
int oldCapacity = capacity(); |
|
|
|
@ -225,15 +223,13 @@ public class DefaultDataBuffer implements DataBuffer { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static ByteBuffer allocate(int capacity, boolean direct) { |
|
|
|
private static ByteBuffer allocate(int capacity, boolean direct) { |
|
|
|
return direct ? ByteBuffer.allocateDirect(capacity) : ByteBuffer.allocate(capacity); |
|
|
|
return (direct ? ByteBuffer.allocateDirect(capacity) : ByteBuffer.allocate(capacity)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public byte getByte(int index) { |
|
|
|
public byte getByte(int index) { |
|
|
|
assertIndex(index >= 0, "index %d must be >= 0", index); |
|
|
|
assertIndex(index >= 0, "index %d must be >= 0", index); |
|
|
|
assertIndex(index <= this.writePosition - 1, "index %d must be <= %d", |
|
|
|
assertIndex(index <= this.writePosition - 1, "index %d must be <= %d", index, this.writePosition - 1); |
|
|
|
index, this.writePosition - 1); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return this.byteBuffer.get(index); |
|
|
|
return this.byteBuffer.get(index); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -249,14 +245,14 @@ public class DefaultDataBuffer implements DataBuffer { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public DefaultDataBuffer read(byte[] destination) { |
|
|
|
public DefaultDataBuffer read(byte[] destination) { |
|
|
|
Assert.notNull(destination, "'destination' must not be null"); |
|
|
|
Assert.notNull(destination, "Byte array must not be null"); |
|
|
|
read(destination, 0, destination.length); |
|
|
|
read(destination, 0, destination.length); |
|
|
|
return this; |
|
|
|
return this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public DefaultDataBuffer read(byte[] destination, int offset, int length) { |
|
|
|
public DefaultDataBuffer read(byte[] destination, int offset, int length) { |
|
|
|
Assert.notNull(destination, "'destination' must not be null"); |
|
|
|
Assert.notNull(destination, "Byte array must not be null"); |
|
|
|
assertIndex(this.readPosition <= this.writePosition - length, |
|
|
|
assertIndex(this.readPosition <= this.writePosition - length, |
|
|
|
"readPosition %d and length %d should be smaller than writePosition %d", |
|
|
|
"readPosition %d and length %d should be smaller than writePosition %d", |
|
|
|
this.readPosition, length, this.writePosition); |
|
|
|
this.readPosition, length, this.writePosition); |
|
|
|
@ -281,14 +277,14 @@ public class DefaultDataBuffer implements DataBuffer { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public DefaultDataBuffer write(byte[] source) { |
|
|
|
public DefaultDataBuffer write(byte[] source) { |
|
|
|
Assert.notNull(source, "'source' must not be null"); |
|
|
|
Assert.notNull(source, "Byte array must not be null"); |
|
|
|
write(source, 0, source.length); |
|
|
|
write(source, 0, source.length); |
|
|
|
return this; |
|
|
|
return this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public DefaultDataBuffer write(byte[] source, int offset, int length) { |
|
|
|
public DefaultDataBuffer write(byte[] source, int offset, int length) { |
|
|
|
Assert.notNull(source, "'source' must not be null"); |
|
|
|
Assert.notNull(source, "Byte array must not be null"); |
|
|
|
ensureCapacity(length); |
|
|
|
ensureCapacity(length); |
|
|
|
|
|
|
|
|
|
|
|
ByteBuffer tmp = this.byteBuffer.duplicate(); |
|
|
|
ByteBuffer tmp = this.byteBuffer.duplicate(); |
|
|
|
@ -309,11 +305,12 @@ public class DefaultDataBuffer implements DataBuffer { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public DefaultDataBuffer write(ByteBuffer... byteBuffers) { |
|
|
|
public DefaultDataBuffer write(ByteBuffer... buffers) { |
|
|
|
Assert.notEmpty(byteBuffers, "'byteBuffers' must not be empty"); |
|
|
|
if (!ObjectUtils.isEmpty(buffers)) { |
|
|
|
int capacity = Arrays.stream(byteBuffers).mapToInt(ByteBuffer::remaining).sum(); |
|
|
|
int capacity = Arrays.stream(buffers).mapToInt(ByteBuffer::remaining).sum(); |
|
|
|
ensureCapacity(capacity); |
|
|
|
ensureCapacity(capacity); |
|
|
|
Arrays.stream(byteBuffers).forEach(this::write); |
|
|
|
Arrays.stream(buffers).forEach(this::write); |
|
|
|
|
|
|
|
} |
|
|
|
return this; |
|
|
|
return this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -442,7 +439,7 @@ public class DefaultDataBuffer implements DataBuffer { |
|
|
|
assertIndex(length <= this.capacity, "length %d must be <= %d", index, this.capacity); |
|
|
|
assertIndex(length <= this.capacity, "length %d must be <= %d", index, this.capacity); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static void assertIndex(boolean expression, String format, Object... args) { |
|
|
|
private void assertIndex(boolean expression, String format, Object... args) { |
|
|
|
if (!expression) { |
|
|
|
if (!expression) { |
|
|
|
String message = String.format(format, args); |
|
|
|
String message = String.format(format, args); |
|
|
|
throw new IndexOutOfBoundsException(message); |
|
|
|
throw new IndexOutOfBoundsException(message); |
|
|
|
|