1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-12-23 13:07:59 +01:00

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; package org.jivesoftware.smackx.filetransfer;
import org.jivesoftware.smack.SmackException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
@ -202,7 +200,7 @@ public abstract class FileTransfer {
} }
protected void writeToStream(final InputStream in, final OutputStream out) protected void writeToStream(final InputStream in, final OutputStream out)
throws SmackException throws IOException
{ {
final byte[] b = new byte[BUFFER_SIZE]; final byte[] b = new byte[BUFFER_SIZE];
int count = 0; int count = 0;
@ -210,23 +208,15 @@ public abstract class FileTransfer {
do { do {
// write to the output stream // write to the output stream
try {
out.write(b, 0, count); out.write(b, 0, count);
} catch (IOException e) {
throw new SmackException("error writing to output stream", e);
}
amountWritten += count; amountWritten += count;
// read more bytes from the input stream // read more bytes from the input stream
try {
count = in.read(b); count = in.read(b);
} catch (IOException e) {
throw new SmackException("error reading from input stream", e);
}
} while (count != -1 && !getStatus().equals(Status.cancelled)); } 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 if (!getStatus().equals(Status.cancelled) && getError() == Error.none
&& amountWritten != fileSize) { && amountWritten != fileSize) {
setStatus(Status.error); setStatus(Status.error);

View file

@ -27,6 +27,8 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask; import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException; import org.jivesoftware.smack.XMPPException.XMPPErrorException;
@ -54,6 +56,8 @@ import org.jivesoftware.smack.XMPPException.XMPPErrorException;
*/ */
public class IncomingFileTransfer extends FileTransfer { public class IncomingFileTransfer extends FileTransfer {
private static final Logger LOGGER = Logger.getLogger(IncomingFileTransfer.class.getName());
private FileTransferRequest recieveRequest; private FileTransferRequest recieveRequest;
private InputStream inputStream; private InputStream inputStream;
@ -104,27 +108,20 @@ public class IncomingFileTransfer extends FileTransfer {
* *
* @param file The location to save the file. * @param file The location to save the file.
* @throws SmackException when the file transfer fails * @throws SmackException when the file transfer fails
* @throws IOException
* @throws IllegalArgumentException This exception is thrown when the the provided file is * @throws IllegalArgumentException This exception is thrown when the the provided file is
* either null, or cannot be written to. * either null, or cannot be written to.
*/ */
public void recieveFile(final File file) throws SmackException { public void recieveFile(final File file) throws SmackException, IOException {
if (file != null) { if (file == null) {
throw new IllegalArgumentException("File cannot be null");
}
if (!file.exists()) { if (!file.exists()) {
try {
file.createNewFile(); file.createNewFile();
} }
catch (IOException e) {
throw new SmackException(
"Could not create file to write too", e);
}
}
if (!file.canWrite()) { if (!file.canWrite()) {
throw new IllegalArgumentException("Cannot write to provided file"); throw new IllegalArgumentException("Cannot write to provided file");
} }
}
else {
throw new IllegalArgumentException("File cannot be null");
}
Thread transferThread = new Thread(new Runnable() { Thread transferThread = new Thread(new Runnable() {
public void run() { public void run() {
@ -143,16 +140,17 @@ public class IncomingFileTransfer extends FileTransfer {
setStatus(Status.in_progress); setStatus(Status.in_progress);
writeToStream(inputStream, outputStream); writeToStream(inputStream, outputStream);
} }
catch (SmackException e) {
setStatus(Status.error);
setError(Error.stream);
setException(e);
}
catch (FileNotFoundException e) { catch (FileNotFoundException e) {
setStatus(Status.error); setStatus(Status.error);
setError(Error.bad_file); setError(Error.bad_file);
setException(e); setException(e);
} }
catch (IOException e) {
setStatus(Status.error);
setError(Error.stream);
setException(e);
}
if (getStatus().equals(Status.in_progress)) { if (getStatus().equals(Status.in_progress)) {
setStatus(Status.complete); setStatus(Status.complete);
@ -160,17 +158,15 @@ public class IncomingFileTransfer extends FileTransfer {
if (inputStream != null) { if (inputStream != null) {
try { try {
inputStream.close(); inputStream.close();
} } catch (IOException e) {
catch (Throwable io) { LOGGER.log(Level.WARNING, "Closing input stream", e);
/* Ignore */
} }
} }
if (outputStream != null) { if (outputStream != null) {
try { try {
outputStream.close(); outputStream.close();
} } catch (IOException e) {
catch (Throwable io) { LOGGER.log(Level.WARNING, "Closing output stream", e);
/* Ignore */
} }
} }
} }

View file

@ -22,6 +22,8 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.IllegalStateChangeException; import org.jivesoftware.smack.SmackException.IllegalStateChangeException;
@ -38,6 +40,7 @@ import org.jivesoftware.smack.packet.XMPPError;
* *
*/ */
public class OutgoingFileTransfer extends FileTransfer { public class OutgoingFileTransfer extends FileTransfer {
private static final Logger LOGGER = Logger.getLogger(OutgoingFileTransfer.class.getName());
private static int RESPONSE_TIMEOUT = 60 * 1000; private static int RESPONSE_TIMEOUT = 60 * 1000;
private NegotiationProgress callback; private NegotiationProgress callback;
@ -247,19 +250,22 @@ public class OutgoingFileTransfer extends FileTransfer {
setStatus(FileTransfer.Status.error); setStatus(FileTransfer.Status.error);
setError(Error.bad_file); setError(Error.bad_file);
setException(e); setException(e);
} catch (SmackException e) { } catch (IOException e) {
setStatus(FileTransfer.Status.error); setStatus(FileTransfer.Status.error);
setException(e); setException(e);
} finally { } finally {
try {
if (inputStream != null) { if (inputStream != null) {
try {
inputStream.close(); inputStream.close();
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Closing input stream", e);
}
} }
outputStream.flush(); try {
outputStream.close(); outputStream.close();
} catch (IOException e) { } catch (IOException e) {
/* Do Nothing */ LOGGER.log(Level.WARNING, "Closing output stream", e);
} }
} }
updateStatus(Status.in_progress, FileTransfer.Status.complete); updateStatus(Status.in_progress, FileTransfer.Status.complete);
@ -310,7 +316,7 @@ public class OutgoingFileTransfer extends FileTransfer {
} }
try { try {
writeToStream(in, outputStream); writeToStream(in, outputStream);
} catch (SmackException e) { } catch (IOException e) {
setStatus(FileTransfer.Status.error); setStatus(FileTransfer.Status.error);
setException(e); setException(e);
} finally { } finally {