private String putData(InputStream srcStream, String remoteFile,
boolean append)
throws IOException, FTPException {
IOException storedEx = null;
BufferedInputStream in = null;
BufferedOutputStream out = null;
long size = 0;
try {
in = new BufferedInputStream(srcStream);
remoteFile = initPut(remoteFile, append);
// get an output stream
out = new BufferedOutputStream(
new DataOutputStream(getOutputStream()), transferBufferSize*2);
// if resuming, we skip over the unwanted bytes
if (resume && resumeMarker > 0) {
in.skip(resumeMarker);
}
else
resumeMarker = 0;
byte[] buf = new byte[transferBufferSize];
byte[] prevBuf = new byte[FTP_LINE_SEPARATOR.length];
int matchpos = 0;
// read a chunk at a time and write to the data socket
long monitorCount = 0;
int count = 0;
boolean isASCII = getType() == FTPTransferType.ASCII;
long start = System.currentTimeMillis();
if (throttler != null) {
throttler.reset();
}
while ((count = in.read(buf)) > 0 && !cancelTransfer) {
if (isASCII) { // we want to allow \r\n, \r and \n
for (int i = 0; i < count; i++) {
// LF without preceding CR (i.e. Unix text file)
if (buf[i] == LINE_FEED && matchpos == 0) {
out.write(CARRIAGE_RETURN);
out.write(LINE_FEED);
size += 2;
monitorCount += 2;
}
else if (buf[i] == FTP_LINE_SEPARATOR[matchpos]) {
prevBuf[matchpos] = buf[i];
matchpos++;
if (matchpos == FTP_LINE_SEPARATOR.length) {
out.write(CARRIAGE_RETURN);
out.write(LINE_FEED);
size += 2;
monitorCount += 2;
matchpos = 0;
}
}
else { // no match current char
// this must be a matching \r if we matched first char
if (matchpos > 0) {
out.write(CARRIAGE_RETURN);
out.write(LINE_FEED);
size += 2;
monitorCount += 2;
}
out.write(buf[i]);
size++;
monitorCount++;
matchpos = 0;
}
}
}
else { // binary
out.write(buf, 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
// - must be a \r which we convert into a line terminator
if (isASCII && matchpos > 0) {
out.write(CARRIAGE_RETURN);
out.write(LINE_FEED);
size += 2;
monitorCount += 2;
}
}
catch (IOException ex) {
storedEx = ex;
log.error("Caught and rethrowing exception in getDataAfterInitGet()", ex);
}
finally {
resume = false;
try {
if (in != null)
in.close();
}
catch (IOException ex) {
log.warn("Caught exception closing input stream", ex);
}