send(data, fileName, writeDirPath, dstAddr, dstPort, append, sync, null);
}
public static void send(@Nonnull final FastByteArrayOutputStream data, @Nonnull final String fileName, @Nullable final String writeDirPath, @Nonnull final InetAddress dstAddr, final int dstPort, final boolean append, final boolean sync, @Nullable final TransferClientHandler handler)
throws IOException {
final SocketAddress dstSockAddr = new InetSocketAddress(dstAddr, dstPort);
SocketChannel channel = null;
Socket socket = null;
final OutputStream out;
try {
channel = SocketChannel.open();
socket = channel.socket();
socket.connect(dstSockAddr);
out = socket.getOutputStream();
} catch (IOException e) {
LOG.error("failed to connect: " + dstSockAddr, e);
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();
try {
IOUtils.writeString(fileName, dos);
IOUtils.writeString(writeDirPath, dos);
long nbytes = data.size();
dos.writeLong(nbytes);
dos.writeBoolean(append);
dos.writeBoolean(sync);
if(handler == null) {
dos.writeBoolean(false);
} else {
dos.writeBoolean(true);
handler.writeAdditionalHeader(dos);
}
// send file using zero-copy send
data.writeTo(out);
if(LOG.isDebugEnabled()) {
LOG.debug("Sent a file data '" + fileName + "' of " + nbytes + " bytes to "
+ dstSockAddr.toString() + " in " + sw.toString());
}
if(sync) {// receive ack in sync mode
long remoteRecieved = din.readLong();
if(remoteRecieved != nbytes) {