Coverage Report - org.owasp.dependencycheck.utils.FileUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
FileUtils
39%
9/23
25%
2/8
2.2
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.utils;
 19  
 
 20  
 import org.slf4j.Logger;
 21  
 import org.slf4j.LoggerFactory;
 22  
 
 23  
 import java.io.File;
 24  
 import java.io.IOException;
 25  
 import java.util.UUID;
 26  
 
 27  
 /**
 28  
  * A collection of utilities for processing information about files.
 29  
  *
 30  
  * @author Jeremy Long
 31  
  */
 32  
 public final class FileUtils {
 33  
 
 34  
     /**
 35  
      * The logger.
 36  
      */
 37  1
     private static final Logger LOGGER = LoggerFactory.getLogger(FileUtils.class);
 38  
     /**
 39  
      * Bit bucket for non-Windows systems
 40  
      */
 41  
     private static final String BIT_BUCKET_UNIX = "/dev/null";
 42  
 
 43  
     /**
 44  
      * Bit bucket for Windows systems (yes, only one 'L')
 45  
      */
 46  
     private static final String BIT_BUCKET_WIN = "NUL";
 47  
 
 48  
     /**
 49  
      * Private constructor for a utility class.
 50  
      */
 51  0
     private FileUtils() {
 52  0
     }
 53  
 
 54  
     /**
 55  
      * Returns the (lowercase) file extension for a specified file.
 56  
      *
 57  
      * @param fileName the file name to retrieve the file extension from.
 58  
      * @return the file extension.
 59  
      */
 60  
     public static String getFileExtension(String fileName) {
 61  2
         String ret = null;
 62  2
         final int pos = fileName.lastIndexOf(".");
 63  2
         if (pos >= 0) {
 64  2
             ret = fileName.substring(pos + 1, fileName.length()).toLowerCase();
 65  
         }
 66  2
         return ret;
 67  
     }
 68  
 
 69  
     /**
 70  
      * Deletes a file. If the File is a directory it will recursively delete the contents.
 71  
      *
 72  
      * @param file the File to delete
 73  
      * @return true if the file was deleted successfully, otherwise false
 74  
      */
 75  
     public static boolean delete(File file) {
 76  3
         boolean success = true;
 77  3
         if (!org.apache.commons.io.FileUtils.deleteQuietly(file)) {
 78  0
             success = false;
 79  0
             LOGGER.debug("Failed to delete file: {}; attempting to delete on exit.", file.getPath());
 80  0
             file.deleteOnExit();
 81  
         }
 82  3
         return success;
 83  
     }
 84  
 
 85  
     /**
 86  
      * Generates a new temporary file name that is guaranteed to be unique.
 87  
      *
 88  
      * @param prefix the prefix for the file name to generate
 89  
      * @param extension the extension of the generated file name
 90  
      * @return a temporary File
 91  
      * @throws java.io.IOException thrown if the temporary folder could not be created
 92  
      */
 93  
     public static File getTempFile(String prefix, String extension) throws IOException {
 94  0
         final File dir = Settings.getTempDirectory();
 95  0
         final String tempFileName = String.format("%s%s.%s", prefix, UUID.randomUUID().toString(), extension);
 96  0
         final File tempFile = new File(dir, tempFileName);
 97  0
         if (tempFile.exists()) {
 98  0
             return getTempFile(prefix, extension);
 99  
         }
 100  0
         return tempFile;
 101  
     }
 102  
 
 103  
     /**
 104  
      * Return the bit bucket for the OS. '/dev/null' for Unix and 'NUL' for Windows
 105  
      *
 106  
      * @return a String containing the bit bucket
 107  
      */
 108  
     public static String getBitBucket() {
 109  0
         if (System.getProperty("os.name").startsWith("Windows")) {
 110  0
             return BIT_BUCKET_WIN;
 111  
         } else {
 112  0
             return BIT_BUCKET_UNIX;
 113  
         }
 114  
     }
 115  
 }