1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.onemind.commons.java.util;
22
23 import java.io.*;
24 /***
25 * File related utilities methods
26 * @author TiongHiang Lee (thlee@onemindsoft.org)
27 * @version $Id: FileUtils.java,v 1.3 2004/09/19 20:07:04 thlee Exp $ $Name: $
28 */
29 public final class FileUtils
30 {
31
32 /***
33 * {@inheritDoc}
34 */
35 private FileUtils()
36 {
37 };
38
39 /*** the forward slash * */
40 private static final String FSLASH = "/";
41
42 /*** the back slash * */
43 private static final String BSLASH = "//";
44
45 /*** the separator * */
46 private static final String SEPARATOR = FSLASH;
47
48
49
50 /***
51 * Concat the strings to be a valid file path
52 * @param args the string
53 * @return the file path
54 * @todo add doc to describe the behavior
55 */
56 public static String concatFilePath(String[] args)
57 {
58 StringBuffer sb = new StringBuffer(args[0]);
59 for (int i = 1; i < args.length; i++)
60 {
61 concatFilePath(sb, args[i]);
62 }
63 return sb.toString();
64 }
65
66
67 /***
68 * Concat filepath with the prefix and suffix
69 * @param prefix the prefix
70 * @param suffix the suffix
71 * @return the file path
72 * @todo add doc to describe the behavior
73 */
74 public static String concatFilePath(String prefix, String suffix)
75 {
76 return concatFilePath(new StringBuffer(prefix), suffix);
77 }
78
79 /***
80 * concat a meaning file path
81 * @param prefix the prefix
82 * @param suffix the suffix
83 * @return the concat'ed file path
84 */
85 private static String concatFilePath(StringBuffer prefix, String suffix)
86 {
87 String pf = prefix.toString();
88 if (pf.endsWith(FSLASH) || pf.endsWith(BSLASH))
89 {
90 if (suffix.startsWith(FSLASH) || suffix.startsWith(BSLASH))
91 {
92 if (suffix.length() > 1)
93 {
94 prefix.append(suffix.substring(1));
95 }
96
97 } else
98 {
99 prefix.append(suffix);
100 }
101 } else
102 {
103 if (suffix.startsWith(FSLASH) || suffix.startsWith(BSLASH))
104 {
105 prefix.append(suffix);
106 } else
107 {
108 prefix.append(FSLASH);
109 prefix.append(suffix);
110 }
111 }
112 return prefix.toString();
113 }
114
115 /***
116 * Copy the input to the output. Will not close the output after copying
117 * @param input the input stream
118 * @param output the output
119 * @param chunkSize the chunk size
120 *
121 */
122 public static void copyStream(InputStream input, OutputStream output, int chunkSize) throws IOException
123 {
124 byte[] buffer = new byte[chunkSize];
125 int n = input.read(buffer, 0, chunkSize);
126 while (n != -1)
127 {
128 output.write(buffer, 0, n);
129 n = input.read(buffer, 0, chunkSize);
130 }
131 output.flush();
132 }
133 }