001 /* 002 Copyright (C) 2001-2002 Laurent Martelli <laurent@aopsys.com> 003 004 This program is free software; you can redistribute it and/or modify 005 it under the terms of the GNU Lesser General Public License as 006 published by the Free Software Foundation; either version 2 of the 007 License, or (at your option) any later version. 008 009 This program is distributed in the hope that it will be useful, 010 but WITHOUT ANY WARRANTY; without even the implied warranty of 011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 GNU Lesser General Public License for more details. 013 014 You should have received a copy of the GNU Lesser General Public License 015 along with this program; if not, write to the Free Software 016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 017 018 package org.objectweb.jac.util; 019 020 import java.io.File; 021 import java.io.FileInputStream; 022 import java.io.FileNotFoundException; 023 import java.io.FileOutputStream; 024 import java.io.FilenameFilter; 025 import java.io.IOException; 026 import java.io.InputStream; 027 import java.io.InputStreamReader; 028 import java.io.OutputStreamWriter; 029 import java.io.Reader; 030 import java.io.UnsupportedEncodingException; 031 import java.io.Writer; 032 import java.util.zip.GZIPInputStream; 033 import java.io.FileFilter; 034 035 /** 036 * Various often used file functions 037 */ 038 public class Files { 039 /** 040 * Returns an input stream or a file. If the file is compressed 041 * with gzip, it is decompressed. 042 * 043 * @param f the file to get an input stream for 044 */ 045 public static InputStream autoDecompressStream(File f) 046 throws FileNotFoundException, IOException 047 { 048 InputStream in = new FileInputStream(f); 049 if (Streams.readUShort(in) == GZIPInputStream.GZIP_MAGIC) { 050 in.close(); 051 in = new GZIPInputStream(new FileInputStream(f)); 052 } else { 053 in.close(); 054 in = new FileInputStream(f); 055 } 056 return in; 057 } 058 059 /** 060 * Returns a reader or a file. If the file is compressed with gzip, it is decompressed. 061 * 062 * @param f the file to get an input stream for 063 * @param encoding charset encoding to use for the Reader 064 */ 065 public static Reader autoDecompressReader(File f, String encoding) 066 throws FileNotFoundException, IOException 067 { 068 return new InputStreamReader(autoDecompressStream(f),encoding); 069 } 070 071 /** 072 * Creates a writer for a file with a specific encoding 073 * 074 * @param f the file to create a writer for 075 * @param encoding the encoding of the file 076 */ 077 public static Writer newFileWriter(File f, String encoding) 078 throws FileNotFoundException, UnsupportedEncodingException 079 { 080 return new OutputStreamWriter(new FileOutputStream(f),encoding); 081 } 082 083 /** 084 * Creates a FilenameFilter which matches files whose name end 085 * with a particular extension 086 * 087 * @param extension the extension 088 * @return a FilenameFilter 089 */ 090 public static FilenameFilter extensionFilenamFilter(final String extension) { 091 return 092 new FilenameFilter() { 093 public boolean accept(java.io.File file, String name) { 094 return name.endsWith(extension); 095 } 096 }; 097 } 098 099 /** 100 * Replaces leading ~ by the user's home directory 101 * @param path file path to expand 102 */ 103 public static String expandFileName(String path) { 104 if (path.startsWith("~")) { 105 return System.getProperty("user.home") + path.substring(1); 106 } else { 107 return path; 108 } 109 } 110 111 /** A filter to list only directories */ 112 public static final FileFilter directoryFilter = 113 new FileFilter() { 114 public boolean accept(File f) { 115 return f.isDirectory(); 116 } 117 }; 118 119 /** A filter to list only non hidden files */ 120 public static final FileFilter nonHiddenFilter = 121 new FileFilter() { 122 public boolean accept(File f) { 123 return !f.isHidden(); 124 } 125 }; 126 127 public static File[] listDirectories(File dir) { 128 return dir.listFiles(directoryFilter); 129 } 130 131 public static File[] listNonHiddenFiles(File dir) { 132 return dir.listFiles(nonHiddenFilter); 133 } 134 }