Merge branch 'release/5.45.0'
[cdmlib.git] / cdmlib-commons / src / main / java / eu / etaxonomy / cdm / common / FileCopy.java
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 package eu.etaxonomy.cdm.common;
10
11 import java.io.File;
12 import java.io.FileInputStream;
13 import java.io.FileOutputStream;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.OutputStream;
17
18 import org.apache.logging.log4j.LogManager;
19 import org.apache.logging.log4j.Logger;
20
21 /**
22 * @author a.mueller
23 * @since 20.11.2008
24 */
25 public class FileCopy {
26
27 private static final Logger logger = LogManager.getLogger();
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 public static boolean copy(String source, String destDirectory, String destFileName)
101 throws IOException {
102 File sourceFile = new File(source);
103 File destinationDir = new File(destDirectory);
104 return copy(sourceFile, destinationDir, destFileName);
105 }
106
107 public static boolean copy(String source, String destDirectory)
108 throws IOException {
109 return copy(source, destDirectory, null);
110 }
111
112 /**
113 * True if file
114 * @param file File destination
115 * @return true if data can be copied, false otherwise
116 */
117 public static boolean doCopy(File file) {
118 boolean exists = file.exists();
119 if (overwrite == DO_OVERWRITE || !exists) {
120 return true;
121 } else if (overwrite == NO_OVERWRITE) {
122 return false;
123 } else{
124 return false;
125 }
126 }
127 }