FileUtils.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  *
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  *
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.util;
022 
023 import java.io.*;
024 
025 /**
026  * FileUtils provides some simple helper methods for working with files. It follows the convention of wrapping all
027  * checked exceptions as runtimes, so code using these methods is free of try-catch blocks but does not expect to
028  * recover from errors.
029  *
030  <p/><table id="crc"><caption>CRC Card</caption>
031  <tr><th> Responsibilities <th> Collaborations
032  <tr><td> Read a text file as a string.
033  <tr><td> Open a file or default resource as an input stream.
034  </table>
035  */
036 public class FileUtils
037 {
038     /**
039      * Reads a text file as a string.
040      *
041      @param filename The name of the file.
042      *
043      @return The contents of the file.
044      */
045     public static String readFileAsString(String filename)
046     {
047         BufferedInputStream is = null;
048 
049         try{
050             try
051             {
052                 is = new BufferedInputStream(new FileInputStream(filename));
053             }
054             catch (FileNotFoundException e)
055             {
056                 throw new RuntimeException(e);
057             }
058 
059             return readStreamAsString(is);
060         }finally {
061             if (is != null)
062             {
063                 try
064                 {
065                     is.close();
066                 }
067                 catch (IOException e)
068                 {
069                     throw new RuntimeException(e);
070                 }
071             }
072         }
073     }
074 
075     /**
076      * Reads a text file as a string.
077      *
078      @param file The file.
079      *
080      @return The contents of the file.
081      */
082     public static String readFileAsString(File file)
083     {
084         BufferedInputStream is = null;
085 
086         try
087         {
088             is = new BufferedInputStream(new FileInputStream(file));
089         }
090         catch (FileNotFoundException e)
091         {
092             throw new RuntimeException(e);
093         }
094 
095         return readStreamAsString(is);
096     }
097 
098     /**
099      * Reads the contents of a reader, one line at a time until the end of stream is encountered, and returns all
100      * together as a string.
101      *
102      @param is The reader.
103      *
104      @return The contents of the reader.
105      */
106     private static String readStreamAsString(BufferedInputStream is)
107     {
108         try
109         {
110             byte[] data = new byte[4096];
111 
112             StringBuffer inBuffer = new StringBuffer();
113 
114             String line;
115             int read;
116 
117             while ((read = is.read(data)) != -1)
118             {
119                 String s = new String(data, 0, read);
120                 inBuffer.append(s);
121             }
122 
123             return inBuffer.toString();
124         }
125         catch (IOException e)
126         {
127             throw new RuntimeException(e);
128         }
129     }
130 
131     /**
132      * Either opens the specified filename as an input stream, or uses the default resource loaded using the
133      * specified class loader, if opening the file fails or no file name is specified.
134      *
135      @param filename        The name of the file to open.
136      @param defaultResource The name of the default resource on the classpath if the file cannot be opened.
137      @param cl              The classloader to load the default resource with.
138      *
139      @return An input stream for the file or resource, or null if one could not be opened.
140      */
141     public static InputStream openFileOrDefaultResource(String filename, String defaultResource, ClassLoader cl)
142     {
143         InputStream is = null;
144 
145         // Flag to indicate whether the default resource should be used. By default this is true, so that the default
146         // is used when opening the file fails.
147         boolean useDefault = true;
148 
149         // Try to open the file if one was specified.
150         if (filename != null)
151         {
152             try
153             {
154                 is = new BufferedInputStream(new FileInputStream(new File(filename)));
155 
156                 // Clear the default flag because the file was succesfully opened.
157                 useDefault = false;
158             }
159             catch (FileNotFoundException e)
160             {
161                 // Ignore this exception, the default will be used instead.
162             }
163         }
164 
165         // Load the default resource if a file was not specified, or if opening the file failed.
166         if (useDefault)
167         {
168             is = cl.getResourceAsStream(defaultResource);
169         }
170 
171         return is;
172     }
173 
174     /**
175      * Copies the specified source file to the specified destintaion file. If the destinationst file does not exist,
176      * it is created.
177      *
178      @param src The source file name.
179      @param dst The destination file name.
180      */
181     public static void copy(File src, File dst)
182     {
183         try
184         {
185             InputStream in = new FileInputStream(src);
186             if (!dst.exists())
187             {
188                 dst.createNewFile();
189             }
190 
191             OutputStream out = new FileOutputStream(dst);
192 
193             // Transfer bytes from in to out
194             byte[] buf = new byte[1024];
195             int len;
196             while ((len = in.read(buf)) 0)
197             {
198                 out.write(buf, 0, len);
199             }
200 
201             in.close();
202             out.close();
203         }
204         catch (IOException e)
205         {
206             throw new RuntimeException(e);
207         }
208     }
209 
210     /*
211      * Deletes a given file
212      */
213      public static boolean deleteFile(String filePath)
214      {
215          return delete(new File(filePath)false);
216      }
217 
218     /*
219      * Deletes a given empty directory 
220      */
221      public static boolean deleteDirectory(String directoryPath)
222      {
223          File directory = new File(directoryPath);
224 
225          if (directory.isDirectory())
226          {
227            if (directory.listFiles().length == 0)
228            {
229                return delete(directory, true);
230            }
231          }
232 
233          return false;
234      }
235 
236      /**
237       * Delete a given file/directory,
238       * A directory will always require the recursive flag to be set.
239       * if a directory is specified and recursive set then delete the whole tree
240       @param file the File object to start at
241       @param recursive boolean to recurse if a directory is specified.
242       @return <code>true</code> if and only if the file or directory is
243       *          successfully deleted; <code>false</code> otherwise
244       */
245      public static boolean delete(File file, boolean recursive)
246      {
247          boolean success = true;
248 
249          if (file.isDirectory())
250          {
251              if (recursive)
252              {
253                  for (File subFile : file.listFiles())
254                  {
255                      success = delete(subFile, true& success ;
256                  }
257 
258                  return file.delete();
259              }
260 
261              return false;
262          }
263 
264          return success && file.delete();
265      }
266 
267 
268     public static class UnableToCopyException extends Exception
269     {
270         UnableToCopyException(String msg)
271         {
272             super(msg);
273         }
274     }
275 
276     public static void copyRecursive(File source, File dstthrows FileNotFoundException, UnableToCopyException
277     {
278 
279         if (!source.exists())
280         {
281             throw new FileNotFoundException("Unable to copy '" + source.toString() "' as it does not exist.");
282         }
283 
284         if (dst.exists() && !dst.isDirectory())
285         {
286             throw new IllegalArgumentException("Unable to copy '" + source.toString() "' to '" + dst + "' a file with same name exists.");
287         }
288 
289 
290         if (source.isFile())
291         {
292             copy(source, dst);
293         }
294 
295         //else we have a source directory
296         if (!dst.isDirectory() && !dst.mkdir())
297         {
298              throw new UnableToCopyException("Unable to create destination directory");
299         }
300 
301 
302         for (File file : source.listFiles())
303         {
304            if (file.isFile())
305            {
306                copy(file, new File(dst.toString() + File.separator + file.getName()));
307            }
308            else
309            {
310                copyRecursive(file, new File(dst + File.separator + file.getName()));
311            }
312         }
313 
314 
315     }
316 }