Coverage Report - org.homeunix.thecave.buddi.util.FileFunctions
 
Classes in this File Line Coverage Branch Coverage Complexity
FileFunctions
0%
0/43
0%
0/24
3.667
FileFunctions$1
0%
0/2
0%
0/6
3.667
FileFunctions$2
0%
0/5
0%
0/10
3.667
 
 1  
 /*
 2  
  * Created on Aug 28, 2005 by wyatt
 3  
  */
 4  
 package org.homeunix.thecave.buddi.util;
 5  
 
 6  
 import java.io.BufferedReader;
 7  
 import java.io.File;
 8  
 import java.io.FileInputStream;
 9  
 import java.io.FileNotFoundException;
 10  
 import java.io.FileOutputStream;
 11  
 import java.io.FilenameFilter;
 12  
 import java.io.IOException;
 13  
 import java.io.InputStream;
 14  
 import java.io.InputStreamReader;
 15  
 import java.nio.channels.FileChannel;
 16  
 import java.util.LinkedList;
 17  
 import java.util.List;
 18  
 
 19  
 /**
 20  
  * Common file-based functions for which there is no good support in Java.
 21  
  * 
 22  
  * @author wyatt
 23  
  *
 24  
  */
 25  
 public class FileFunctions {
 26  
         
 27  0
         private FileFunctions(){}
 28  
         
 29  
         /**
 30  
          * Copies a given file from Source to Destination
 31  
          * @param source
 32  
          * @param dest
 33  
          * @throws IOException
 34  
          */
 35  
         public static void copyFile(File source, File dest) throws IOException {
 36  0
                 FileChannel in = null, out = null;
 37  
                 try {          
 38  0
                         in = new FileInputStream(source).getChannel();
 39  0
                         out = new FileOutputStream(dest).getChannel();
 40  
                         
 41  0
                         in.transferTo( 0, in.size(), out);
 42  
                         
 43  
                 }
 44  0
                 catch (FileNotFoundException fnfe){
 45  0
                         fnfe.printStackTrace();
 46  
                 }
 47  
                 finally{
 48  0
                         if (in != null)
 49  0
                                 in.close();
 50  0
                         if (out != null)
 51  0
                                 out.close();
 52  
                 }
 53  0
         }
 54  
 
 55  
         /**
 56  
          * Reads a String from a given InputStream.  Useful for reading text 
 57  
          * files from Jars, etc.  Thanks to JST for helping find and fix a bug 
 58  
          * in this code which would append garbage characters to the output.  
 59  
          * @param stream
 60  
          * @return
 61  
          * @throws IOException
 62  
          */
 63  
         public static String readTextStream(InputStream stream) throws IOException {
 64  0
                 StringBuilder sb = new StringBuilder(1024);
 65  0
                 BufferedReader reader = new BufferedReader(new InputStreamReader(stream));                        
 66  0
                 char[] chars = new char[1024];
 67  
                 int charsRead;
 68  0
                 while((charsRead = reader.read(chars)) > -1){
 69  0
                         sb.append(String.valueOf(chars).substring(0, charsRead));
 70  
                 }
 71  
 
 72  0
                 reader.close();
 73  0
                 return sb.toString();                
 74  
         }
 75  
         
 76  
         /**
 77  
          * Simple test to see if a folder is writable.  File.canWrite()
 78  
          * does not work on all Windows folders, as the folder may
 79  
          * be set RO but permissions allow it to be written.
 80  
          * This is in response to Buddi bug #1716654, "Error creating
 81  
          * a new file in My Documents folder".
 82  
          * @param folder
 83  
          * @return
 84  
          */
 85  
         public static boolean isFolderWritable(File folder){
 86  0
                 if (!folder.isDirectory())
 87  0
                         folder = folder.getParentFile();
 88  
                 
 89  
                 //If the folder is set to writable, it is fine...
 90  0
                 if (folder.canWrite())
 91  0
                         return true;
 92  
                 
 93  
                 //... the problem is when the folder is mistakenly set to read only.
 94  
                 // The test is simple - we try to write to a new file in the given 
 95  
                 // folder.  If it succeeds, we remove it and return true.  If it
 96  
                 // fails, we return false.
 97  0
                 File testFile = null;
 98  0
                 final int MAX_CHECKS = 25;
 99  0
                 for (int i = 0; i <= MAX_CHECKS && (testFile == null || testFile.exists()); i++){
 100  0
                         testFile = new File(folder.getAbsolutePath() + File.separator + "." + Math.random());
 101  0
                         if (i == MAX_CHECKS)
 102  0
                                 return false; //Could not test file - all tests already failed
 103  
                 }
 104  
 
 105  
                 try {
 106  0
                         testFile.createNewFile();
 107  0
                         if (testFile.exists()){
 108  0
                                 testFile.delete();
 109  0
                                 return true;
 110  
                         }
 111  
                 }
 112  0
                 catch (IOException ioe){}
 113  
 
 114  0
                 return false;
 115  
         }
 116  
         
 117  
         /**
 118  
          * Returns a list of all File objects which descend from the given
 119  
          * root.  If root is a file, this list will contain just that file.
 120  
          * It root if a directory, this list will contain that file, plus
 121  
          * all directories and files beneath it. 
 122  
          * @param root
 123  
          * @return
 124  
          */
 125  
         public static List<File> getFileTree(File root){
 126  0
                 List<File> files = new LinkedList<File>();
 127  0
                 files.add(root);
 128  
 
 129  0
                 if (root.isDirectory()){
 130  0
                         for (File file : root.listFiles()) {
 131  0
                                 files.addAll(getFileTree(file));
 132  
                         }
 133  
                 }
 134  
 
 135  0
                 return files;
 136  
         }
 137  
         
 138  
         public static FilenameFilter getDirectoryFilter(final boolean includeHiddenDirectories){
 139  0
                 return new FilenameFilter(){
 140  
                         public boolean accept(File dir, String name) {
 141  0
                                 return (includeHiddenDirectories || !name.startsWith(".")) && new File(dir.getAbsoluteFile() + File.separator + name).isDirectory();
 142  
                         }
 143  
                 };
 144  
         }
 145  
         
 146  
         public static FilenameFilter getExtensionFilter(final boolean includeHiddenDirectories, final String... extensions){
 147  0
                 return new FilenameFilter(){
 148  
                         public boolean accept(File dir, String name) {
 149  0
                                 for (String string : extensions) {
 150  0
                                         if (name.toLowerCase().endsWith(string.toLowerCase()))
 151  0
                                                 return (includeHiddenDirectories || !name.startsWith(".")) && new File(dir.getAbsoluteFile() + File.separator + name).isFile();                                                
 152  
                                 }
 153  0
                                 return false;
 154  
                         }
 155  
                 };
 156  
         }
 157  
 }