use of me.matoosh.undernet.event.ftp.FileTransferErrorEvent in project UnderNet by itsMatoosh.
the class FileTransfer method startSending.
/**
* Starts the file transfer.
*/
public void startSending() {
if (fileTransferType.equals(FileTransferType.OUTBOUND)) {
// File sending logic.
UnderNet.router.fileTransferManager.executor.submit(new Callable() {
@Override
public Object call() throws Exception {
// Amount of bytes read from the send stream.
int totalRead = 0;
try {
// The send buffer.
byte[] buffer = new byte[BUFFER_SIZE];
int read = 0;
while ((read = inputStream.read(buffer)) > 0) {
totalRead += read;
byte[] data = new byte[read];
System.arraycopy(buffer, 0, data, 0, read);
sendChunk(data);
logger.debug("Chunk sent " + totalRead + "/" + fileInfo.fileLength);
}
} catch (IOException e) {
FileTransferManager.logger.error("Error reading " + BUFFER_SIZE + " chunk from file: " + file, e);
EventManager.callEvent(new FileTransferErrorEvent(FileTransfer.this, e));
} finally {
// File sent or error.
EventManager.callEvent(new FileTransferFinishedEvent(FileTransfer.this));
}
return null;
}
});
}
}
use of me.matoosh.undernet.event.ftp.FileTransferErrorEvent in project UnderNet by itsMatoosh.
the class FileTransfer method prepareStreams.
/**
* Prepares the file streams for this trasfer.
*/
private void prepareStreams() {
// Caching the path of the file.
file = new File(UnderNet.fileManager.getContentFolder() + "/" + fileInfo.fileName);
if (fileTransferType == FileTransferType.OUTBOUND) {
try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
// File doesn't exist.
// Calling a transfer error.
FileTransferManager.logger.error("Couldn't find file: " + file, new FileNotFoundException(file.toString()));
EventManager.callEvent(new FileTransferErrorEvent(this, new FileNotFoundException(file.toString())));
return;
}
} else {
// Creating or replacing the file.
if (file.exists()) {
file.delete();
}
try {
// Creating new file.
file.createNewFile();
outputStream = new FileOutputStream(file);
} catch (IOException e) {
// Calling a transfer error.
FileTransferManager.logger.error("Couldn't create file: " + file, e);
EventManager.callEvent(new FileTransferErrorEvent(this, e));
return;
}
}
}
Aggregations