Fixed a problem that symbols were not redownloaded if a 0 size file was created because the target file to download was not available before.

This commit is contained in:
LevelX2 2016-01-22 14:28:46 +01:00
parent ecedc360e9
commit 93ac094470
2 changed files with 76 additions and 53 deletions

View file

@ -3,10 +3,8 @@
*
* Created on 25.08.2010
*/
package org.mage.plugins.card.dl;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
@ -21,7 +19,6 @@ import org.mage.plugins.card.dl.beans.properties.Property;
import org.mage.plugins.card.dl.lm.AbstractLaternaBean;
import org.mage.plugins.card.utils.CardImageUtils;
/**
* The class DownloadJob.
*
@ -29,7 +26,9 @@ import org.mage.plugins.card.utils.CardImageUtils;
* @author Clemens Koza
*/
public class DownloadJob extends AbstractLaternaBean {
public static enum State {
NEW, WORKING, FINISHED, ABORTED;
}
@ -48,7 +47,9 @@ public class DownloadJob extends AbstractLaternaBean {
}
/**
* Sets the job's state. If the state is {@link State#ABORTED}, it instead sets the error to "ABORTED"
* Sets the job's state. If the state is {@link State#ABORTED}, it instead
* sets the error to "ABORTED"
*
* @param state
*/
public void setState(State state) {
@ -60,8 +61,9 @@ public class DownloadJob extends AbstractLaternaBean {
}
/**
* Sets the job's state to {@link State#ABORTED} and the error message to the given message. Logs a warning
* with the given message.
* Sets the job's state to {@link State#ABORTED} and the error message to
* the given message. Logs a warning with the given message.
*
* @param message
*/
public void setError(String message) {
@ -69,8 +71,9 @@ public class DownloadJob extends AbstractLaternaBean {
}
/**
* Sets the job's state to {@link State#ABORTED} and the error to the given exception. Logs a warning with the
* given exception.
* Sets the job's state to {@link State#ABORTED} and the error to the given
* exception. Logs a warning with the given exception.
*
* @param error
*/
public void setError(Exception error) {
@ -78,8 +81,9 @@ public class DownloadJob extends AbstractLaternaBean {
}
/**
* Sets the job's state to {@link State#ABORTED} and the error to the given exception. Logs a warning with the
* given message and exception.
* Sets the job's state to {@link State#ABORTED} and the error to the given
* exception. Logs a warning with the given message and exception.
*
* @param message
* @param error
*/
@ -97,6 +101,7 @@ public class DownloadJob extends AbstractLaternaBean {
/**
* Sets the job's message.
*
* @param message
*/
public void setMessage(String message) {
@ -119,7 +124,6 @@ public class DownloadJob extends AbstractLaternaBean {
return message.getValue();
}
public String getName() {
return name;
}
@ -213,6 +217,14 @@ public class DownloadJob extends AbstractLaternaBean {
return new FileOutputStream(file);
}
@Override
public boolean isValid() throws IOException {
if (file.isFile()) {
return file.length() > 0;
}
return false;
}
@Override
public boolean exists() {
return file.isFile();
@ -228,16 +240,20 @@ public class DownloadJob extends AbstractLaternaBean {
}
public interface Source {
InputStream open() throws IOException;
int length() throws IOException;
}
public interface Destination {
OutputStream open() throws IOException;
boolean exists() throws IOException;
boolean isValid() throws IOException;
void delete() throws IOException;
}
}

View file

@ -3,7 +3,6 @@
*
* Created on 25.08.2010
*/
package org.mage.plugins.card.dl;
import java.io.BufferedInputStream;
@ -29,7 +28,6 @@ import org.mage.plugins.card.dl.DownloadJob.Source;
import org.mage.plugins.card.dl.DownloadJob.State;
import org.mage.plugins.card.dl.lm.AbstractLaternaBean;
/**
* The class Downloader.
*
@ -100,10 +98,12 @@ public class Downloader extends AbstractLaternaBean implements Disposable {
}
/**
* Performs the download job: Transfers data from {@link Source} to {@link Destination} and updates the
* download job's state to reflect the progress.
* Performs the download job: Transfers data from {@link Source} to
* {@link Destination} and updates the download job's state to reflect the
* progress.
*/
private class DownloadCallback implements Callback<DownloadJob> {
@Override
public void onMessage(DownloadJob job) {
//the job won't be processed by multiple threads
@ -118,10 +118,17 @@ public class Downloader extends AbstractLaternaBean implements Disposable {
Destination dst = job.getDestination();
BoundedRangeModel progress = job.getProgress();
if(dst.exists()) {
if (dst.isValid()) {
progress.setMaximum(1);
progress.setValue(1);
} else {
if (dst.exists()) {
try {
dst.delete();
} catch (IOException ex1) {
logger.warn("While deleting not valid file", ex1);
}
}
progress.setMaximum(src.length());
InputStream is = new BufferedInputStream(src.open());
try {