Project

General

Profile

Download (3.62 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy 
4
* http://www.e-taxonomy.eu
5
* 
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.cdm.common;
11

    
12
import java.io.File;
13
import java.io.FileInputStream;
14
import java.io.FileOutputStream;
15
import java.io.IOException;
16
import java.io.InputStream;
17
import java.io.OutputStream;
18

    
19
import org.apache.log4j.Logger;
20

    
21
/**
22
 * @author a.mueller
23
 * @since 20.11.2008
24
 * @version 1.0
25
 */
26
public class FileCopy {
27
	private static final Logger logger = Logger.getLogger(FileCopy.class);
28

    
29
	// overwrite constants
30
	public static final int DO_OVERWRITE = 1;
31
	public static final int NO_OVERWRITE = 2;
32

    
33
	// default values
34
	private static int bufferSize = 4 * 1024;
35
	private static int overwrite = DO_OVERWRITE;
36
		
37
	/**
38
	 * Copies a File to another directory 
39
	 * @param sourceFile
40
	 * @param destinationDirectory
41
	 * @param destFileName
42
	 * @return
43
	 * @throws IOException
44
	 */
45
	public static boolean copy(File sourceFile, File destinationDirectory, String destFileName)
46
			throws IOException {
47
		if (sourceFile == null){
48
			logger.debug("No sourcefile defined");
49
			throw new IOException("No sourcefile defined");
50
		}
51
		if (!sourceFile.isFile() || !sourceFile.canRead()) {
52
			logger.debug("Not a readable file: " + sourceFile.getName());
53
			throw new IOException("Not a readable file: " + sourceFile.getName());
54
		}
55
		if (destFileName == null || destFileName.equals("")){
56
			destFileName = sourceFile.getName();
57
		}
58
		InputStream in = new FileInputStream(sourceFile);
59
		copy(in, destinationDirectory, destFileName);
60
		
61
		if (!destinationDirectory.isDirectory()) {
62
			logger.warn("Not a directory: " + destinationDirectory.getName());
63
			return false;
64
		}
65
		File destinationFile = new File(destinationDirectory, destFileName);
66

    
67
		OutputStream out = new FileOutputStream(destinationFile);
68
		
69
		return copy(in, out);
70
	}
71
	
72
	public static boolean copy(InputStream in, File destinationDirectory, String destFileName)
73
			throws IOException {
74
		
75
		if (!destinationDirectory.isDirectory()) {
76
			throw new IOException("Destination is not a directory");
77
		}
78
		if (destFileName == null || destFileName.equals("")){
79
			throw new IOException("No destination file name specified");
80
		}
81
		File destinationFile = new File(destinationDirectory, destFileName);
82
		OutputStream out = new FileOutputStream(destinationFile);
83
		
84
		return copy(in, out);
85
}
86
	
87
	public static boolean copy(InputStream in, OutputStream out)
88
			throws IOException {
89
		byte[] buffer = new byte[bufferSize];
90
		int bytesRead;
91
		while ((bytesRead = in.read(buffer)) >= 0) {
92
			out.write(buffer, 0, bytesRead);
93
		}
94
		out.close();
95
		in.close();
96
		logger.debug("File copied");
97
		return true;
98
	}
99

    
100
	
101
	public static boolean copy(String source, String destDirectory, String destFileName)
102
			throws IOException {
103
		File sourceFile = new File(source);
104
		File destinationDir = new File(destDirectory);
105
		return copy(sourceFile, destinationDir, destFileName);
106
	}
107
	
108
	public static boolean copy(String source, String destDirectory)
109
			throws IOException {
110
		return copy(source, destDirectory, null);
111
	}
112

    
113

    
114
	/**
115
	 * True if file 
116
	 * @param file File destination
117
	 * @return true if data can be copied, false otherwise
118
	 */
119
	public static boolean doCopy(File file) {
120
		boolean exists = file.exists();
121
		if (overwrite == DO_OVERWRITE || !exists) {
122
			return true;
123
		} else if (overwrite == NO_OVERWRITE) {
124
			return false;
125
		} else{
126
			return false;
127
		}
128
	}
129

    
130

    
131
}
132
	
133
	
134

    
135

    
(10-10/21)