01: package com.xoetrope.export.util;
02:
03: import java.io.BufferedInputStream;
04: import java.io.InputStream;
05: import java.io.OutputStream;
06: import net.xoetrope.xui.XProject;
07:
08: /**
09: *
10: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
11: * the GNU Public License (GPL), please see license.txt for more details. If
12: * you make commercial use of this software you must purchase a commercial
13: * license from Xoetrope.</p>
14: * <p> $Revision: 1.6 $</p>
15: */
16: public class FileCopy {
17:
18: /**
19: * Copy a file specified by the source path to the target path
20: * @param clazz a class whose classloader will be used to load the resource
21: * @param source the file which we want to copy
22: * @param target the file we want to create
23: * @return true if the copy operation was successful, otherwise false is returned
24: */
25: public static boolean copyFile(XProject currentProject,
26: Class clazz, String source, String target) {
27: byte[] fileBytes = new byte[0];
28: InputStream is = null;
29: if (clazz != null)
30: is = new BufferedInputStream(clazz.getClassLoader()
31: .getResourceAsStream(source));
32: if (is == null)
33: is = currentProject.getBufferedInputStream(source);
34:
35: if (is != null) {
36: try {
37: byte[] b = new byte[1024];
38: if (is != null) {
39: int i = is.read(b);
40: OutputStream fos = currentProject
41: .getBufferedOutputStream(target, false);
42: while (i != -1) {
43: fileBytes = new byte[i];
44: System.arraycopy(b, 0, fileBytes, 0, i);
45: fos.write(fileBytes);
46: b = new byte[1024];
47: i = is.read(b);
48: }
49: fos.flush();
50: fos.close();
51: } else
52: return false;
53: } catch (Exception ex) {
54: System.err.println("Could not copy file " + source);
55: return false;
56: }
57: }
58:
59: return true;
60: }
61:
62: /**
63: * Copy a file specified by the source path to the target path
64: * @param source the file which we want to copy
65: * @param target the file we want to create
66: * @return true if the copy operation was successful, otherwise false is returned
67: */
68: public static boolean copyFile(XProject currentProject,
69: String source, String target) {
70: return copyFile(currentProject, null, source, target);
71: }
72:
73: }
|