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

View file

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