From 93ac09447084fa343dc76c526f65990562073271 Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Fri, 22 Jan 2016 14:28:46 +0100 Subject: [PATCH] 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. --- .../org/mage/plugins/card/dl/DownloadJob.java | 68 ++++++++++++------- .../org/mage/plugins/card/dl/Downloader.java | 61 +++++++++-------- 2 files changed, 76 insertions(+), 53 deletions(-) diff --git a/Mage.Client/src/main/java/org/mage/plugins/card/dl/DownloadJob.java b/Mage.Client/src/main/java/org/mage/plugins/card/dl/DownloadJob.java index 42b67cf7a5..32462dfa60 100644 --- a/Mage.Client/src/main/java/org/mage/plugins/card/dl/DownloadJob.java +++ b/Mage.Client/src/main/java/org/mage/plugins/card/dl/DownloadJob.java @@ -1,12 +1,10 @@ /** * DownloadJob.java - * + * * Created on 25.08.2010 */ - package org.mage.plugins.card.dl; - import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -21,25 +19,26 @@ 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. - * + * * @version V0.0 25.08.2010 * @author Clemens Koza */ public class DownloadJob extends AbstractLaternaBean { + public static enum State { + NEW, WORKING, FINISHED, ABORTED; } - private final String name; - private final Source source; - private final Destination destination; - private final Property state = properties.property("state", State.NEW); - private final Property message = properties.property("message"); - private final Property error = properties.property("error"); - private final BoundedRangeModel progress = new DefaultBoundedRangeModel(); + private final String name; + private final Source source; + private final Destination destination; + private final Property state = properties.property("state", State.NEW); + private final Property message = properties.property("message"); + private final Property error = properties.property("error"); + private final BoundedRangeModel progress = new DefaultBoundedRangeModel(); public DownloadJob(String name, Source source, Destination destination) { this.name = name; @@ -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,14 +81,15 @@ 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 */ public void setError(String message, Exception error) { if (message == null) { - + message = "Download of " + this.getName() + "from " + this.getSource().toString() + " caused error: " + error.toString(); } // log.warn(message, 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; } @@ -163,9 +167,9 @@ public class DownloadJob extends AbstractLaternaBean { @Override public String toString() { - return proxy != null ? proxy.type().toString()+" " :"" + url; + return proxy != null ? proxy.type().toString() + " " : "" + url; } - + }; } @@ -189,11 +193,11 @@ public class DownloadJob extends AbstractLaternaBean { public int length() throws IOException { return getConnection().getContentLength(); } - + @Override public String toString() { - return proxy != null ? proxy.type().toString()+" " :"" + url; - } + return proxy != null ? proxy.type().toString() + " " : "" + url; + } }; } @@ -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; } } diff --git a/Mage.Client/src/main/java/org/mage/plugins/card/dl/Downloader.java b/Mage.Client/src/main/java/org/mage/plugins/card/dl/Downloader.java index 1ff78368e1..ca5160a71d 100644 --- a/Mage.Client/src/main/java/org/mage/plugins/card/dl/Downloader.java +++ b/Mage.Client/src/main/java/org/mage/plugins/card/dl/Downloader.java @@ -1,9 +1,8 @@ /** * Downloader.java - * + * * Created on 25.08.2010 */ - package org.mage.plugins.card.dl; import java.io.BufferedInputStream; @@ -29,10 +28,9 @@ 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. - * + * * @version V0.0 25.08.2010 * @author Clemens Koza */ @@ -40,16 +38,16 @@ public class Downloader extends AbstractLaternaBean implements Disposable { private static final Logger logger = Logger.getLogger(Downloader.class); - private final List jobs = properties.list("jobs"); + private final List jobs = properties.list("jobs"); private final Channel channel = new MemoryChannel<>(); - private final ExecutorService pool = Executors.newCachedThreadPool(); - private final List fibers = new ArrayList<>(); + private final ExecutorService pool = Executors.newCachedThreadPool(); + private final List fibers = new ArrayList<>(); public Downloader() { PoolFiberFactory f = new PoolFiberFactory(pool); //subscribe multiple fibers for parallel execution - for(int i = 0, numThreads = 10; i < numThreads; i++) { + for (int i = 0, numThreads = 10; i < numThreads; i++) { Fiber fiber = f.create(); fiber.start(); fibers.add(fiber); @@ -59,15 +57,15 @@ public class Downloader extends AbstractLaternaBean implements Disposable { @Override public void dispose() { - for(DownloadJob j:getJobs()) { - switch(j.getState()) { + for (DownloadJob j : getJobs()) { + switch (j.getState()) { case NEW: case WORKING: j.setState(State.ABORTED); } } - for(Fiber f:fibers) { + for (Fiber f : fibers) { f.dispose(); } pool.shutdown(); @@ -84,10 +82,10 @@ public class Downloader extends AbstractLaternaBean implements Disposable { } public void add(DownloadJob job) { - if(job.getState() == State.WORKING) { + if (job.getState() == State.WORKING) { throw new IllegalArgumentException("Job already running"); } - if(job.getState() == State.FINISHED) { + if (job.getState() == State.FINISHED) { throw new IllegalArgumentException("Job already finished"); } job.setState(State.NEW); @@ -100,15 +98,17 @@ 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 { + @Override public void onMessage(DownloadJob job) { //the job won't be processed by multiple threads - synchronized(job) { - if(job.getState() != State.NEW) { + synchronized (job) { + if (job.getState() != State.NEW) { return; } job.setState(State.WORKING); @@ -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 { @@ -129,45 +136,45 @@ public class Downloader extends AbstractLaternaBean implements Disposable { try { byte[] buf = new byte[8 * 1024]; int total = 0; - for(int len; (len = is.read(buf)) != -1;) { - if(job.getState() == State.ABORTED) { + for (int len; (len = is.read(buf)) != -1;) { + if (job.getState() == State.ABORTED) { throw new IOException("Job was aborted"); } progress.setValue(total += len); os.write(buf, 0, len); } - } catch(IOException ex) { + } catch (IOException ex) { try { dst.delete(); - } catch(IOException ex1) { + } catch (IOException ex1) { logger.warn("While deleting", ex1); } throw ex; } finally { try { os.close(); - } catch(IOException ex) { + } catch (IOException ex) { logger.warn("While closing", ex); } } } finally { try { is.close(); - } catch(IOException ex) { + } catch (IOException ex) { logger.warn("While closing", ex); } } } job.setState(State.FINISHED); - } catch(ConnectException ex) { + } catch (ConnectException ex) { String message; if (ex.getMessage() != null) { message = ex.getMessage(); } else { message = "Unknown error"; } - logger.warn("Error resource download " + job.getName() +" from "+ job.getSource().toString() + ": " + message); - } catch(IOException ex) { + logger.warn("Error resource download " + job.getName() + " from " + job.getSource().toString() + ": " + message); + } catch (IOException ex) { job.setError(ex); } }