3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.cdm
.common
;
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
;
20 import org
.apache
.log4j
.Logger
;
27 public class FileCopy
{
28 private static final Logger logger
= Logger
.getLogger(FileCopy
.class);
30 // overwrite constants
31 public static final int DO_OVERWRITE
= 1;
32 public static final int NO_OVERWRITE
= 2;
35 private static int bufferSize
= 4 * 1024;
36 private static int overwrite
= DO_OVERWRITE
;
39 * Copies a File to another directory
41 * @param destinationDirectory
46 public static boolean copy(File sourceFile
, File destinationDirectory
, String destFileName
)
48 if (sourceFile
== null){
49 logger
.debug("No sourcefile defined");
50 throw new IOException("No sourcefile defined");
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());
56 if (destFileName
== null || destFileName
.equals("")){
57 destFileName
= sourceFile
.getName();
59 InputStream in
= new FileInputStream(sourceFile
);
60 copy(in
, destinationDirectory
, destFileName
);
62 if (!destinationDirectory
.isDirectory()) {
63 logger
.warn("Not a directory: " + destinationDirectory
.getName());
66 File destinationFile
= new File(destinationDirectory
, destFileName
);
68 OutputStream out
= new FileOutputStream(destinationFile
);
73 public static boolean copy(InputStream in
, File destinationDirectory
, String destFileName
)
76 if (!destinationDirectory
.isDirectory()) {
77 throw new IOException("Destination is not a directory");
79 if (destFileName
== null || destFileName
.equals("")){
80 throw new IOException("No destination file name specified");
82 File destinationFile
= new File(destinationDirectory
, destFileName
);
83 OutputStream out
= new FileOutputStream(destinationFile
);
88 public static boolean copy(InputStream in
, OutputStream out
)
90 byte[] buffer
= new byte[bufferSize
];
92 while ((bytesRead
= in
.read(buffer
)) >= 0) {
93 out
.write(buffer
, 0, bytesRead
);
97 logger
.debug("File copied");
102 public static boolean copy(String source
, String destDirectory
, String destFileName
)
104 File sourceFile
= new File(source
);
105 File destinationDir
= new File(destDirectory
);
106 return copy(sourceFile
, destinationDir
, destFileName
);
109 public static boolean copy(String source
, String destDirectory
)
111 return copy(source
, destDirectory
, null);
117 * @param file File destination
118 * @return true if data can be copied, false otherwise
120 public static boolean doCopy(File file
) {
121 boolean exists
= file
.exists();
122 if (overwrite
== DO_OVERWRITE
|| !exists
) {
124 } else if (overwrite
== NO_OVERWRITE
) {