001 /** 002 * Copyright 2010-2012 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.codehaus.mojo.license; 017 018 /* 019 * Codehaus License Maven Plugin 020 * 021 * This program is free software: you can redistribute it and/or modify 022 * it under the terms of the GNU Lesser General Public License as published by 023 * the Free Software Foundation, either version 3 of the License, or 024 * (at your option) any later version. 025 * 026 * This program is distributed in the hope that it will be useful, 027 * but WITHOUT ANY WARRANTY; without even the implied warranty of 028 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 029 * GNU Lesser General Public License for more details. 030 * 031 * You should have received a copy of the GNU Lesser General Public License 032 * along with this program. If not, see <http://www.gnu.org/licenses/lgpl-3.0.html>. 033 */ 034 035 import org.codehaus.plexus.util.FileUtils; 036 import org.codehaus.plexus.util.IOUtil; 037 038 import java.io.*; 039 040 /** 041 * Some basic file io utilities 042 * 043 * @author pgier 044 * @author tchemit <chemit@codelutin.com> 045 * @since 1.0 046 */ 047 public class FileUtil 048 { 049 050 public static void tryClose( InputStream is ) 051 { 052 if ( is == null ) 053 { 054 return; 055 } 056 try 057 { 058 is.close(); 059 } 060 catch ( IOException e ) 061 { 062 // do nothing 063 } 064 } 065 066 public static void tryClose( OutputStream os ) 067 { 068 if ( os == null ) 069 { 070 return; 071 } 072 try 073 { 074 os.close(); 075 } 076 catch ( IOException e ) 077 { 078 // do nothing 079 } 080 } 081 082 /** 083 * Creates the directory (and his parents) if necessary. 084 * 085 * @param dir the directory to create if not exisiting 086 * @return {@code true} if directory was created, {@code false} if was no 087 * need to create it 088 * @throws IOException if could not create directory 089 */ 090 public static boolean createDirectoryIfNecessary( File dir ) 091 throws IOException 092 { 093 if ( !dir.exists() ) 094 { 095 boolean b = dir.mkdirs(); 096 if ( !b ) 097 { 098 throw new IOException( "Could not create directory " + dir ); 099 } 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 }