EMMA Coverage Report (generated Sun Apr 20 22:38:01 CEST 2008)
[all classes][net.sf.jomic.tools]

COVERAGE SUMMARY FOR SOURCE FILE [CopyFileTask.java]

nameclass, %method, %block, %line, %
CopyFileTask.java100% (1/1)100% (4/4)69%  (149/215)84%  (31.1/37)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CopyFileTask100% (1/1)100% (4/4)69%  (149/215)84%  (31.1/37)
start (): void 100% (1/1)66%  (95/145)81%  (19.4/24)
CopyFileTask (File, File, boolean): void 100% (1/1)73%  (36/49)89%  (8.9/10)
<static initializer> 100% (1/1)80%  (12/15)80%  (0.8/1)
CopyFileTask (File, File): void 100% (1/1)100% (6/6)100% (2/2)

1// Jomic - a viewer for comic book archives.
2// Copyright (C) 2004-2008 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/>.
16package net.sf.jomic.tools;
17 
18import java.io.File;
19import java.io.FileInputStream;
20import java.io.FileOutputStream;
21import java.io.IOException;
22import java.io.InputStream;
23import java.io.OutputStream;
24 
25import net.sf.wraplog.Logger;
26 
27/**
28 *  Task to copy a file, overwritting a possibly already existing file. If the task fails, an
29 *  attempt is made to remove the possibly incomplete file.
30 *
31 * @author    Thomas Aglassinger
32 */
33public class CopyFileTask extends AbstractTask
34{
35    private static final int BUFFER_SIZE = 4096;
36    private FileTools fileTools;
37    private Logger logger;
38    private boolean makeDirs;
39    private File source;
40    private File target;
41 
42    public CopyFileTask(File newSourceFile, File newTargetFile, boolean newMakeDirs) {
43        super();
44        assert newSourceFile != null;
45        assert newTargetFile != null;
46 
47        logger = Logger.getLogger(CopyFileTask.class);
48        fileTools = FileTools.instance();
49        source = newSourceFile;
50        target = newTargetFile;
51        makeDirs = newMakeDirs;
52        // Make sure that empty files get a maximum progress of 1.
53        setMaxProgress(Math.max(1, source.length()));
54    }
55 
56    public CopyFileTask(File newSourceFile, File newTargetFile) {
57        this(newSourceFile, newTargetFile, false);
58    }
59 
60    public void start()
61        throws IOException {
62        if (logger.isInfoEnabled()) {
63            logger.info("copy \"" + source + "\" to \"" + target + "\"");
64        }
65        boolean copied = false;
66        InputStream in = new FileInputStream(source);
67 
68        if (makeDirs) {
69            fileTools.mkdirs(target.getParentFile());
70        }
71 
72        try {
73            OutputStream out = new FileOutputStream(target);
74 
75            try {
76                byte[] buffer = new byte[BUFFER_SIZE];
77 
78                setProgress(0);
79                while (!copied && !isInterrupted()) {
80                    int bytesRead = in.read(buffer);
81 
82                    if (bytesRead > 0) {
83                        out.write(buffer, 0, bytesRead);
84                        setProgress(getProgress() + bytesRead);
85                    } else {
86                        copied = true;
87                    }
88                }
89            } finally {
90                out.close();
91                if (!copied || isInterrupted()) {
92                    fileTools.deleteOrWarn(target, logger);
93                } else if (getProgress() != getMaxProgress()) {
94                    // Make sure that empty files also end with a progress of 1.
95                    setProgress(getMaxProgress());
96                }
97            }
98        } finally {
99            in.close();
100        }
101    }
102}

[all classes][net.sf.jomic.tools]
EMMA 2.0.4217 (C) Vladimir Roubtsov