}
private int writeFile(T session, WriteRequest req,
boolean hasFragmentation, int maxLength, long currentTime) throws Exception {
int localWrittenBytes;
FileRegion region = (FileRegion) req.getMessage();
if (region.getRemainingBytes() > 0) {
int length;
if (hasFragmentation) {
length = (int) Math.min(region.getRemainingBytes(), maxLength);
} else {
length = (int) Math.min(Integer.MAX_VALUE, region.getRemainingBytes());
}
localWrittenBytes = transferFile(session, region, length);
region.update(localWrittenBytes);
} else {
localWrittenBytes = 0;
}
session.increaseWrittenBytes(localWrittenBytes, currentTime);
if (region.getRemainingBytes() <= 0 ||
!hasFragmentation && localWrittenBytes != 0) {
fireMessageSent(session, req);
}
return localWrittenBytes;