|
|
|
@ -27,14 +27,13 @@ import java.util.function.IntPredicate; |
|
|
|
import org.springframework.util.Assert; |
|
|
|
import org.springframework.util.Assert; |
|
|
|
import org.springframework.util.ObjectUtils; |
|
|
|
import org.springframework.util.ObjectUtils; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Default implementation of the {@link DataBuffer} interface that uses a |
|
|
|
* Default implementation of the {@link DataBuffer} interface that uses a |
|
|
|
* {@link ByteBuffer} internally. with separate read and write positions. |
|
|
|
* {@link ByteBuffer} internally. with separate read and write positions. |
|
|
|
* Constructed using the {@link DefaultDataBufferFactory}. |
|
|
|
* Constructed using the {@link DefaultDataBufferFactory}. |
|
|
|
* |
|
|
|
* |
|
|
|
* <p>Inspired by Netty's {@code ByteBuf}. Introduced so that non-Netty runtimes (i.e. Servlet) |
|
|
|
* <p>Inspired by Netty's {@code ByteBuf}. Introduced so that non-Netty runtimes |
|
|
|
* do not require Netty on the classpath. |
|
|
|
* (i.e. Servlet) do not require Netty on the classpath. |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Arjen Poutsma |
|
|
|
* @author Arjen Poutsma |
|
|
|
* @author Juergen Hoeller |
|
|
|
* @author Juergen Hoeller |
|
|
|
@ -52,32 +51,29 @@ public class DefaultDataBuffer implements DataBuffer { |
|
|
|
|
|
|
|
|
|
|
|
private ByteBuffer byteBuffer; |
|
|
|
private ByteBuffer byteBuffer; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private int capacity; |
|
|
|
|
|
|
|
|
|
|
|
private int readPosition; |
|
|
|
private int readPosition; |
|
|
|
|
|
|
|
|
|
|
|
private int writePosition; |
|
|
|
private int writePosition; |
|
|
|
|
|
|
|
|
|
|
|
private int capacity; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private DefaultDataBuffer(DefaultDataBufferFactory dataBufferFactory, ByteBuffer byteBuffer) { |
|
|
|
private DefaultDataBuffer(DefaultDataBufferFactory dataBufferFactory, ByteBuffer byteBuffer) { |
|
|
|
Assert.notNull(dataBufferFactory, "'dataBufferFactory' must not be null"); |
|
|
|
Assert.notNull(dataBufferFactory, "DefaultDataBufferFactory must not be null"); |
|
|
|
Assert.notNull(byteBuffer, "'byteBuffer' must not be null"); |
|
|
|
Assert.notNull(byteBuffer, "ByteBuffer must not be null"); |
|
|
|
|
|
|
|
|
|
|
|
this.dataBufferFactory = dataBufferFactory; |
|
|
|
this.dataBufferFactory = dataBufferFactory; |
|
|
|
ByteBuffer slice = byteBuffer.slice(); |
|
|
|
ByteBuffer slice = byteBuffer.slice(); |
|
|
|
this.byteBuffer = slice; |
|
|
|
this.byteBuffer = slice; |
|
|
|
this.capacity = slice.remaining(); |
|
|
|
this.capacity = slice.remaining(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static DefaultDataBuffer fromFilledByteBuffer(DefaultDataBufferFactory dataBufferFactory, |
|
|
|
static DefaultDataBuffer fromFilledByteBuffer(DefaultDataBufferFactory dataBufferFactory, ByteBuffer byteBuffer) { |
|
|
|
ByteBuffer byteBuffer) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DefaultDataBuffer dataBuffer = new DefaultDataBuffer(dataBufferFactory, byteBuffer); |
|
|
|
DefaultDataBuffer dataBuffer = new DefaultDataBuffer(dataBufferFactory, byteBuffer); |
|
|
|
dataBuffer.writePosition(byteBuffer.remaining()); |
|
|
|
dataBuffer.writePosition(byteBuffer.remaining()); |
|
|
|
return dataBuffer; |
|
|
|
return dataBuffer; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static DefaultDataBuffer fromEmptyByteBuffer(DefaultDataBufferFactory dataBufferFactory, |
|
|
|
static DefaultDataBuffer fromEmptyByteBuffer(DefaultDataBufferFactory dataBufferFactory, ByteBuffer byteBuffer) { |
|
|
|
ByteBuffer byteBuffer) { |
|
|
|
|
|
|
|
return new DefaultDataBuffer(dataBufferFactory, byteBuffer); |
|
|
|
return new DefaultDataBuffer(dataBufferFactory, byteBuffer); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -95,6 +91,7 @@ public class DefaultDataBuffer implements DataBuffer { |
|
|
|
this.capacity = byteBuffer.remaining(); |
|
|
|
this.capacity = byteBuffer.remaining(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public DefaultDataBufferFactory factory() { |
|
|
|
public DefaultDataBufferFactory factory() { |
|
|
|
return this.dataBufferFactory; |
|
|
|
return this.dataBufferFactory; |
|
|
|
@ -414,17 +411,17 @@ public class DefaultDataBuffer implements DataBuffer { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public boolean equals(Object obj) { |
|
|
|
public boolean equals(Object other) { |
|
|
|
if (this == obj) { |
|
|
|
if (this == other) { |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
if (!(obj instanceof DefaultDataBuffer)) { |
|
|
|
if (!(other instanceof DefaultDataBuffer)) { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
DefaultDataBuffer other = (DefaultDataBuffer) obj; |
|
|
|
DefaultDataBuffer otherBuffer = (DefaultDataBuffer) other; |
|
|
|
return (this.readPosition == other.readPosition && |
|
|
|
return (this.readPosition == otherBuffer.readPosition && |
|
|
|
this.writePosition == other.writePosition && |
|
|
|
this.writePosition == otherBuffer.writePosition && |
|
|
|
this.byteBuffer.equals(other.byteBuffer)); |
|
|
|
this.byteBuffer.equals(otherBuffer.byteBuffer)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
@ -434,10 +431,11 @@ public class DefaultDataBuffer implements DataBuffer { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public String toString() { |
|
|
|
public String toString() { |
|
|
|
return String.format("DefaultDataBuffer (r: %d, w %d, c %d)", this.readPosition, |
|
|
|
return String.format("DefaultDataBuffer (r: %d, w %d, c %d)", |
|
|
|
this.writePosition, this.capacity); |
|
|
|
this.readPosition, this.writePosition, this.capacity); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void checkIndex(int index, int length) { |
|
|
|
private void checkIndex(int index, int length) { |
|
|
|
assertIndex(index >= 0, "index %d must be >= 0", index); |
|
|
|
assertIndex(index >= 0, "index %d must be >= 0", index); |
|
|
|
assertIndex(length >= 0, "length %d must be >= 0", index); |
|
|
|
assertIndex(length >= 0, "length %d must be >= 0", index); |
|
|
|
@ -452,6 +450,7 @@ public class DefaultDataBuffer implements DataBuffer { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private class DefaultDataBufferInputStream extends InputStream { |
|
|
|
private class DefaultDataBufferInputStream extends InputStream { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
@ -479,7 +478,6 @@ public class DefaultDataBuffer implements DataBuffer { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private class DefaultDataBufferOutputStream extends OutputStream { |
|
|
|
private class DefaultDataBufferOutputStream extends OutputStream { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
@ -496,16 +494,14 @@ public class DefaultDataBuffer implements DataBuffer { |
|
|
|
|
|
|
|
|
|
|
|
private static class SlicedDefaultDataBuffer extends DefaultDataBuffer { |
|
|
|
private static class SlicedDefaultDataBuffer extends DefaultDataBuffer { |
|
|
|
|
|
|
|
|
|
|
|
SlicedDefaultDataBuffer(ByteBuffer byteBuffer, DefaultDataBufferFactory dataBufferFactory, |
|
|
|
SlicedDefaultDataBuffer(ByteBuffer byteBuffer, DefaultDataBufferFactory dataBufferFactory, int length) { |
|
|
|
int length) { |
|
|
|
|
|
|
|
super(dataBufferFactory, byteBuffer); |
|
|
|
super(dataBufferFactory, byteBuffer); |
|
|
|
writePosition(length); |
|
|
|
writePosition(length); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public DefaultDataBuffer capacity(int newCapacity) { |
|
|
|
public DefaultDataBuffer capacity(int newCapacity) { |
|
|
|
throw new UnsupportedOperationException( |
|
|
|
throw new UnsupportedOperationException("Changing the capacity of a sliced buffer is not supported"); |
|
|
|
"Changing the capacity of a sliced buffer is not supported"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|