| 1 | // Jomic - a viewer for comic book archives. |
| 2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | package net.sf.jomic.tools; |
| 17 | |
| 18 | import java.io.File; |
| 19 | import java.io.FileInputStream; |
| 20 | import java.io.FileOutputStream; |
| 21 | import java.io.IOException; |
| 22 | import java.io.InputStream; |
| 23 | import java.io.OutputStream; |
| 24 | |
| 25 | import org.apache.commons.logging.Log; |
| 26 | import org.apache.commons.logging.LogFactory; |
| 27 | |
| 28 | /** |
| 29 | * Task to copy a file, overwritting a possibly already existing file. If the task fails, an |
| 30 | * attempt is made to remove the possibly incomplete file. |
| 31 | * |
| 32 | * @author Thomas Aglassinger |
| 33 | */ |
| 34 | public class CopyFileTask extends AbstractTask |
| 35 | { |
| 36 | private static final int BUFFER_SIZE = 4096; |
| 37 | private FileTools fileTools; |
| 38 | private Log logger; |
| 39 | private boolean makeDirs; |
| 40 | private File source; |
| 41 | private File target; |
| 42 | |
| 43 | public CopyFileTask(File newSourceFile, File newTargetFile, boolean newMakeDirs) { |
| 44 | super(); |
| 45 | assert newSourceFile != null; |
| 46 | assert newTargetFile != null; |
| 47 | |
| 48 | logger = LogFactory.getLog(CopyFileTask.class); |
| 49 | fileTools = FileTools.instance(); |
| 50 | source = newSourceFile; |
| 51 | target = newTargetFile; |
| 52 | makeDirs = newMakeDirs; |
| 53 | // Make sure that empty files get a maximum progress of 1. |
| 54 | setMaxProgress(Math.max(1, source.length())); |
| 55 | } |
| 56 | |
| 57 | public CopyFileTask(File newSourceFile, File newTargetFile) { |
| 58 | this(newSourceFile, newTargetFile, false); |
| 59 | } |
| 60 | |
| 61 | public void start() |
| 62 | throws IOException { |
| 63 | if (logger.isInfoEnabled()) { |
| 64 | logger.info("copy \"" + source + "\" to \"" + target + "\""); |
| 65 | } |
| 66 | boolean copied = false; |
| 67 | InputStream in = new FileInputStream(source); |
| 68 | |
| 69 | if (makeDirs) { |
| 70 | fileTools.mkdirs(target.getParentFile()); |
| 71 | } |
| 72 | |
| 73 | try { |
| 74 | OutputStream out = new FileOutputStream(target); |
| 75 | |
| 76 | try { |
| 77 | byte[] buffer = new byte[BUFFER_SIZE]; |
| 78 | |
| 79 | setProgress(0); |
| 80 | while (!copied && !isInterrupted()) { |
| 81 | int bytesRead = in.read(buffer); |
| 82 | |
| 83 | if (bytesRead > 0) { |
| 84 | out.write(buffer, 0, bytesRead); |
| 85 | setProgress(getProgress() + bytesRead); |
| 86 | } else { |
| 87 | copied = true; |
| 88 | } |
| 89 | } |
| 90 | } finally { |
| 91 | out.close(); |
| 92 | if (!copied || isInterrupted()) { |
| 93 | fileTools.deleteOrWarn(target, logger); |
| 94 | } else if (getProgress() != getMaxProgress()) { |
| 95 | // Make sure that empty files also end with a progress of 1. |
| 96 | setProgress(getMaxProgress()); |
| 97 | } |
| 98 | } |
| 99 | } finally { |
| 100 | in.close(); |
| 101 | } |
| 102 | } |
| 103 | } |