1 /**
2 * Copyright 2010-2012 The Kuali Foundation
3 *
4 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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 package org.codehaus.mojo.license;
17
18 /*
19 * Codehaus License Maven Plugin
20 *
21 * This program is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU Lesser General Public License as published by
23 * the Free Software Foundation, either version 3 of the License, or
24 * (at your option) any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU Lesser General Public License for more details.
30 *
31 * You should have received a copy of the GNU Lesser General Public License
32 * along with this program. If not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
33 */
34
35 import org.codehaus.plexus.util.FileUtils;
36 import org.codehaus.plexus.util.IOUtil;
37
38 import java.io.*;
39
40 /**
41 * Some basic file io utilities
42 *
43 * @author pgier
44 * @author tchemit <chemit@codelutin.com>
45 * @since 1.0
46 */
47 public class FileUtil
48 {
49
50 public static void tryClose( InputStream is )
51 {
52 if ( is == null )
53 {
54 return;
55 }
56 try
57 {
58 is.close();
59 }
60 catch ( IOException e )
61 {
62 // do nothing
63 }
64 }
65
66 public static void tryClose( OutputStream os )
67 {
68 if ( os == null )
69 {
70 return;
71 }
72 try
73 {
74 os.close();
75 }
76 catch ( IOException e )
77 {
78 // do nothing
79 }
80 }
81
82 /**
83 * Creates the directory (and his parents) if necessary.
84 *
85 * @param dir the directory to create if not exisiting
86 * @return {@code true} if directory was created, {@code false} if was no
87 * need to create it
88 * @throws IOException if could not create directory
89 */
90 public static boolean createDirectoryIfNecessary( File dir )
91 throws IOException
92 {
93 if ( !dir.exists() )
94 {
95 boolean b = dir.mkdirs();
96 if ( !b )
97 {
98 throw new IOException( "Could not create directory " + dir );
99 }
100 return true;
101 }
102 return false;
103 }
104
105 public static boolean createNewFile( File file )
106 throws IOException
107 {
108 createDirectoryIfNecessary( file.getParentFile() );
109 if ( !file.exists() )
110 {
111 boolean b = file.createNewFile();
112 if ( !b )
113 {
114 throw new IOException( "Could not create new file " + file );
115 }
116 return true;
117 }
118 return false;
119 }
120
121 /**
122 * Delete the given file.
123 *
124 * @param file the file to delete
125 * @throws IOException if could not delete the file
126 */
127 public static void deleteFile( File file )
128 throws IOException
129 {
130 if ( !file.exists() )
131 {
132 // file does not exist, can not delete it
133 return;
134 }
135 boolean b = file.delete();
136 if ( !b )
137 {
138 throw new IOException( "could not delete file " + file );
139 }
140 }
141
142 /**
143 * Rename the given file to a new destination.
144 *
145 * @param file the file to rename
146 * @param destination the destination file
147 * @throws IOException if could not rename the file
148 */
149 public static void renameFile( File file, File destination )
150 throws IOException
151 {
152 boolean b = file.renameTo( destination );
153 if ( !b )
154 {
155 throw new IOException( "could not rename " + file + " to " + destination );
156 }
157 }
158
159 /**
160 * Copy a file to a given locationand logging.
161 *
162 * @param source represents the file to copy.
163 * @param target file name of destination file.
164 * @throws IOException if could not copy file.
165 */
166 public static void copyFile( File source, File target )
167 throws IOException
168 {
169 createDirectoryIfNecessary( target.getParentFile() );
170 FileUtils.copyFile( source, target );
171 }
172
173 public static File getFile( File base, String... paths )
174 {
175 StringBuilder buffer = new StringBuilder();
176 for ( String path : paths )
177 {
178 buffer.append( File.separator ).append( path );
179 }
180 return new File( base, buffer.substring( 1 ) );
181 }
182
183 /**
184 * @param file the source file
185 * @return the backup file
186 */
187 public static File getBackupFile( File file )
188 {
189 return new File( file.getAbsolutePath() + "~" );
190 }
191
192 /**
193 * Backups the given file using the {@link FileUtil#getBackupFile(File)} as
194 * destination file.
195 *
196 * @param f the file to backup
197 * @throws IOException if any pb while copying the file
198 */
199 public static void backupFile( File f )
200 throws IOException
201 {
202 File dst = FileUtil.getBackupFile( f );
203 copyFile( f, dst );
204 }
205
206 /**
207 * Permet de lire un fichier et de retourner sont contenu sous forme d'une
208 * chaine de carateres
209 *
210 * @param file le fichier a lire
211 * @param encoding encoding to read file
212 * @return the content of the file
213 * @throws IOException if IO pb
214 */
215 static public String readAsString( File file, String encoding )
216 throws IOException
217 {
218 FileInputStream inf = new FileInputStream( file );
219 BufferedReader in = new BufferedReader( new InputStreamReader( inf, encoding ) );
220 try
221 {
222 return IOUtil.toString( in );
223 }
224 finally
225 {
226 in.close();
227 }
228 }
229
230 /**
231 * Sauvegarde un contenu dans un fichier.
232 *
233 * @param file le fichier a ecrire
234 * @param content le contenu du fichier
235 * @param encoding l'encoding d'ecriture
236 * @throws IOException if IO pb
237 */
238 public static void writeString( File file, String content, String encoding )
239 throws IOException
240 {
241 createDirectoryIfNecessary( file.getParentFile() );
242 BufferedWriter out;
243 out = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( file ), encoding ) );
244 try
245 {
246 IOUtil.copy( content, out );
247 }
248 finally
249 {
250 out.close();
251 }
252 }
253 }