*/
private void getDataAfterInitGet(OutputStream destStream)
throws IOException, FTPException {
// create the buffered output stream for writing the file
BufferedOutputStream out =
new BufferedOutputStream(destStream);
BufferedInputStream in = null;
long size = 0;
IOException storedEx = null;
try {
// get an input stream to read data from ... AFTER we have
// the ok to go ahead AND AFTER we've successfully opened a
// stream for the local file
in = new BufferedInputStream(
new DataInputStream(getInputStream()));
// do the retrieving
long monitorCount = 0;
byte [] chunk = new byte[transferBufferSize];
int count;
boolean isASCII = getType() == FTPTransferType.ASCII;
long start = System.currentTimeMillis();
if (throttler != null) {
throttler.reset();
}
byte[] prevBuf = new byte[FTP_LINE_SEPARATOR.length];
int matchpos = 0;
// read from socket & write to file in chunks
while ((count = readChunk(in, chunk, transferBufferSize)) >= 0 && !cancelTransfer) {
if (isASCII) {
for (int i = 0; i < count; i++) {
if (chunk[i] == FTP_LINE_SEPARATOR[matchpos]) {
prevBuf[matchpos] = chunk[i];
matchpos++;
if (matchpos == FTP_LINE_SEPARATOR.length) {
out.write(LINE_SEPARATOR);
size += LINE_SEPARATOR.length;
monitorCount += LINE_SEPARATOR.length;
matchpos = 0;
}
}
else { // no match
// write out existing matches
if (matchpos > 0) {
out.write(prevBuf, 0, matchpos);
size += matchpos;
monitorCount += matchpos;
}
out.write(chunk[i]);
size++;
monitorCount++;
matchpos = 0;
}
}
}
else { // binary
out.write(chunk, 0, count);
size += count;
monitorCount += count;
}
if (throttler != null) {
throttler.throttleTransfer(size);
}
if (monitor != null && monitorCount > monitorInterval) {
monitor.bytesTransferred(size);
monitorCount = 0;
}
if (serverWakeupInterval > 0 && System.currentTimeMillis() - start > serverWakeupInterval*1000) {
start = System.currentTimeMillis();
sendServerWakeup();
}
}
// write out anything left at the end that has been saved
if (isASCII && matchpos > 0) {
out.write(prevBuf, 0, matchpos);
size += matchpos;
monitorCount += matchpos;
}
}
catch (IOException ex) {
storedEx = ex;
log.error("Caught and rethrowing exception in getDataAfterInitGet()", ex);
}
finally {
try {
if (out != null)
out.close();
}
catch (IOException ex) {
log.warn("Caught exception closing output stream", ex);
}