2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.common
;
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
;
18 import org
.apache
.logging
.log4j
.LogManager
;
19 import org
.apache
.logging
.log4j
.Logger
;
25 public class FileCopy
{
27 private static final Logger logger
= LogManager
.getLogger();
29 // overwrite constants
30 public static final int DO_OVERWRITE
= 1;
31 public static final int NO_OVERWRITE
= 2;
34 private static int bufferSize
= 4 * 1024;
35 private static int overwrite
= DO_OVERWRITE
;
38 * Copies a File to another directory
40 * @param destinationDirectory
45 public static boolean copy(File sourceFile
, File destinationDirectory
, String destFileName
)
47 if (sourceFile
== null){
48 logger
.debug("No sourcefile defined");
49 throw new IOException("No sourcefile defined");
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());
55 if (destFileName
== null || destFileName
.equals("")){
56 destFileName
= sourceFile
.getName();
58 InputStream in
= new FileInputStream(sourceFile
);
59 copy(in
, destinationDirectory
, destFileName
);
61 if (!destinationDirectory
.isDirectory()) {
62 logger
.warn("Not a directory: " + destinationDirectory
.getName());
65 File destinationFile
= new File(destinationDirectory
, destFileName
);
67 OutputStream out
= new FileOutputStream(destinationFile
);
72 public static boolean copy(InputStream in
, File destinationDirectory
, String destFileName
)
75 if (!destinationDirectory
.isDirectory()) {
76 throw new IOException("Destination is not a directory");
78 if (destFileName
== null || destFileName
.equals("")){
79 throw new IOException("No destination file name specified");
81 File destinationFile
= new File(destinationDirectory
, destFileName
);
82 OutputStream out
= new FileOutputStream(destinationFile
);
87 public static boolean copy(InputStream in
, OutputStream out
)
89 byte[] buffer
= new byte[bufferSize
];
91 while ((bytesRead
= in
.read(buffer
)) >= 0) {
92 out
.write(buffer
, 0, bytesRead
);
96 logger
.debug("File copied");
100 public static boolean copy(String source
, String destDirectory
, String destFileName
)
102 File sourceFile
= new File(source
);
103 File destinationDir
= new File(destDirectory
);
104 return copy(sourceFile
, destinationDir
, destFileName
);
107 public static boolean copy(String source
, String destDirectory
)
109 return copy(source
, destDirectory
, null);
114 * @param file File destination
115 * @return true if data can be copied, false otherwise
117 public static boolean doCopy(File file
) {
118 boolean exists
= file
.exists();
119 if (overwrite
== DO_OVERWRITE
|| !exists
) {
121 } else if (overwrite
== NO_OVERWRITE
) {