1 17 18 package org.objectweb.jac.util; 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.FileNotFoundException ; 23 import java.io.FileOutputStream ; 24 import java.io.FilenameFilter ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.InputStreamReader ; 28 import java.io.OutputStreamWriter ; 29 import java.io.Reader ; 30 import java.io.UnsupportedEncodingException ; 31 import java.io.Writer ; 32 import java.util.zip.GZIPInputStream ; 33 import java.io.FileFilter ; 34 35 38 public class Files { 39 45 public static InputStream autoDecompressStream(File f) 46 throws FileNotFoundException , IOException 47 { 48 InputStream in = new FileInputStream (f); 49 if (Streams.readUShort(in) == GZIPInputStream.GZIP_MAGIC) { 50 in.close(); 51 in = new GZIPInputStream (new FileInputStream (f)); 52 } else { 53 in.close(); 54 in = new FileInputStream (f); 55 } 56 return in; 57 } 58 59 65 public static Reader autoDecompressReader(File f, String encoding) 66 throws FileNotFoundException , IOException 67 { 68 return new InputStreamReader (autoDecompressStream(f),encoding); 69 } 70 71 77 public static Writer newFileWriter(File f, String encoding) 78 throws FileNotFoundException , UnsupportedEncodingException 79 { 80 return new OutputStreamWriter (new FileOutputStream (f),encoding); 81 } 82 83 90 public static FilenameFilter extensionFilenamFilter(final String extension) { 91 return 92 new FilenameFilter () { 93 public boolean accept(java.io.File file, String name) { 94 return name.endsWith(extension); 95 } 96 }; 97 } 98 99 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 112 public static final FileFilter directoryFilter = 113 new FileFilter () { 114 public boolean accept(File f) { 115 return f.isDirectory(); 116 } 117 }; 118 119 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 } 135 | Popular Tags |