Index: lutinutil/src/java/org/codelutin/util/IOUtils.java diff -u lutinutil/src/java/org/codelutin/util/IOUtils.java:1.1 lutinutil/src/java/org/codelutin/util/IOUtils.java:1.2 --- lutinutil/src/java/org/codelutin/util/IOUtils.java:1.1 Thu Oct 14 19:39:03 2004 +++ lutinutil/src/java/org/codelutin/util/IOUtils.java Mon Nov 22 16:14:10 2004 @@ -1,60 +1,60 @@ -package org.codelutin.util; +package org.codelutin.util; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStream; + +import java.nio.channels.FileChannel; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.InputStream; - -import java.nio.channels.FileChannel; - public class IOUtils { - - - /* - * Copy a file - */ - public static void copyFile (File in, File out) throws java.io.IOException { - - FileChannel sourceChannel = new FileInputStream(in).getChannel(); - FileChannel destinationChannel = new FileOutputStream(out).getChannel(); - - sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel); - - sourceChannel.close(); - destinationChannel.close(); - - return; - } - - /* - * Get a temporary file path - */ - public static String getTemporaryFilePath (String tempFilePrefix, File tmpDirectory) throws java.io.IOException { - - // Get a File object with given prefix, default suffix is ".tmp" - File temporaryFile = File.createTempFile(tempFilePrefix, null, tmpDirectory); - String temporaryFilePath = temporaryFile.getPath(); - - return temporaryFilePath; - } - - /* - * Get a ByteArrayOutputStream containing all data that could be read from the given InputStream - */ - public static ByteArrayOutputStream readBytesFrom (InputStream inputStream, int defaultBufferSize) throws java.io.IOException { - - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(defaultBufferSize); - byte[] buffer = new byte [defaultBufferSize]; - - int readBytes = inputStream.read(buffer); - while (readBytes > 0) { - outputStream.write(buffer, 0, readBytes); - readBytes = inputStream.read(buffer); - } - - return outputStream; - } - + + + /** + * Copy a file + */ + public static void copyFile (File in, File out) throws java.io.IOException { + + FileChannel sourceChannel = new FileInputStream(in).getChannel(); + FileChannel destinationChannel = new FileOutputStream(out).getChannel(); + + sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel); + + sourceChannel.close(); + destinationChannel.close(); + + return; + } + + /** + * Get a temporary file path + */ + public static String getTemporaryFilePath (String tempFilePrefix, File tmpDirectory) throws java.io.IOException { + + // Get a File object with given prefix, default suffix is ".tmp" + File temporaryFile = File.createTempFile(tempFilePrefix, null, tmpDirectory); + String temporaryFilePath = temporaryFile.getPath(); + + return temporaryFilePath; + } + + /** + * Get a ByteArrayOutputStream containing all data that could be read from the given InputStream + */ + public static ByteArrayOutputStream readBytesFrom (InputStream inputStream, int defaultBufferSize) throws java.io.IOException { + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(defaultBufferSize); + byte[] buffer = new byte [defaultBufferSize]; + + int readBytes = inputStream.read(buffer); + while (readBytes > 0) { + outputStream.write(buffer, 0, readBytes); + readBytes = inputStream.read(buffer); + } + + return outputStream; + } + } Index: lutinutil/src/java/org/codelutin/util/FileUtil.java diff -u /dev/null lutinutil/src/java/org/codelutin/util/FileUtil.java:1.1 --- /dev/null Mon Nov 22 16:14:15 2004 +++ lutinutil/src/java/org/codelutin/util/FileUtil.java Mon Nov 22 16:14:10 2004 @@ -0,0 +1,187 @@ +/* *##% + * Copyright (C) 2002, 2003, 2004 Code Lutin, Cédric Pineau, + * Benjamin Poussin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +/* * + * FileUtil.java + * + * Created: 22 nov. 2004 + * + * @author Benjamin Poussin + * @version $Revision: 1.1 $ + * + * Mise a jour: $Date: 2004/11/22 16:14:10 $ + * par : $Author: bpoussin $ + */ + +package org.codelutin.util; + +import java.io.File; +import java.io.FileFilter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +public class FileUtil { // FileUtil + + static public interface FileAction { + public boolean doAction(File f); + } + + + + + /** + * Retourne tous les sous répertoires du répertoire passé en argument. + * @param directory un répertoire + * @return une liste d'objet {@link java.io.File} de répertoires et ceci + * recursivement à partir de directory, si directory + * n'est pas un répertoire la liste est vide. + */ + public static List getSubDirectories(File directory) { + class DirectoryFilter implements FileFilter { + public boolean accept(File f) { + return f.isDirectory(); + } + } + return getFilteredElements(directory, new DirectoryFilter()); + } + + /** + * Retourne tous les fichiers du répertoire passé en argument. + * @param directory un répertoire + * @return une liste d'objet {@link java.io.File} des fichiers et ceci + * recursivement à partir de directory, si directory n'est pas un + * répertoire la liste est vide + */ + public static List getFiles(File directory) { + class NormalFileFilter implements FileFilter { + public boolean accept(File f) { + return f.isFile(); + } + } + return getFilteredElements(directory, new NormalFileFilter()); + } + + /** + * Retourne les fichiers d'un répertoire qui s'attisfont un certain pattern. + * La recherche est faire récursivement dans les sous répertoires + * @param directory le répertoire à partir duquel il faut faire la recherche + * @param pattern le pattern que doit respecter le fichier pour être dans la + * liste résultante + * @return une liste d'objet {@link java.io.File} qui ont s'attisfait le + * pattern. + */ + public static List find(File directory, final String pattern){ + final String root = directory.getAbsolutePath(); + final int rootLength = root.length(); + + return getFilteredElements(directory, new FileFilter() { + public boolean accept(File f) { + String longFilename = f.getAbsolutePath(); + // + 1 to remove the first / or \ + String filename = longFilename.substring(rootLength + 1); + return filename.matches(pattern); + } + }); + } + + /** + * Retourne la liste de toutes les fichiers ou répertoire qui s'attisfont + * le filtre + * @param directory repertoire à partir duquel il faut faire la recherche + * @param ff le filtre à appliquer pour savoir si le fichier parcouru doit + * être conservé dans les résultats. + * @return une liste d'objet {@link java.io.File}, qui s'attisfont le filtre + */ + public static List getFilteredElements(File directory, FileFilter ff) { + ArrayList result = new ArrayList(); + LinkedList todo = new LinkedList(); + if (directory.isDirectory()){ + todo.addAll(Arrays.asList(directory.listFiles())); + } + while(todo.size() > 0){ + File file = (File)todo.removeFirst(); + if (file.isDirectory()){ + todo.addAll(Arrays.asList(file.listFiles())); + } + if(ff.accept(file)){ + result.add(file); + } + } + return result; + } + + /** + * Supprime recursivement tout le contenu d'un répertoire. + * @return vrai si tout se passe bien, false si la suppression d'un élement + * se passe mal + */ + public static boolean deleteRecursively(File directory) { + return walkBefore(directory, new FileAction(){ + public boolean doAction(File f) { + return f.delete(); + } + }); + } + + /** + * Permet de faire une action avant le parcours des fichiers, c-a-d que + * l'on fera l'action sur les fichiers contenu dans un répertoire + * après l'action sur le répertoire lui même. + * @param f le fichier ou répertoire à partir duquel il faut commencer + * @param fileAction l'action à effectuer sur chaque fichier + * @return le résultat des fileAction executé sur les fichiers, chaque + * résultat de FileAction sont assemblé par un ET logique pour donner + * le résultat final + */ + public static boolean walkAfter(File f, FileAction fileAction) { + boolean result=fileAction.doAction(f); + if (f.isDirectory()) { + File list[] = f.listFiles(); + for (int i = 0; i < list.length; i++) { + result=result && walkAfter(list[i], fileAction); + } + } + return result; + } + + /** + * Permet de faire une action apès le parcours des fichiers, c-a-d que + * l'on fera l'action sur les fichiers contenu dans un répertoire + * avant l'action sur le répertoire lui même. + * @param f le fichier ou répertoire à partir duquel il faut commencer + * @param fileAction l'action à effectuer sur chaque fichier + * @return le résultat des fileAction executé sur les fichiers, chaque + * résultat de FileAction sont assemblé par un ET logique pour donner + * le résultat final + */ + public static boolean walkBefore(File f, FileAction fileAction) { + boolean result=true; + if (f.isDirectory()) { + File list[] = f.listFiles(); + for (int i = 0; i < list.length; i++) { + result=result && walkBefore(list[i], fileAction); + } + } + return result && fileAction.doAction(f); + } + +} // FileUtil +