Filetransfer API should not wrap IOException

into SmackException.
This commit is contained in:
Florian Schmaus 2014-08-26 15:26:37 +02:00
parent 6af5d5e462
commit 218dc66cf7
3 changed files with 42 additions and 50 deletions

View File

@ -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);

View File

@ -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);
}
}
}

View File

@ -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 {