1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.xframe.xsddoc.util;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29
30 /***
31 * Static file utility methods.
32 *
33 * @author <a href="mailto:kriede@users.sourceforge.net">Kurt Riede</a>
34 */
35 public final class FileUtil {
36
37 /*** Size of buffer when copying files. */
38 private static final int COPY_BUFFER_SIZE = 8192;
39
40 /*** file separator from <code>File.separator</code>. */
41 private static final String FILE_SEP = File.separator;
42
43 /***
44 * Private default constructor to prevent instantiation.
45 */
46 private FileUtil() {
47 }
48
49 /***
50 * Copy a file.
51 *
52 * @param in <code>InputStream</code>
53 * @param destFile out as <code>File</code>
54 * @throws IOException if file cannot ne copied
55 */
56 public static void copyFile(final InputStream in, final File destFile) throws IOException {
57 if (destFile.exists() && destFile.isFile()) {
58 destFile.delete();
59 }
60 FileOutputStream out = null;
61 try {
62 out = new FileOutputStream(destFile);
63 copyFile(in, out);
64 } finally {
65 if (out != null) {
66 out.close();
67 }
68 }
69 }
70
71 /***
72 * Copy a file.
73 *
74 * @param in <code>InputStream</code>
75 * @param out as <code>OutputStream</code>
76 * @throws IOException if file cannot ne copied
77 */
78 public static void copyFile(final InputStream in, final OutputStream out) throws IOException {
79 final byte[] buffer = new byte[COPY_BUFFER_SIZE];
80 int count = 0;
81 do {
82 out.write(buffer, 0, count);
83 count = in.read(buffer, 0, buffer.length);
84 } while (count != -1);
85 }
86
87 /***
88 * Copy a file.
89 *
90 * @param in <code>InputStream</code>
91 * @param destFile out file name
92 * @throws IOException if file cannot ne copied
93 */
94 public static void copyFile(final InputStream in, final String destFile) throws IOException {
95 copyFile(in, new File(destFile));
96 }
97
98 /***
99 * Copy a file.
100 *
101 * @param inFile source file name
102 * @param destFile destination file name
103 * @throws IOException if file cannot ne copied
104 */
105 public static void copyFile(final String inFile, final String destFile) throws IOException {
106 FileInputStream in = null;
107 try {
108 in = new FileInputStream(inFile);
109 copyFile(in, destFile);
110 } finally {
111 if (in != null) {
112 in.close();
113 }
114 }
115 }
116
117 /***
118 * Returns the location of a file relative to a base file.
119 *
120 * @param baseFile the base file
121 * @param file the file to be resolved
122 * @return resolved location
123 */
124 public static String getLocation(final String baseFile, final String file) {
125 if (baseFile == null || file.indexOf(':') > 0) {
126 return file;
127 }
128 final String parent = new File(baseFile).getParent();
129 if (parent == null) {
130 return file;
131 }
132 return parent + FILE_SEP + file;
133 }
134 }