package org.apache.tomcat.util.net;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import org.apache.tomcat.util.buf.ByteBufferUtils;
public class SocketBufferHandler {
static SocketBufferHandler EMPTY = new SocketBufferHandler(0, 0, false) {
@Override
public void expand(int newSize) {
}
@Override
public void unReadReadBuffer(ByteBuffer returnedData) {
}
};
private volatile boolean readBufferConfiguredForWrite = true;
private volatile ByteBuffer readBuffer;
private volatile boolean writeBufferConfiguredForWrite = true;
private volatile ByteBuffer writeBuffer;
private final boolean direct;
public SocketBufferHandler(int readBufferSize, int writeBufferSize,
boolean direct) {
this.direct = direct;
if (direct) {
readBuffer = ByteBuffer.allocateDirect(readBufferSize);
writeBuffer = ByteBuffer.allocateDirect(writeBufferSize);
} else {
readBuffer = ByteBuffer.allocate(readBufferSize);
writeBuffer = ByteBuffer.allocate(writeBufferSize);
}
}
public void configureReadBufferForWrite() {
setReadBufferConfiguredForWrite(true);
}
public void configureReadBufferForRead() {
setReadBufferConfiguredForWrite(false);
}
private void setReadBufferConfiguredForWrite(boolean readBufferConFiguredForWrite) {
if (this.readBufferConfiguredForWrite != readBufferConFiguredForWrite) {
if (readBufferConFiguredForWrite) {
int remaining = readBuffer.remaining();
if (remaining == 0) {
readBuffer.clear();
} else {
readBuffer.compact();
}
} else {
readBuffer.flip();
}
this.readBufferConfiguredForWrite = readBufferConFiguredForWrite;
}
}
public ByteBuffer getReadBuffer() {
return readBuffer;
}
public boolean isReadBufferEmpty() {
if (readBufferConfiguredForWrite) {
return readBuffer.position() == 0;
} else {
return readBuffer.remaining() == 0;
}
}
public void unReadReadBuffer(ByteBuffer returnedData) {
if (isReadBufferEmpty()) {
configureReadBufferForWrite();
readBuffer.put(returnedData);
} else {
int bytesReturned = returnedData.remaining();
if (readBufferConfiguredForWrite) {
if ((readBuffer.position() + bytesReturned) > readBuffer.capacity()) {
throw new BufferOverflowException();
} else {
for (int i = 0; i < readBuffer.position(); i++) {
readBuffer.put(i + bytesReturned, readBuffer.get(i));
}
for (int i = 0; i < bytesReturned; i++) {
readBuffer.put(i, returnedData.get());
}
readBuffer.position(readBuffer.position() + bytesReturned);
}
} else {
int shiftRequired = bytesReturned - readBuffer.position();
if (shiftRequired > 0) {
if ((readBuffer.capacity() - readBuffer.limit()) < shiftRequired) {
throw new BufferOverflowException();
}
int oldLimit = readBuffer.limit();
readBuffer.limit(oldLimit + shiftRequired);
for (int i = readBuffer.position(); i < oldLimit; i++) {
readBuffer.put(i + shiftRequired, readBuffer.get(i));
}
} else {
shiftRequired = 0;
}
int insertOffset = readBuffer.position() + shiftRequired - bytesReturned;
for (int i = insertOffset; i < bytesReturned + insertOffset; i++) {
readBuffer.put(i, returnedData.get());
}
readBuffer.position(insertOffset);
}
}
}
public void configureWriteBufferForWrite() {
setWriteBufferConfiguredForWrite(true);
}
public void configureWriteBufferForRead() {
setWriteBufferConfiguredForWrite(false);
}
private void setWriteBufferConfiguredForWrite(boolean writeBufferConfiguredForWrite) {
if (this.writeBufferConfiguredForWrite != writeBufferConfiguredForWrite) {
if (writeBufferConfiguredForWrite) {
int remaining = writeBuffer.remaining();
if (remaining == 0) {
writeBuffer.clear();
} else {
writeBuffer.compact();
writeBuffer.position(remaining);
writeBuffer.limit(writeBuffer.capacity());
}
} else {
writeBuffer.flip();
}
this.writeBufferConfiguredForWrite = writeBufferConfiguredForWrite;
}
}
public boolean isWriteBufferWritable() {
if (writeBufferConfiguredForWrite) {
return writeBuffer.hasRemaining();
} else {
return writeBuffer.remaining() == 0;
}
}
public ByteBuffer getWriteBuffer() {
return writeBuffer;
}
public boolean isWriteBufferEmpty() {
if (writeBufferConfiguredForWrite) {
return writeBuffer.position() == 0;
} else {
return writeBuffer.remaining() == 0;
}
}
public void reset() {
readBuffer.clear();
readBufferConfiguredForWrite = true;
writeBuffer.clear();
writeBufferConfiguredForWrite = true;
}
public void expand(int newSize) {
configureReadBufferForWrite();
readBuffer = ByteBufferUtils.expand(readBuffer, newSize);
configureWriteBufferForWrite();
writeBuffer = ByteBufferUtils.expand(writeBuffer, newSize);
}
public void free() {
if (direct) {
ByteBufferUtils.cleanDirectBuffer(readBuffer);
ByteBufferUtils.cleanDirectBuffer(writeBuffer);
}
}
}