try {
socket = new Socket(address, syncPort);
LOG.info("sync connected to " + socket.getInetAddress().getHostAddress() + " port " + socket.getLocalPort());
final CRC32 crc32 = new CRC32();
final DataOutput output = new DataOutputStream(new CheckedOutputStream(socket.getOutputStream(), crc32));
final DataInput input = new DataInputStream(socket.getInputStream());
output.writeByte(INIT);
long logId = input.readLong();
do {
final long nextLogId = logId + 1;
final File file = Util.logFile(nextLogId);
if (file.exists() && server.getLogger().isWritten(nextLogId)) {
logId++;
output.writeByte(RECOVERY_LOG);
crc32.reset();
output.writeLong(logId);
LOG.info("sending recovery file: " + file.getName());
final BufferedInputStream fileInput = new BufferedInputStream(new FileInputStream(file));
final byte[] buffer = new byte[8092];
int read;
while ((read = fileInput.read(buffer)) > 0) {
output.writeInt(read);
output.write(buffer, 0, read);
}
output.writeInt(0);
output.writeLong(crc32.getValue());
}
try {
Thread.sleep(300);
} catch (final InterruptedException ignore) {
}