r2312 - trunk/nuiton-utils/src/main/java/org/nuiton/util
Author: tchemit Date: 2012-03-22 21:40:13 +0100 (Thu, 22 Mar 2012) New Revision: 2312 Url: http://nuiton.org/repositories/revision/nuiton-utils/2312 Log: fixes #2007 : Deprecate methods that duplicated commons-io in FileUtils Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/FileUtil.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/FileUtil.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/FileUtil.java 2012-03-22 13:07:27 UTC (rev 2311) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/FileUtil.java 2012-03-22 20:40:13 UTC (rev 2312) @@ -36,6 +36,8 @@ package org.nuiton.util; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -56,6 +58,7 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintStream; +import java.io.Reader; import java.nio.CharBuffer; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; @@ -81,7 +84,7 @@ * Created: 22 novembre 2004 * * @author bpoussin <poussin@codelutin.com> - * $Id$ + * $Id$ */ public class FileUtil { // FileUtil @@ -90,19 +93,21 @@ /** Encoding utilisé (peut être redéfini) */ // TODO fdesbois 2011-04-16 : Perhaps change ISO encoding by UTF-8 - static public String ENCODING = "ISO-8859-1"; - static protected File currentDirectory = new File("."); + public static String ENCODING = "ISO-8859-1"; - static public void setCurrentDirectory(File dir) { + protected static File currentDirectory = new File("."); + + public static void setCurrentDirectory(File dir) { currentDirectory = dir; } - static public File getCurrentDirectory() { + public static File getCurrentDirectory() { return currentDirectory; } - static public class PatternChooserFilter extends javax.swing.filechooser.FileFilter { + public static class PatternChooserFilter extends javax.swing.filechooser.FileFilter { protected String pattern; + protected String description; public PatternChooserFilter(String pattern, String description) { @@ -132,7 +137,7 @@ * @return le fichier accepté, ou null si rien n'est chois ou l'utilisateur a annulé * @see #getFile(javax.swing.filechooser.FileFilter...) */ - static public File getFile(String... patternOrDescriptionFilters) { + public static File getFile(String... patternOrDescriptionFilters) { File result; result = getFile(null, patternOrDescriptionFilters); return result; @@ -145,22 +150,22 @@ * @param filters les filtres a ajouter * @return le fichier accepté, ou null si rien n'est chois ou l'utilisateur a annulé */ - static public File getFile(javax.swing.filechooser.FileFilter... filters) { + public static File getFile(javax.swing.filechooser.FileFilter... filters) { File result = getFile(null, filters); return result; } - /** + /** * Retourne le nom du fichier entre dans la boite de dialogue. * Si le bouton annuler est utilisé, ou qu'il y a une erreur retourne null. * - * @param parent le component parent du dialog - * @param patternOrDescriptionFilters les filtres a utiliser, les chaines doivent etre données + * @param parent le component parent du dialog + * @param patternOrDescriptionFilters les filtres a utiliser, les chaines doivent etre données * par deux, le pattern du filtre + la description du filtre * @return le fichier accepté, ou null si rien n'est chois ou l'utilisateur a annulé * @see #getFile(javax.swing.filechooser.FileFilter...) */ - static public File getFile(Component parent, + public static File getFile(Component parent, String... patternOrDescriptionFilters) { File result; result = getFile("Ok", "Ok", parent, patternOrDescriptionFilters); @@ -171,24 +176,26 @@ * Retourne le nom du fichier entre dans la boite de dialogue. * Si le bouton annuler est utilisé, ou qu'il y a une erreur retourne null. * - * @param title le titre de la boite de dialogue - * @param approvalText le label du boutton d'acceptation - * @param parent le component parent du dialog + * @param title le titre de la boite de dialogue + * @param approvalText le label du boutton d'acceptation + * @param parent le component parent du dialog * @param patternOrDescriptionFilters les filtres a utiliser, les chaines doivent etre données * par deux, le pattern du filtre + la description du filtre * @return le fichier accepté, ou null si rien n'est chois ou l'utilisateur a annulé * @see #getFile(javax.swing.filechooser.FileFilter...) */ - static public File getFile(String title, + public static File getFile(String title, String approvalText, Component parent, String... patternOrDescriptionFilters) { if (patternOrDescriptionFilters.length % 2 != 0) { - throw new IllegalArgumentException("Arguments must be (pattern, description) couple"); + throw new IllegalArgumentException( + "Arguments must be (pattern, description) couple"); } javax.swing.filechooser.FileFilter[] filters = - new javax.swing.filechooser.FileFilter[patternOrDescriptionFilters.length / 2]; + new javax.swing.filechooser.FileFilter[ + patternOrDescriptionFilters.length / 2]; for (int i = 0; i < filters.length; i++) { String pattern = patternOrDescriptionFilters[i * 2]; String description = patternOrDescriptionFilters[i * 2 + 1]; @@ -199,16 +206,16 @@ return result; } - + /** * Retourne le nom du fichier entre dans la boite de dialogue. * Si le bouton annuler est utilisé, ou qu'il y a une erreur retourne null. * - * @param parent le component parent du dialog + * @param parent le component parent du dialog * @param filters les filtres a ajouter * @return le fichier accepté, ou null si rien n'est chois ou l'utilisateur a annulé */ - static public File getFile(Component parent, + public static File getFile(Component parent, javax.swing.filechooser.FileFilter... filters) { File result = getFile("Ok", "Ok", parent, filters); return result; @@ -218,13 +225,13 @@ * Retourne le nom du fichier entre dans la boite de dialogue. * Si le bouton annuler est utilisé, ou qu'il y a une erreur retourne null. * - * @param title le titre de la boite de dialogue + * @param title le titre de la boite de dialogue * @param approvalText le label du boutton d'acceptation - * @param parent le component parent du dialog - * @param filters les filtres a ajouter + * @param parent le component parent du dialog + * @param filters les filtres a ajouter * @return le fichier accepté, ou null si rien n'est chois ou l'utilisateur a annulé */ - static public File getFile(String title, + public static File getFile(String title, String approvalText, Component parent, javax.swing.filechooser.FileFilter... filters) { @@ -251,8 +258,7 @@ return theFile.getAbsoluteFile(); } } - } - catch (Exception eee) { + } catch (Exception eee) { log.warn("Erreur:", eee); } return null; @@ -263,10 +269,10 @@ * Si le bouton annuler est utilisé, ou qu'il y a une erreur retourne * null. */ - static public String getDirectory() { - return getDirectory(null,"Ok", "Ok"); + public static String getDirectory() { + return getDirectory(null, "Ok", "Ok"); } - + /** * @param title le nom de la boite de dialogue * @param approvalText le texte de l'action d'acceptation du répertoire dans le file chooser @@ -274,20 +280,20 @@ * Si le bouton annuler est utilisé, ou qu'il y a une erreur retourne * null. */ - static public String getDirectory(String title, String approvalText) { + public static String getDirectory(String title, String approvalText) { String result = getDirectory(null, title, approvalText); return result; } /** - * @param parent le component parent du dialog + * @param parent le component parent du dialog * @param title le nom de la boite de dialogue * @param approvalText le texte de l'action d'acceptation du répertoire dans le file chooser * @return le nom du repertoire entre dans la boite de dialogue. * Si le bouton annuler est utilisé, ou qu'il y a une erreur retourne * null. */ - static public String getDirectory(Component parent, + public static String getDirectory(Component parent, String title, String approvalText) { try { @@ -319,8 +325,10 @@ * @param file le fichier source à convertire * @return le contenu du fichier sous la forme d'un tableau de bytes. * @throws IOException if any io pb + * @deprecated since 2.4.6, prefer use method {@link IOUtils#toByteArray(Reader)}. */ - static public byte[] fileToByte(File file) throws IOException { + @Deprecated + public static byte[] fileToByte(File file) throws IOException { ByteArrayOutputStream result; InputStream in = new BufferedInputStream(new FileInputStream(file)); try { @@ -345,10 +353,12 @@ * @param src the incoming stream to grab * @param dst the dst file * @return the file filled by incoming input stream - * @throws IOException if any io pb + * @throws IOException if any io pb * @throws NullPointerException if src or dst parameter is null + * @deprecated since 2.4.6, prefer use method {@link FileUtils#copyInputStreamToFile(InputStream, File)}. */ - static public File inputStreamToFile(InputStream src, File dst) throws IOException, NullPointerException { + @Deprecated + public static File inputStreamToFile(InputStream src, File dst) throws IOException, NullPointerException { if (src == null) { throw new NullPointerException("parameter 'src' can not be null"); } @@ -378,7 +388,7 @@ * @return le fichier temporaire contenant les bytes * @throws IOException if any io pb */ - static public File byteToFile(byte[] bytes) throws IOException { + public static File byteToFile(byte[] bytes) throws IOException { File file = File.createTempFile("FileUtil-byteToFile", ".tmp"); byteToFile(bytes, file); return file; @@ -391,8 +401,10 @@ * @param file le fichier dans lequel il faut ecrire les bytes * @return le fichier passé en parametre * @throws IOException if any io pb + * @deprecated since 2.4.6, prefer use method {@link FileUtils#writeByteArrayToFile(File, byte[])} */ - static public File byteToFile(byte[] bytes, + @Deprecated + public static File byteToFile(byte[] bytes, File file) throws IOException { OutputStream out = new BufferedOutputStream(new FileOutputStream(file)); try { @@ -410,7 +422,7 @@ * @return the reader on the given file * @throws IOException if any io pb */ - static public BufferedReader getReader(File file) throws IOException { + public static BufferedReader getReader(File file) throws IOException { return getReader(file, ENCODING); } @@ -423,7 +435,7 @@ * @return the buffered reader in the given encoding * @throws IOException if any io pb */ - static public BufferedReader getReader(File file, + public static BufferedReader getReader(File file, String encoding) throws IOException { FileInputStream inf = new FileInputStream(file); InputStreamReader in = new InputStreamReader(inf, encoding); @@ -438,7 +450,7 @@ * @return the writer on the given file * @throws IOException if any io pb */ - static public BufferedWriter getWriter(File file) throws IOException { + public static BufferedWriter getWriter(File file) throws IOException { return getWriter(file, ENCODING); } @@ -451,7 +463,7 @@ * @return the buffered writer on the given file with given encoding * @throws IOException if any io pb */ - static public BufferedWriter getWriter(File file, + public static BufferedWriter getWriter(File file, String encoding) throws IOException { FileOutputStream outf = new FileOutputStream(file); OutputStreamWriter out = new OutputStreamWriter(outf, encoding); @@ -471,7 +483,7 @@ * @return le fichier pointant sur le nouveau repertoire * @throws IOException if any io pb */ - static public File createTempDirectory(String prefix, + public static File createTempDirectory(String prefix, String suffix, File tmpdir) throws IOException { if (tmpdir == null) { @@ -496,7 +508,7 @@ * @return the temprary created file * @throws IOException if any io pb */ - static public File createTempDirectory(String prefix, + public static File createTempDirectory(String prefix, String suffix) throws IOException { return createTempDirectory(prefix, suffix, null); } @@ -507,8 +519,10 @@ * @param f1 the first file * @param f2 the second file * @return vrai si f1 est plus recent que f2 + * @deprecated since 2.4.6, prefer use method {@link FileUtils#isFileNewer(File, File)}. */ - static public boolean isNewer(File f1, File f2) { + @Deprecated + public static boolean isNewer(File f1, File f2) { boolean result = f1.lastModified() > f2.lastModified(); return result; } @@ -516,15 +530,17 @@ /** * Permet de lire un fichier et de retourner sont contenu sous forme d'une * chaine de carateres. - * + * <p/> * Warning, this method use {@link #ENCODING} to read file (by default * in ISO). * * @param file le fichier a lire * @return le contenu du fichier * @throws IOException if any io pb + * @deprecated since 2.4.6, prefer use method {@link FileUtils#readFileToString(File)}. */ - static public String readAsString(File file) throws IOException { + @Deprecated + public static String readAsString(File file) throws IOException { StringBuffer result = new StringBuffer(); char[] cbuf = new char[2000]; BufferedReader in = getReader(file); @@ -539,15 +555,17 @@ /** * Permet de sauver une chaine directement dans un fichier - * + * <p/> * Warning, this method use {@link #ENCODING} to read file (by default * in ISO). - * + * * @param file Le fichier dans lequel il faut ecrire la chaine * @param content Le texte a ecrire dans le fichier * @throws IOException if any pb while writing + * @deprecated since 2.4.6, prefer use method {@link FileUtils#writeStringToFile(File, String)}. */ - static public void writeString(File file, + @Deprecated + public static void writeString(File file, String content) throws IOException { //fixme on doit tester le retour de la méthode, car il se peut que le répertoire // ne puisse etre cree. @@ -570,8 +588,10 @@ * @param content Le texte a ecrire dans le fichier * @param encoding encoding to use * @throws IOException if any pb while writing + * @deprecated since 2.4.6, prefer use method {@link FileUtils#writeStringToFile(File, String, String)}. */ - static public void writeString(File file, + @Deprecated + public static void writeString(File file, String content, String encoding) throws IOException { //fixme on doit tester le retour de la méthode, car il se peut que le répertoire @@ -596,7 +616,7 @@ * @return le fichier qui contient content * @throws IOException if any io pb */ - static public File getTempFile(String content) throws IOException { + public static File getTempFile(String content) throws IOException { return getTempFile(content, ""); } @@ -609,7 +629,7 @@ * @return le fichier qui contient content * @throws IOException if any io pb */ - static public File getTempFile(String content, + public static File getTempFile(String content, String fileSuffix) throws IOException { File result = File.createTempFile("tmp-" + FileUtil.class.getName(), fileSuffix); @@ -627,7 +647,7 @@ * s'il est trouvé * @return le nom du fichier sans le suffixe si trouvé. */ - static public String basename(File file, String... suffixes) { + public static String basename(File file, String... suffixes) { String result = basename(file.getName(), suffixes); return result; } @@ -642,7 +662,7 @@ * @return le nom du fichier sans le suffixe si trouvé. * @since 1.4.2 */ - static public String basename(String name, String... suffixes) { + public static String basename(String name, String... suffixes) { String result = name; for (String suffixe : suffixes) { if (result.endsWith(suffixe)) { @@ -663,7 +683,7 @@ * @return l'extension ou la chaine vide si le fichier n'a pas d'extension * l'extension ne contient pas le chaine de delimitation */ - static public String extension(File file, String... extchars) { + public static String extension(File file, String... extchars) { String name = file.getName(); String result = extension(name, extchars); return result; @@ -672,14 +692,14 @@ /** * Permet de récupérer l'extension d'un nom de fichier * - * @param name le nom du fichier dont on souhaite l'extension + * @param name le nom du fichier dont on souhaite l'extension * @param extchars la liste des caracteres pouvant former l'extension * dans l'ordre de preference. Si vide on utilise ".". * @return l'extension ou la chaine vide si le fichier n'a pas d'extension * l'extension ne contient pas le chaine de delimitation * @since 1.4.2 */ - static public String extension(String name, String... extchars) { + public static String extension(String name, String... extchars) { String result = ""; if (extchars.length == 0) { @@ -706,7 +726,7 @@ * @throws IOException si aucune extension trouvé dans le fichier d'origine * @since 1.4.2 */ - static public File changeExtension(File file, + public static File changeExtension(File file, String newExtension, String... extchars) throws IOException { String name = file.getName(); @@ -725,7 +745,7 @@ * @throws IOException si aucune extension trouvé dans le fichier d'origine * @since 1.4.2 */ - static public String changeExtension(String name, + public static String changeExtension(String name, String newExtension, String... extchars) throws IOException { String extension = extension(name, extchars); @@ -744,13 +764,13 @@ * l'arborescence de {@code inputDirectory} dans le répertoire * {@code ouputDirectory}. * - * @param inputDirectory le répertoire de départ + * @param inputDirectory le répertoire de départ * @param outputDirectory le répertoire cible - * @param file le fichier + * @param file le fichier * @return le fichier mirroir dans le répertoire cible * @since 1.4.2 */ - static public File getRelativeFile(File inputDirectory, + public static File getRelativeFile(File inputDirectory, File outputDirectory, File file) { String inputPath = inputDirectory.getAbsolutePath(); @@ -873,7 +893,9 @@ * @param directory le chemin du répertoire à supprimer * @return vrai si tout se passe bien, false si la suppression d'un élement * se passe mal + * @deprecated since 2.4.6, prefer use method {@link FileUtils#deleteDirectory(File)}. */ + @Deprecated public static boolean deleteRecursively(String directory) { return deleteRecursively(new File(directory)); } @@ -884,7 +906,9 @@ * @param directory le répertoire à supprimer * @return vrai si tout se passe bien, false si la suppression d'un élement * se passe mal + * @deprecated since 2.4.6, prefer use method {@link FileUtils#deleteDirectory(File)}. */ + @Deprecated public static boolean deleteRecursively(File directory) { return walkBefore(directory, new FileAction() { @Override @@ -944,7 +968,9 @@ * @param source le fichier source * @param target le fichier cible * @throws IOException Erreur de copie + * @deprecated since 2.4.6, prefer use method {@link FileUtils#copyFile(File,File)}. */ + @Deprecated public static void copy(File source, File target) throws IOException { //fixme on doit tester le retour de la méthode, car il se peut que le répertoire // ne puisse être copié. @@ -993,7 +1019,7 @@ * fichiers/repertoires pour etre copié. Si vide alors tout est copié * @throws IOException if any io pb */ - static public void copyRecursively(File srcDir, + public static void copyRecursively(File srcDir, File destDir, String... includePatterns) throws IOException { copyAndRenameRecursively(srcDir, destDir, null, null, includePatterns); @@ -1014,7 +1040,7 @@ * fichiers/repertoires pour etre copié. Si vide alors tout est copié * @throws IOException if any io pb */ - static public void copyAndRenameRecursively(File srcDir, File destDir, + public static void copyAndRenameRecursively(File srcDir, File destDir, String renameFrom, String renameTo, String... includePatterns) throws IOException { @@ -1046,7 +1072,7 @@ * fichiers/repertoires pour etre copié. Si vide alors tout est copié * @throws IOException if any io pb */ - static public void copyAndRenameRecursively(File srcDir, + public static void copyAndRenameRecursively(File srcDir, File destDir, boolean includeSrcDir, String renameFrom, @@ -1089,13 +1115,14 @@ /** * Get a ByteArrayOutputStream containing all data that could be read from the given InputStream - * @param inputStream the stream to read + * + * @param inputStream the stream to read * @param defaultBufferSize the buffer size * @return the input stream read for input * @throws IOException if any pb while reading or writing */ public static ByteArrayOutputStream readBytesFrom(InputStream inputStream, - int defaultBufferSize) throws IOException { + int defaultBufferSize) throws IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream( defaultBufferSize); @@ -1111,7 +1138,7 @@ } /** - * @param file le fichier à tester. + * @param file le fichier à tester. * @param includePatterns les patterns pour accepeter le fichier depuis son nom * @return <code>true</code> si le fichier est accepté, <code>false> autrement. */ @@ -1127,26 +1154,26 @@ } return result; } - + /** * Use the linePattern to break the given CharBuffer into lines, applying * the input pattern to each line to see if we have a match - * + * <p/> * Code taken from : - * + * <p/> * http://java.sun.com/javase/6/docs/technotes/guides/io/example/Grep.java - * + * * @param regex regex to search into file - * @param cb nio buffer + * @param cb nio buffer * @return matching lines (or {code null} if no matching lines) * @throws IOException * @since 1.1.2 */ protected static List<CharSequence> grep(String regex, CharBuffer cb) { - + List<CharSequence> linesList = null; - + Pattern pattern = Pattern.compile(regex); Pattern linePattern = Pattern.compile(".*\r?\n"); @@ -1159,8 +1186,7 @@ CharSequence cs = lm.group(); // The current line if (pm == null) { pm = pattern.matcher(cs); - } - else { + } else { pm.reset(cs); } if (pm.find()) { @@ -1174,23 +1200,23 @@ break; } } - + return linesList; } - + /** * Java implementation for the unix grep command. - * + * <p/> * Code taken from : - * + * <p/> * http://java.sun.com/javase/6/docs/technotes/guides/io/example/Grep.java - * + * <p/> * May fail on windows with error : * The requested operation cannot be performed on a file with a user-mapped section open - * + * * @param searchRegex regex to search into file - * @param f file to search into - * @param encoding encoding to use + * @param f file to search into + * @param encoding encoding to use * @return matching lines (or {code null} if no matching lines) * @throws IOException * @since 1.1.2 @@ -1210,7 +1236,7 @@ fc = fis.getChannel(); // Get the file's size and then map it into memory - int sz = (int)fc.size(); + int sz = (int) fc.size(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz); // Decode the file into a char buffer @@ -1220,8 +1246,7 @@ // Perform the search lines = grep(searchRegex, cb); - } - finally { + } finally { // Close the channel and the stream if (fc != null) { fc.close(); @@ -1236,14 +1261,14 @@ /** * Java implementation for the unix grep command. - * + * <p/> * May fail on windows with error : * The requested operation cannot be performed on a file with a user-mapped section open - * - * @param searchRegex regex to search into file + * + * @param searchRegex regex to search into file * @param rootDirectory directory to seacrh into - * @param fileRegex regex for file to find in {@code rootDirectory} - * @param encoding encoding to use + * @param fileRegex regex for file to find in {@code rootDirectory} + * @param encoding encoding to use * @return all matching lines for each files * @throws IOException * @since 1.1.2 @@ -1266,13 +1291,13 @@ /** * Search for files matching regex in current directory. - * + * <p/> * May fail on windows with error : * The requested operation cannot be performed on a file with a user-mapped section open - * + * * @param searchRegex regex to search into file - * @param fileRegex regex for file to find in current dir - * @param encoding encoding to use + * @param fileRegex regex for file to find in current dir + * @param encoding encoding to use * @return all matching lines for each files * @throws IOException * @since 1.1.2 @@ -1289,16 +1314,16 @@ /** * Sed implementation for a single file. - * + * <p/> * Oginal source code from http://kickjava.com/src/org/apache/lenya/util/SED.java.htm. - * + * <p/> * May fail on windows with error : * The requested operation cannot be performed on a file with a user-mapped section open - * + * * @param searchRegex Prefix which shall be replaced - * @param replace Prefix which is going to replace the original - * @param file File which sed shall be applied - * @param encoding charset encoding + * @param replace Prefix which is going to replace the original + * @param file File which sed shall be applied + * @param encoding charset encoding * @throws IOException * @since 1.1.2 */ @@ -1316,7 +1341,7 @@ FileChannel fc = fis.getChannel(); // Get the file's size and then map it into memory - int sz = (int)fc.size(); + int sz = (int) fc.size(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz); // Decode the file into a char buffer @@ -1343,15 +1368,15 @@ /** * Java implementation for the unix sed command. - * + * <p/> * May fail on windows with error : * The requested operation cannot be performed on a file with a user-mapped section open - * - * @param searchRegex regex to search into file - * @param replace string to replace matching patterns + * + * @param searchRegex regex to search into file + * @param replace string to replace matching patterns * @param rootDirectory directory to search into - * @param fileRegex regex for file to find in {@code rootDirectory} - * @param encoding encoding to use + * @param fileRegex regex for file to find in {@code rootDirectory} + * @param encoding encoding to use * @throws IOException * @since 1.1.2 */ @@ -1368,14 +1393,14 @@ /** * Java implementation for the unix sed command. - * + * <p/> * May fail on windows with error : * The requested operation cannot be performed on a file with a user-mapped section open - * + * * @param searchRegex regex to search into file - * @param replace string to replace matching patterns - * @param fileRegex regex for file to find in current dir - * @param encoding encoding to use + * @param replace string to replace matching patterns + * @param fileRegex regex for file to find in current dir + * @param encoding encoding to use * @throws IOException * @since 1.1.2 */ @@ -1447,5 +1472,5 @@ File result = getFileFromPaths(rootDirectory, paths); return result; } - + } // FileUtil
participants (1)
-
tchemit@users.nuiton.org