Search in sources :

Example 1 with FileTransferErrorEvent

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;
            }
        });
    }
}
Also used : FileTransferErrorEvent(me.matoosh.undernet.event.ftp.FileTransferErrorEvent) FileTransferFinishedEvent(me.matoosh.undernet.event.ftp.FileTransferFinishedEvent) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with FileTransferErrorEvent

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;
        }
    }
}
Also used : FileTransferErrorEvent(me.matoosh.undernet.event.ftp.FileTransferErrorEvent) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 FileTransferErrorEvent (me.matoosh.undernet.event.ftp.FileTransferErrorEvent)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 Callable (java.util.concurrent.Callable)1 FileTransferFinishedEvent (me.matoosh.undernet.event.ftp.FileTransferFinishedEvent)1