KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > tools > IOTools


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (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 General Public License for more details.
13  *
14  * You should have received a copy of the GNU 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
19 package sync4j.framework.tools;
20
21 import java.io.*;
22
23 /**
24  * Container of utility methods for io access
25  *
26  * @author Stefano Fornari @ Funambol
27  * @version $Id: IOTools.java,v 1.5 2005/03/02 20:57:38 harrie Exp $
28  */

29 public class IOTools {
30     
31     /**
32      * Reads a file into a byte array given its filename
33      *
34      * @param file the filename (as java.io.File)
35      *
36      * @return the content of the file as a byte array
37      *
38      * @throws java.io.IOException;
39      */

40     static public byte[] readFileBytes(File file)
41     throws IOException {
42         FileInputStream fis = null;
43         
44         byte[] buf = new byte[(int)file.length()];
45         try {
46             fis = new FileInputStream(file);
47             fis.read(buf);
48             fis.close();
49         } finally {
50             if (fis != null) {
51                 fis.close();
52             }
53         }
54         
55         return buf;
56     }
57         
58     
59     /**
60      * Reads a file into a byte array given its filename
61      *
62      * @param filename the filename (as java.lang.String)
63      *
64      * @return the content of the file as a byte array
65      *
66      * @throws java.io.IOException;
67      */

68     static public byte[] readFileBytes(String JavaDoc filename)
69     throws IOException {
70         return readFileBytes(new File(filename));
71     }
72     
73     /**
74      * Reads a file into a String given its filename
75      *
76      * @param file the filename (as java.io.File)
77      *
78      * @return the content of the file as a string
79      *
80      * @throws java.io.IOException;
81      */

82     static public String JavaDoc readFileString(File file)
83     throws IOException {
84         return new String JavaDoc(readFileBytes(file));
85     }
86     
87     /**
88      * Reads a file into a String given its filename
89      *
90      * @param filename the filename (as java.lang.String)
91      *
92      * @return the content of the file as a string
93      *
94      * @throws java.io.IOException;
95      */

96     static public String JavaDoc readFileString(String JavaDoc filename)
97     throws IOException {
98         return readFileString(new File(filename));
99     }
100     
101     /**
102      * Writes the given string to the file with the given name
103      *
104      * @param str the string to write
105      * @param file the file name as a java.io.File
106      *
107      * @throws java.io.IOException
108      */

109     static public void writeFile(String JavaDoc str, File file)
110     throws IOException {
111         writeFile(str.getBytes(), file);
112     }
113     
114     /**
115      * Writes the given string to the file with the given name
116      *
117      * @param str the string to write
118      * @param filename the file name as a java.lang.String
119      *
120      * @throws java.io.IOException
121      */

122     static public void writeFile(String JavaDoc str, String JavaDoc filename)
123     throws IOException {
124         writeFile(str.getBytes(), new File(filename));
125     }
126     
127     /**
128      * Writes the given bytes to the file with the given name
129      *
130      * @param buf the bytes to write
131      * @param filename the file name as a java.lang.String
132      *
133      * @throws java.io.IOException
134      */

135     static public void writeFile(byte[] buf, String JavaDoc filename)
136     throws IOException {
137         writeFile(buf, new File(filename));
138     }
139     
140     /**
141      * Writes the given bytes to the file with the given name
142      *
143      * @param buf the bytes to write
144      * @param file the file name as a java.io.File
145      *
146      * @throws java.io.IOException
147      */

148     static public void writeFile(byte[] buf, File file)
149     throws IOException {
150         FileOutputStream fos = null;
151         try {
152             fos = new FileOutputStream(file);
153             fos.write(buf);
154             fos.close();
155         } finally {
156             if (fos != null) {
157                 fos.close();
158             }
159         }
160     }
161     
162     /**
163      * Returns a <i>FilenameFilter</i> that accepts only the files of the given
164      * type (extension).
165      *
166      * @param type the type (the file extension) of the files to select. NULL
167      * means all files, the empty string means files without extension
168      * The filtering is case-insensitive
169      *
170      * @return the filter
171      */

172     public static FilenameFilter getFileTypeFilter(String JavaDoc type) {
173         return new FileTypeFilter(type);
174     }
175     
176     // -------------------------------------------------------------------------
177

178     /**
179      * This class is a <i>FilenameFilter</i> that accepts only the files of the
180      * specified type (extension). The filtering is case-insensitive,
181      */

182     public static class FileTypeFilter implements FilenameFilter {
183         
184         private String JavaDoc type;
185         
186         /**
187          * Creates the filter on the given type.
188          *
189          * @param type the type (the file extension) of the files to select. NULL
190          * means all files, the empty string means files without
191          * extension. The filtering is case-insensitive
192          */

193         public FileTypeFilter(final String JavaDoc type) {
194             this.type = type.toUpperCase();
195         }
196         
197         public boolean accept(File dir, String JavaDoc name) {
198             if (type == null) {
199                 return true;
200             }
201             
202             if (type.length() == 0) {
203                 return (name.indexOf('.') < 0);
204             }
205             
206             return (name.toUpperCase().endsWith(type));
207         }
208     }
209 }
Popular Tags