Project

General

Profile

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

    
11
package eu.etaxonomy.cdm.common;
12

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

    
20
import org.apache.log4j.Logger;
21

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

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

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

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

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

    
114

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

    
131

    
132
}
133
	
134
	
135

    
136

    
(9-9/20)