IOUtils.closeQuietly(channel);
NetUtils.closeQuietly(socket);
throw e;
}
DataInputStream din = null;
if(sync) {
InputStream in = socket.getInputStream();
din = new DataInputStream(in);
}
final DataOutputStream dos = new DataOutputStream(out);
final StopWatch sw = new StopWatch();
FileInputStream src = null;
final long nbytes;
try {
src = new FileInputStream(file);
FileChannel fc = src.getChannel();
String fileName = file.getName();
IOUtils.writeString(fileName, dos);
IOUtils.writeString(writeDirPath, dos);
long xferBytes = (count == -1L) ? fc.size() : count;
dos.writeLong(xferBytes);
dos.writeBoolean(append); // append=false
dos.writeBoolean(sync);
if(handler == null) {
dos.writeBoolean(false);
} else {
dos.writeBoolean(true);
handler.writeAdditionalHeader(dos);
}
// send file using zero-copy send
nbytes = fc.transferTo(fromPos, xferBytes, channel);
if(LOG.isDebugEnabled()) {
LOG.debug("Sent a file '" + file.getAbsolutePath() + "' of " + nbytes
+ " bytes to " + dstSockAddr.toString() + " in " + sw.toString());
}
if(sync) {// receive ack in sync mode
long remoteRecieved = din.readLong();
if(remoteRecieved != xferBytes) {
throw new IllegalStateException("Sent " + xferBytes
+ " bytes, but remote node received " + remoteRecieved + " bytes");
}
}