| 1 |
|
package com.atlassian.core.util; |
| 2 |
|
|
| 3 |
|
import com.atlassian.core.util.zip.FolderArchiver; |
| 4 |
|
import org.apache.log4j.Logger; |
| 5 |
|
import org.apache.commons.io.IOUtils; |
| 6 |
|
|
| 7 |
|
import javax.servlet.http.HttpServletRequest; |
| 8 |
|
import java.io.*; |
| 9 |
|
import java.util.ArrayList; |
| 10 |
|
import java.util.List; |
| 11 |
|
import java.util.Iterator; |
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
|
|
|
| 36.9% |
Uncovered Elements: 118 (187) |
Complexity: 51 |
Complexity Density: 0.44 |
|
| 16 |
|
public class FileUtils |
| 17 |
|
{ |
| 18 |
|
private static final Logger log = Logger.getLogger(FileUtils.class); |
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
@param |
| 23 |
|
@param |
| 24 |
|
@return |
| 25 |
|
@throws |
| 26 |
|
@deprecated@link |
| 27 |
|
@see |
| 28 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 29 |
1
|
public static int copy(final InputStream input, final OutputStream output)... |
| 30 |
|
throws IOException |
| 31 |
|
{ |
| 32 |
1
|
return IOUtils.copy(input, output); |
| 33 |
|
} |
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
@param |
| 38 |
|
@param |
| 39 |
|
@param |
| 40 |
|
@return |
| 41 |
|
@throws |
| 42 |
|
@deprecated@link |
| 43 |
|
@see |
| 44 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 45 |
0
|
public static int copy(final InputStream input,... |
| 46 |
|
final OutputStream output, |
| 47 |
|
final int bufferSize) |
| 48 |
|
throws IOException |
| 49 |
|
{ |
| 50 |
0
|
return IOUtils.copy(input, output); |
| 51 |
|
} |
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
@link |
| 56 |
|
@param |
| 57 |
|
@deprecated@link |
| 58 |
|
@see |
| 59 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 60 |
2
|
public static void shutdownStream(final OutputStream output)... |
| 61 |
|
{ |
| 62 |
2
|
IOUtils.closeQuietly(output); |
| 63 |
|
} |
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
@link |
| 68 |
|
@param |
| 69 |
|
@deprecated@link |
| 70 |
|
@see |
| 71 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 72 |
2
|
public static void shutdownStream(final InputStream input)... |
| 73 |
|
{ |
| 74 |
2
|
IOUtils.closeQuietly(input); |
| 75 |
|
} |
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
|
|
|
| 75.9% |
Uncovered Elements: 7 (29) |
Complexity: 8 |
Complexity Density: 0.47 |
|
| 80 |
2
|
public static boolean deleteDir(File dir)... |
| 81 |
|
{ |
| 82 |
2
|
if (dir == null) |
| 83 |
|
{ |
| 84 |
0
|
return false; |
| 85 |
|
} |
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 90 |
2
|
File candir; |
| 91 |
2
|
try |
| 92 |
|
{ |
| 93 |
2
|
candir = dir.getCanonicalFile(); |
| 94 |
|
} |
| 95 |
|
catch (IOException e) |
| 96 |
|
{ |
| 97 |
0
|
return false; |
| 98 |
|
} |
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
2
|
if (!candir.equals(dir.getAbsoluteFile())) |
| 103 |
|
{ |
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
0
|
return false; |
| 108 |
|
} |
| 109 |
|
|
| 110 |
|
|
| 111 |
|
|
| 112 |
2
|
File[] files = candir.listFiles(); |
| 113 |
2
|
if (files != null) |
| 114 |
|
{ |
| 115 |
4
|
for (int i = 0; i < files.length; i++) |
| 116 |
|
{ |
| 117 |
2
|
File file = files[i]; |
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
|
|
| 122 |
2
|
boolean deleted = !file.delete(); |
| 123 |
2
|
if (deleted) |
| 124 |
|
{ |
| 125 |
|
|
| 126 |
|
|
| 127 |
1
|
if (file.isDirectory()) deleteDir(file); |
| 128 |
|
|
| 129 |
|
|
| 130 |
|
} |
| 131 |
|
} |
| 132 |
|
} |
| 133 |
|
|
| 134 |
|
|
| 135 |
|
|
| 136 |
2
|
return dir.delete(); |
| 137 |
|
} |
| 138 |
|
|
| 139 |
|
|
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
| 143 |
0
|
public static void recursiveDelete(File file)... |
| 144 |
|
{ |
| 145 |
0
|
if (!file.isDirectory()) |
| 146 |
|
{ |
| 147 |
0
|
file.delete(); |
| 148 |
0
|
return; |
| 149 |
|
} |
| 150 |
|
|
| 151 |
0
|
File[] files = file.listFiles(); |
| 152 |
0
|
for (int i = 0; i < files.length; i++) |
| 153 |
|
{ |
| 154 |
0
|
File next = files[i]; |
| 155 |
0
|
recursiveDelete(next); |
| 156 |
|
} |
| 157 |
|
|
| 158 |
0
|
file.delete(); |
| 159 |
|
} |
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
|
| 164 |
|
|
| 165 |
|
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
| 166 |
0
|
public static String getResourceContent(String resource)... |
| 167 |
|
{ |
| 168 |
0
|
InputStream is = ClassLoaderUtils.getResourceAsStream(resource, FileUtils.class); |
| 169 |
0
|
if (is == null) return null; |
| 170 |
|
|
| 171 |
0
|
try |
| 172 |
|
{ |
| 173 |
0
|
return IOUtils.toString(is); |
| 174 |
|
} |
| 175 |
|
catch (IOException e) |
| 176 |
|
{ |
| 177 |
0
|
log.error("IOException reading stream: " + e, e); |
| 178 |
0
|
return null; |
| 179 |
|
} |
| 180 |
|
finally |
| 181 |
|
{ |
| 182 |
0
|
IOUtils.closeQuietly(is); |
| 183 |
|
} |
| 184 |
|
} |
| 185 |
|
|
| 186 |
|
|
| 187 |
|
|
| 188 |
|
|
| 189 |
|
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
| 190 |
0
|
public static String getResourceContent(HttpServletRequest req, String resource)... |
| 191 |
|
{ |
| 192 |
0
|
InputStream is = req.getSession().getServletContext().getResourceAsStream(resource); |
| 193 |
0
|
if (is == null) return ""; |
| 194 |
|
|
| 195 |
0
|
try |
| 196 |
|
{ |
| 197 |
0
|
String result = IOUtils.toString(is); |
| 198 |
0
|
return (result == null) ? "" : result; |
| 199 |
|
} |
| 200 |
|
catch (IOException e) |
| 201 |
|
{ |
| 202 |
0
|
log.error("IOException reading stream: " + e, e); |
| 203 |
0
|
return ""; |
| 204 |
|
} |
| 205 |
|
finally |
| 206 |
|
{ |
| 207 |
0
|
IOUtils.closeQuietly(is); |
| 208 |
|
} |
| 209 |
|
} |
| 210 |
|
|
| 211 |
|
|
| 212 |
|
|
| 213 |
|
|
| 214 |
|
@deprecated@link |
| 215 |
|
@see |
| 216 |
|
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
| 217 |
0
|
public static String getInputStreamTextContent(InputStream is)... |
| 218 |
|
{ |
| 219 |
0
|
if (is == null) |
| 220 |
|
{ |
| 221 |
0
|
return null; |
| 222 |
|
} |
| 223 |
|
|
| 224 |
0
|
try |
| 225 |
|
{ |
| 226 |
0
|
return IOUtils.toString(is); |
| 227 |
|
} |
| 228 |
|
catch (IOException e) |
| 229 |
|
{ |
| 230 |
0
|
log.error("IOException reading stream: " + e, e); |
| 231 |
0
|
return null; |
| 232 |
|
} |
| 233 |
|
finally |
| 234 |
|
{ |
| 235 |
0
|
IOUtils.closeQuietly(is); |
| 236 |
|
} |
| 237 |
|
} |
| 238 |
|
|
| 239 |
|
|
| 240 |
|
|
| 241 |
|
|
| 242 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 243 |
5
|
public static void saveTextFile(String stringContent, File destFile) throws IOException... |
| 244 |
|
{ |
| 245 |
5
|
ensureFileAndPathExist(destFile); |
| 246 |
|
|
| 247 |
5
|
FileWriter writer = new FileWriter(destFile); |
| 248 |
5
|
writer.write(stringContent); |
| 249 |
5
|
writer.close(); |
| 250 |
|
} |
| 251 |
|
|
| 252 |
|
|
| 253 |
|
|
| 254 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 255 |
5
|
public static void ensureFileAndPathExist(File file) throws IOException... |
| 256 |
|
{ |
| 257 |
5
|
file.getParentFile().mkdirs(); |
| 258 |
5
|
file.createNewFile(); |
| 259 |
|
} |
| 260 |
|
|
| 261 |
|
|
| 262 |
|
|
| 263 |
|
|
| 264 |
|
|
| 265 |
|
|
| 266 |
|
|
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 267 |
1
|
public static boolean moveDir(File dirName, File destDir)... |
| 268 |
|
{ |
| 269 |
1
|
File destParent = new File(destDir.getParent()); |
| 270 |
|
|
| 271 |
|
|
| 272 |
1
|
if (destDir.exists()) |
| 273 |
|
{ |
| 274 |
0
|
destDir.delete(); |
| 275 |
|
} |
| 276 |
|
|
| 277 |
|
|
| 278 |
1
|
destParent.mkdirs(); |
| 279 |
1
|
return dirName.renameTo(destDir); |
| 280 |
|
} |
| 281 |
|
|
| 282 |
|
|
| 283 |
|
|
| 284 |
|
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 285 |
0
|
public static void createZipFile(File baseDir, File zipFile) throws Exception... |
| 286 |
|
{ |
| 287 |
0
|
FolderArchiver compressor = new FolderArchiver(baseDir, zipFile); |
| 288 |
0
|
compressor.doArchive(); |
| 289 |
|
} |
| 290 |
|
|
| 291 |
|
|
| 292 |
|
|
| 293 |
|
|
| 294 |
|
|
| 295 |
|
|
|
|
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 5 |
Complexity Density: 0.42 |
|
| 296 |
0
|
public static List readResourcesAsList(String resource)... |
| 297 |
|
{ |
| 298 |
0
|
List result = new ArrayList(); |
| 299 |
|
|
| 300 |
0
|
InputStream is = ClassLoaderUtils.getResourceAsStream(resource, FileUtils.class); |
| 301 |
|
|
| 302 |
0
|
try |
| 303 |
|
{ |
| 304 |
0
|
result.addAll(IOUtils.readLines(is)); |
| 305 |
|
} |
| 306 |
|
catch (IOException e) |
| 307 |
|
{ |
| 308 |
0
|
log.error("IOException reading stream: " + e, e); |
| 309 |
0
|
return result; |
| 310 |
|
} |
| 311 |
|
finally |
| 312 |
|
{ |
| 313 |
0
|
IOUtils.closeQuietly(is); |
| 314 |
|
} |
| 315 |
|
|
| 316 |
0
|
for (Iterator iterator = result.iterator(); iterator.hasNext();) |
| 317 |
|
{ |
| 318 |
0
|
String s = (String) iterator.next(); |
| 319 |
0
|
if (org.apache.commons.lang.StringUtils.isBlank(s) || org.apache.commons.lang.StringUtils.trimToEmpty(s).startsWith("#")) |
| 320 |
0
|
iterator.remove(); |
| 321 |
|
} |
| 322 |
|
|
| 323 |
0
|
return result; |
| 324 |
|
} |
| 325 |
|
|
| 326 |
|
|
| 327 |
|
|
| 328 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 329 |
0
|
public static void copyDirectory(File srcDir, File destDir) throws IOException... |
| 330 |
|
{ |
| 331 |
0
|
copyDirectory(srcDir, destDir, false); |
| 332 |
|
} |
| 333 |
|
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 5 |
Complexity Density: 0.45 |
|
| 334 |
0
|
public static void copyDirectory(File srcDir, File destDir, boolean overwrite) throws IOException... |
| 335 |
|
{ |
| 336 |
0
|
File[] files = srcDir.listFiles(); |
| 337 |
|
|
| 338 |
0
|
if (!destDir.exists()) |
| 339 |
0
|
destDir.mkdirs(); |
| 340 |
|
else |
| 341 |
0
|
log.debug(destDir.getAbsolutePath() + " already exists"); |
| 342 |
|
|
| 343 |
0
|
if (files != null) |
| 344 |
|
{ |
| 345 |
0
|
for (int i = 0; i < files.length; i++) |
| 346 |
|
{ |
| 347 |
0
|
File file = files[i]; |
| 348 |
0
|
File dest = new File(destDir, file.getName()); |
| 349 |
|
|
| 350 |
0
|
if (file.isFile()) |
| 351 |
0
|
copyFile(new FileInputStream(file), dest, overwrite); |
| 352 |
|
else |
| 353 |
0
|
copyDirectory(file, dest, overwrite); |
| 354 |
|
} |
| 355 |
|
} |
| 356 |
|
} |
| 357 |
|
|
| 358 |
|
|
| 359 |
|
|
| 360 |
|
|
| 361 |
|
|
| 362 |
|
|
| 363 |
|
@param |
| 364 |
|
@param |
| 365 |
|
|
| 366 |
|
|
| 367 |
|
@throws |
| 368 |
|
|
| 369 |
|
|
| 370 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 371 |
1
|
public static void copyFile(File srcFile, File destFile) throws IOException... |
| 372 |
|
{ |
| 373 |
1
|
copyFile(srcFile, destFile, true); |
| 374 |
|
} |
| 375 |
|
|
| 376 |
|
|
| 377 |
|
|
| 378 |
|
|
| 379 |
|
|
| 380 |
|
|
| 381 |
|
|
| 382 |
|
|
| 383 |
|
|
| 384 |
|
|
| 385 |
|
@param |
| 386 |
|
@param |
| 387 |
|
@param |
| 388 |
|
@throws |
| 389 |
|
|
| 390 |
|
|
|
|
|
| 66.7% |
Uncovered Elements: 4 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
| 391 |
1
|
public static void copyFile(File srcFile, File destFile, boolean overwrite) throws IOException... |
| 392 |
|
{ |
| 393 |
1
|
if (!srcFile.exists()) |
| 394 |
|
{ |
| 395 |
0
|
throw new IOException("File " + srcFile + " does not exist"); |
| 396 |
|
} |
| 397 |
|
|
| 398 |
1
|
InputStream input = new FileInputStream(srcFile); |
| 399 |
1
|
try |
| 400 |
|
{ |
| 401 |
1
|
copyFile(input, destFile, overwrite); |
| 402 |
|
} |
| 403 |
|
finally |
| 404 |
|
{ |
| 405 |
1
|
IOUtils.closeQuietly(input); |
| 406 |
|
} |
| 407 |
|
|
| 408 |
1
|
if (srcFile.length() != srcFile.length()) |
| 409 |
|
{ |
| 410 |
0
|
throw new IOException("Failed to copy full contents from " + srcFile + " to " + destFile); |
| 411 |
|
} |
| 412 |
|
} |
| 413 |
|
|
| 414 |
|
|
| 415 |
|
|
| 416 |
|
|
| 417 |
|
|
| 418 |
|
|
| 419 |
|
|
| 420 |
|
|
| 421 |
|
@param |
| 422 |
|
@param |
| 423 |
|
@throws |
| 424 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 425 |
0
|
public static void copyFile(InputStream srcStream, File destFile) throws IOException... |
| 426 |
|
{ |
| 427 |
0
|
copyFile(srcStream, destFile, false); |
| 428 |
|
} |
| 429 |
|
|
| 430 |
|
|
| 431 |
|
|
| 432 |
|
|
| 433 |
|
|
| 434 |
|
|
| 435 |
|
|
| 436 |
|
|
| 437 |
|
@param |
| 438 |
|
@param |
| 439 |
|
@param |
| 440 |
|
@throws |
| 441 |
|
|
|
|
|
| 56% |
Uncovered Elements: 11 (25) |
Complexity: 6 |
Complexity Density: 0.35 |
|
| 442 |
1
|
public static void copyFile(InputStream srcStream, File destFile, boolean overwrite) throws IOException... |
| 443 |
|
{ |
| 444 |
1
|
File parentFile = destFile.getParentFile(); |
| 445 |
1
|
if (!parentFile.isDirectory()) |
| 446 |
|
{ |
| 447 |
0
|
parentFile.mkdirs(); |
| 448 |
|
} |
| 449 |
|
|
| 450 |
1
|
if (destFile.exists()) |
| 451 |
|
{ |
| 452 |
1
|
if (!destFile.canWrite()) |
| 453 |
|
{ |
| 454 |
0
|
throw new IOException("Unable to open file " + destFile + " for writing."); |
| 455 |
|
} |
| 456 |
1
|
if (!overwrite) |
| 457 |
|
{ |
| 458 |
0
|
log.warn(destFile.getAbsolutePath() + " already exists"); |
| 459 |
0
|
return; |
| 460 |
|
} |
| 461 |
1
|
log.debug("Overwriting file at: " + destFile.getAbsolutePath()); |
| 462 |
|
} |
| 463 |
|
else |
| 464 |
|
{ |
| 465 |
0
|
destFile.createNewFile(); |
| 466 |
|
} |
| 467 |
|
|
| 468 |
1
|
OutputStream output = new BufferedOutputStream(new FileOutputStream(destFile)); |
| 469 |
1
|
try |
| 470 |
|
{ |
| 471 |
1
|
IOUtils.copy(srcStream, output); |
| 472 |
|
} |
| 473 |
|
catch (IOException e) |
| 474 |
|
{ |
| 475 |
0
|
log.error("Error writing stream to file: " + destFile.getAbsolutePath()); |
| 476 |
0
|
throw e; |
| 477 |
|
} |
| 478 |
|
finally |
| 479 |
|
{ |
| 480 |
1
|
IOUtils.closeQuietly(output); |
| 481 |
|
} |
| 482 |
|
} |
| 483 |
|
} |