KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > util > Files


1 /*
2   Copyright (C) 2001-2002 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.util;
19
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.FilenameFilter JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.InputStreamReader JavaDoc;
28 import java.io.OutputStreamWriter JavaDoc;
29 import java.io.Reader JavaDoc;
30 import java.io.UnsupportedEncodingException JavaDoc;
31 import java.io.Writer JavaDoc;
32 import java.util.zip.GZIPInputStream JavaDoc;
33 import java.io.FileFilter JavaDoc;
34
35 /**
36  * Various often used file functions
37  */

38 public class Files {
39     /**
40      * Returns an input stream or a file. If the file is compressed
41      * with gzip, it is decompressed.
42      *
43      * @param f the file to get an input stream for
44      */

45     public static InputStream JavaDoc autoDecompressStream(File f)
46         throws FileNotFoundException JavaDoc, IOException JavaDoc
47     {
48         InputStream JavaDoc in = new FileInputStream JavaDoc(f);
49         if (Streams.readUShort(in) == GZIPInputStream.GZIP_MAGIC) {
50             in.close();
51             in = new GZIPInputStream JavaDoc(new FileInputStream JavaDoc(f));
52         } else {
53             in.close();
54             in = new FileInputStream JavaDoc(f);
55         }
56         return in;
57     }
58
59     /**
60      * Returns a reader or a file. If the file is compressed with gzip, it is decompressed.
61      *
62      * @param f the file to get an input stream for
63      * @param encoding charset encoding to use for the Reader
64      */

65     public static Reader JavaDoc autoDecompressReader(File f, String JavaDoc encoding)
66         throws FileNotFoundException JavaDoc, IOException JavaDoc
67     {
68         return new InputStreamReader JavaDoc(autoDecompressStream(f),encoding);
69     }
70
71     /**
72      * Creates a writer for a file with a specific encoding
73      *
74      * @param f the file to create a writer for
75      * @param encoding the encoding of the file
76      */

77     public static Writer JavaDoc newFileWriter(File f, String JavaDoc encoding)
78         throws FileNotFoundException JavaDoc, UnsupportedEncodingException JavaDoc
79     {
80         return new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(f),encoding);
81     }
82
83     /**
84      * Creates a FilenameFilter which matches files whose name end
85      * with a particular extension
86      *
87      * @param extension the extension
88      * @return a FilenameFilter
89      */

90     public static FilenameFilter JavaDoc extensionFilenamFilter(final String JavaDoc extension) {
91         return
92             new FilenameFilter JavaDoc() {
93                     public boolean accept(java.io.File JavaDoc file, String JavaDoc name) {
94                         return name.endsWith(extension);
95                     }
96                 };
97     }
98
99     /**
100      * Replaces leading ~ by the user's home directory
101      * @param path file path to expand
102      */

103     public static String JavaDoc expandFileName(String JavaDoc 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 JavaDoc directoryFilter =
113         new FileFilter JavaDoc() {
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 JavaDoc nonHiddenFilter =
121         new FileFilter JavaDoc() {
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