final long blockSize = session.getBlockSize();
int totalBlocks = (int) Math.ceil(length / (double) blockSize);
long bytes2Process = length;
int bufferPosition = 0;
final Connection connection = session.getNextFreeConnection();
connection.getSession().addOutstandingTask(connection, task);
// first stage
short blocks = (short) Math.min(WRITE_FIRST_STAGE_BLOCKS, totalBlocks);
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Now sending sequences of length " + blocks + " blocks.");
}
int expectedDataTransferLength = (int) Math.min(bytes2Process, blocks * blockSize);
connection.nextState(new WriteRequestState(connection, src, bufferPosition, TaskAttributes.SIMPLE, expectedDataTransferLength, startAddress, blocks));
startAddress += blocks;
totalBlocks -= blocks;
bytes2Process -= blocks * blockSize;
bufferPosition += expectedDataTransferLength;
// second stage
blocks = (short) Math.min(WRITE_SECOND_STAGE_BLOCKS, totalBlocks);
if (blocks > 0) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Now sending sequences of length " + blocks + " blocks.");
LOGGER.info("Remaining, DataSegmentLength: " + bytes2Process + ", " + expectedDataTransferLength);
}
expectedDataTransferLength = (int) Math.min(bytes2Process, blocks * blockSize);
connection.nextState(new WriteRequestState(connection, src, bufferPosition, TaskAttributes.SIMPLE, expectedDataTransferLength, startAddress, blocks));
startAddress += blocks;
totalBlocks -= blocks;
bytes2Process -= blocks * blockSize;
bufferPosition += expectedDataTransferLength;
}
// third stage
blocks = (short) Math.min(WRITE_THIRD_STAGE_BLOCKS, totalBlocks);
while (blocks > 0) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Now sending sequences of length " + blocks + " blocks.");
}
expectedDataTransferLength = (int) Math.min(bytes2Process, blocks * blockSize);
connection.nextState(new WriteRequestState(connection, src, bufferPosition, TaskAttributes.SIMPLE, expectedDataTransferLength, startAddress, blocks));
startAddress += blocks;
totalBlocks -= blocks;
blocks = (short) Math.min(READ_THIRD_STAGE_BLOCKS, totalBlocks);
bufferPosition += expectedDataTransferLength;
}