From 218dc66cf775c916d50a02f79b9f6d9b8f987d30 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Tue, 26 Aug 2014 15:26:37 +0200 Subject: [PATCH] Filetransfer API should not wrap IOException into SmackException. --- .../smackx/filetransfer/FileTransfer.java | 18 ++----- .../filetransfer/IncomingFileTransfer.java | 50 +++++++++---------- .../filetransfer/OutgoingFileTransfer.java | 24 +++++---- 3 files changed, 42 insertions(+), 50 deletions(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/FileTransfer.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/FileTransfer.java index 1ad5a838e..baed208d0 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/FileTransfer.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/FileTransfer.java @@ -16,8 +16,6 @@ */ package org.jivesoftware.smackx.filetransfer; -import org.jivesoftware.smack.SmackException; - import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -202,7 +200,7 @@ public abstract class FileTransfer { } protected void writeToStream(final InputStream in, final OutputStream out) - throws SmackException + throws IOException { final byte[] b = new byte[BUFFER_SIZE]; int count = 0; @@ -210,23 +208,15 @@ public abstract class FileTransfer { do { // write to the output stream - try { - out.write(b, 0, count); - } catch (IOException e) { - throw new SmackException("error writing to output stream", e); - } + out.write(b, 0, count); amountWritten += count; // read more bytes from the input stream - try { - count = in.read(b); - } catch (IOException e) { - throw new SmackException("error reading from input stream", e); - } + count = in.read(b); } while (count != -1 && !getStatus().equals(Status.cancelled)); - // the connection was likely terminated abrubtly if these are not equal + // the connection was likely terminated abruptly if these are not equal if (!getStatus().equals(Status.cancelled) && getError() == Error.none && amountWritten != fileSize) { setStatus(Status.error); diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/IncomingFileTransfer.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/IncomingFileTransfer.java index 7fd8a1774..72b843936 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/IncomingFileTransfer.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/IncomingFileTransfer.java @@ -27,6 +27,8 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import java.util.logging.Level; +import java.util.logging.Logger; import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.XMPPException.XMPPErrorException; @@ -54,6 +56,8 @@ import org.jivesoftware.smack.XMPPException.XMPPErrorException; */ public class IncomingFileTransfer extends FileTransfer { + private static final Logger LOGGER = Logger.getLogger(IncomingFileTransfer.class.getName()); + private FileTransferRequest recieveRequest; private InputStream inputStream; @@ -104,27 +108,20 @@ public class IncomingFileTransfer extends FileTransfer { * * @param file The location to save the file. * @throws SmackException when the file transfer fails + * @throws IOException * @throws IllegalArgumentException This exception is thrown when the the provided file is * either null, or cannot be written to. */ - public void recieveFile(final File file) throws SmackException { - if (file != null) { - if (!file.exists()) { - try { - file.createNewFile(); - } - catch (IOException e) { - throw new SmackException( - "Could not create file to write too", e); - } - } - if (!file.canWrite()) { - throw new IllegalArgumentException("Cannot write to provided file"); - } - } - else { + public void recieveFile(final File file) throws SmackException, IOException { + if (file == null) { throw new IllegalArgumentException("File cannot be null"); } + if (!file.exists()) { + file.createNewFile(); + } + if (!file.canWrite()) { + throw new IllegalArgumentException("Cannot write to provided file"); + } Thread transferThread = new Thread(new Runnable() { public void run() { @@ -143,16 +140,17 @@ public class IncomingFileTransfer extends FileTransfer { setStatus(Status.in_progress); writeToStream(inputStream, outputStream); } - catch (SmackException e) { - setStatus(Status.error); - setError(Error.stream); - setException(e); - } catch (FileNotFoundException e) { setStatus(Status.error); setError(Error.bad_file); setException(e); } + catch (IOException e) { + setStatus(Status.error); + setError(Error.stream); + setException(e); + } + if (getStatus().equals(Status.in_progress)) { setStatus(Status.complete); @@ -160,17 +158,15 @@ public class IncomingFileTransfer extends FileTransfer { if (inputStream != null) { try { inputStream.close(); - } - catch (Throwable io) { - /* Ignore */ + } catch (IOException e) { + LOGGER.log(Level.WARNING, "Closing input stream", e); } } if (outputStream != null) { try { outputStream.close(); - } - catch (Throwable io) { - /* Ignore */ + } catch (IOException e) { + LOGGER.log(Level.WARNING, "Closing output stream", e); } } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/OutgoingFileTransfer.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/OutgoingFileTransfer.java index b66e56a38..bdf7a439b 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/OutgoingFileTransfer.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/OutgoingFileTransfer.java @@ -22,6 +22,8 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.logging.Level; +import java.util.logging.Logger; import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException.IllegalStateChangeException; @@ -38,6 +40,7 @@ import org.jivesoftware.smack.packet.XMPPError; * */ public class OutgoingFileTransfer extends FileTransfer { + private static final Logger LOGGER = Logger.getLogger(OutgoingFileTransfer.class.getName()); private static int RESPONSE_TIMEOUT = 60 * 1000; private NegotiationProgress callback; @@ -247,20 +250,23 @@ public class OutgoingFileTransfer extends FileTransfer { setStatus(FileTransfer.Status.error); setError(Error.bad_file); setException(e); - } catch (SmackException e) { + } catch (IOException e) { setStatus(FileTransfer.Status.error); setException(e); } finally { - try { if (inputStream != null) { - inputStream.close(); + try { + inputStream.close(); + } catch (IOException e) { + LOGGER.log(Level.WARNING, "Closing input stream", e); + } } - outputStream.flush(); - outputStream.close(); - } catch (IOException e) { - /* Do Nothing */ - } + try { + outputStream.close(); + } catch (IOException e) { + LOGGER.log(Level.WARNING, "Closing output stream", e); + } } updateStatus(Status.in_progress, FileTransfer.Status.complete); } @@ -310,7 +316,7 @@ public class OutgoingFileTransfer extends FileTransfer { } try { writeToStream(in, outputStream); - } catch (SmackException e) { + } catch (IOException e) { setStatus(FileTransfer.Status.error); setException(e); } finally {