Lines 39-44
Link Here
|
39 |
import java.util.Iterator; |
39 |
import java.util.Iterator; |
40 |
import java.util.Map; |
40 |
import java.util.Map; |
41 |
import java.util.Set; |
41 |
import java.util.Set; |
|
|
42 |
import java.util.Stack; |
42 |
import java.util.StringTokenizer; |
43 |
import java.util.StringTokenizer; |
43 |
import java.util.WeakHashMap; |
44 |
import java.util.WeakHashMap; |
44 |
import java.util.jar.JarEntry; |
45 |
import java.util.jar.JarEntry; |
Lines 99-104
Link Here
|
99 |
private FileUtil() { |
100 |
private FileUtil() { |
100 |
} |
101 |
} |
|
|
102 |
/** Returns a folder on given filesystem if such a folder exists. |
103 |
* If not then a folder is created, including any necessary but nonexistent parent |
104 |
* folders. Note that if this operation fails it may have succeeded in creating some of the necessary |
105 |
* parent folders. |
106 |
* |
107 |
* @param folder |
108 |
* @throws java.io.IOException if the creation fails |
109 |
* @return the existing or new folder |
110 |
*/ |
111 |
public static FileObject createFolder (final File folder) throws IOException { |
112 |
//doesn't work for LocalFileSystem (just on MasterFS) - probably not needed |
113 |
FileObject retval = null; |
114 |
File root = getRoot(folder); |
115 |
FileObject rootFo = FileUtil.toFileObject(root); |
116 |
assert rootFo != null : root.getAbsolutePath(); |
117 |
final String relativePath = getRelativePath(root, folder); |
118 |
try { |
119 |
retval = FileUtil.createFolder(rootFo,relativePath); |
120 |
} catch (IOException ex) { |
121 |
//thus retval = null; |
122 |
} |
123 |
//if refresh needed because of external changes |
124 |
if (retval == null || !retval.isValid()) { |
125 |
rootFo.getFileSystem().refresh(false); |
126 |
retval = FileUtil.createFolder(rootFo,relativePath); |
127 |
} |
128 |
assert retval != null; |
129 |
return retval; |
130 |
} |
131 |
|
132 |
/** Returns a data file on given filesystem if such a data file exists. |
133 |
* If not then a data file is created, including any necessary but nonexistent parent |
134 |
* folders. Note that if this operation fails it may have succeeded in creating some of the necessary |
135 |
* parent folders. |
136 |
* |
137 |
* @param data |
138 |
* @throws java.io.IOException |
139 |
* @throws java.io.IOException if the creation fails |
140 |
* @return the existing or new data file |
141 |
*/ |
142 |
public static FileObject createData (final File data) throws IOException { |
143 |
//doesn't work for LocalFileSystem (just on MasterFS) - probably not needed |
144 |
FileObject retval = null; |
145 |
File root = getRoot(data); |
146 |
FileObject rootFo = FileUtil.toFileObject(root); |
147 |
assert rootFo != null : root.getAbsolutePath(); |
148 |
final String relativePath = getRelativePath(root, data); |
149 |
try { |
150 |
retval = FileUtil.createData(rootFo,relativePath); |
151 |
} catch (IOException ex) { |
152 |
//thus retval = null; |
153 |
} |
154 |
//if refresh needed because of external changes |
155 |
if (retval == null || !retval.isValid()) { |
156 |
rootFo.getFileSystem().refresh(false); |
157 |
retval = FileUtil.createData(rootFo,relativePath); |
158 |
} |
159 |
assert retval != null; |
160 |
return retval; |
161 |
} |
162 |
|
163 |
private static File getRoot(final File dir) { |
164 |
File retval = dir; |
165 |
for (; retval.getParentFile() != null; retval = retval.getParentFile()); |
166 |
assert retval != null && retval.exists(); |
167 |
return retval; |
168 |
} |
169 |
|
170 |
private static String getRelativePath(final File dir, final File file) { |
171 |
Stack stack = new Stack (); |
172 |
File tempFile = file; |
173 |
while(tempFile != null && !tempFile.equals(dir)) { |
174 |
stack.push (tempFile.getName()); |
175 |
tempFile = tempFile.getParentFile(); |
176 |
} |
177 |
assert tempFile != null : file.getAbsolutePath() + "not found in " + dir.getAbsolutePath();//NOI18N |
178 |
StringBuilder retval = new StringBuilder(); |
179 |
while (!stack.isEmpty()) { |
180 |
retval.append((String)stack.pop()); |
181 |
if (!stack.isEmpty()) { |
182 |
retval.append("/");//NOI18N |
183 |
} |
184 |
} |
185 |
return retval.toString(); |
186 |
} |
187 |
|
101 |
/** Copies stream of files. |
188 |
/** Copies stream of files. |
102 |
* <P> |
189 |
* <P> |
103 |
* Please be aware, that this method doesn't close any of passed streams. |
190 |
* Please be aware, that this method doesn't close any of passed streams. |