1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-09-10 18:59:41 +02:00

File transfer progress was not being rounded correctly. SMACK-127

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@3598 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Alex Wenckus 2006-03-20 17:59:01 +00:00 committed by alex
parent 3709f51db0
commit 422317e238
4 changed files with 81 additions and 62 deletions

View file

@ -28,9 +28,9 @@ import org.jivesoftware.smack.XMPPException;
/**
* Contains the generic file information and progress related to a particular
* file transfer.
*
*
* @author Alexander Wenckus
*
*
*/
public abstract class FileTransfer {
@ -44,6 +44,8 @@ public abstract class FileTransfer {
private org.jivesoftware.smackx.filetransfer.FileTransfer.Status status;
private final Object statusMonitor = new Object();
protected FileTransferNegotiator negotiator;
protected String streamID;
@ -74,7 +76,7 @@ public abstract class FileTransfer {
/**
* Returns the size of the file being transfered.
*
*
* @return Returns the size of the file being transfered.
*/
public long getFileSize() {
@ -83,7 +85,7 @@ public abstract class FileTransfer {
/**
* Returns the name of the file being transfered.
*
*
* @return Returns the name of the file being transfered.
*/
public String getFileName() {
@ -92,7 +94,7 @@ public abstract class FileTransfer {
/**
* Returns the local path of the file.
*
*
* @return Returns the local path of the file.
*/
public String getFilePath() {
@ -101,7 +103,7 @@ public abstract class FileTransfer {
/**
* Returns the JID of the peer for this file transfer.
*
*
* @return Returns the JID of the peer for this file transfer.
*/
public String getPeer() {
@ -110,21 +112,21 @@ public abstract class FileTransfer {
/**
* Returns the progress of the file transfer as a number between 0 and 1.
*
*
* @return Returns the progress of the file transfer as a number between 0
* and 1.
*/
public double getProgress() {
if(amountWritten == 0) {
if (amountWritten <= 0 || fileSize <= 0) {
return 0;
}
return amountWritten / fileSize;
return (double) amountWritten / (double) fileSize;
}
/**
* Returns true if the transfer has been cancled, if it has stopped because
* of a an error, or the transfer completed succesfully.
*
*
* @return Returns true if the transfer has been cancled, if it has stopped
* because of a an error, or the transfer completed succesfully.
*/
@ -135,7 +137,7 @@ public abstract class FileTransfer {
/**
* Retuns the current status of the file transfer.
*
*
* @return Retuns the current status of the file transfer.
*/
public Status getStatus() {
@ -150,7 +152,7 @@ public abstract class FileTransfer {
* When {@link #getStatus()} returns that there was an {@link Status#ERROR}
* during the transfer, the type of error can be retrieved through this
* method.
*
*
* @return Returns the type of error that occured if one has occured.
*/
public Error getError() {
@ -160,7 +162,7 @@ public abstract class FileTransfer {
/**
* If an exception occurs asynchronously it will be stored for later
* retrival. If there is an error there maybe an exception set.
*
*
* @return The exception that occured or null if there was no exception.
* @see #getError()
*/
@ -178,13 +180,25 @@ public abstract class FileTransfer {
}
protected final void setStatus(Status status) {
this.status = status;
}
synchronized (statusMonitor) {
this.status = status;
}
}
protected final boolean updateStatus(Status oldStatus, Status newStatus) {
synchronized (statusMonitor) {
if (oldStatus != status) {
return false;
}
status = newStatus;
return true;
}
}
protected void writeToStream(final InputStream in, final OutputStream out)
throws XMPPException {
final byte[] b = new byte[1000];
int count = 0;
int count;
amountWritten = 0;
try {
count = in.read(b);
@ -217,30 +231,37 @@ public abstract class FileTransfer {
// equal
if (!getStatus().equals(Status.CANCLED) && getError() == Error.NONE
&& amountWritten != fileSize) {
setStatus(Status.ERROR);
this.error = Error.CONNECTION;
}
}
/**
* A class to represent the current status of the file transfer.
*
*
* @author Alexander Wenckus
*
*
*/
public static class Status {
/**
* An error occured during the transfer.
*
*
* @see FileTransfer#getError()
*/
public static final Status ERROR = new Status();
/**
* The initial status of the file transfer.
*/
public static final Status INITIAL = new Status();
/**
* The file transfer is being negotiated with the peer. The party
* recieving the file has the option to accept or refuse a file transfer
* request. If they accept, then the process of stream negotiation will
* begin. If they refuse the file will not be transfered.
*
*
* @see #NEGOTIATING_STREAM
*/
public static final Status NEGOTIATING_TRANSFER = new Status();
@ -255,7 +276,7 @@ public abstract class FileTransfer {
* The stream to transfer the file is being negotiated over the chosen
* stream type. After the stream negotiating process is complete the
* status becomes negotiated.
*
*
* @see #NEGOTIATED
*/
public static final Status NEGOTIATING_STREAM = new Status();
@ -269,7 +290,7 @@ public abstract class FileTransfer {
/**
* The transfer is in progress.
*
*
* @see FileTransfer#getProgress()
*/
public static final Status IN_PROGRESS = new Status();
@ -338,7 +359,7 @@ public abstract class FileTransfer {
/**
* Returns a String representation of this error.
*
*
* @return Returns a String representation of this error.
*/
public String getMessage() {