KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > micronova > util > codec > CodecFile


1 /*
2
3 Copyright 2003-2007 MicroNova (R)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
8 conditions are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12
13     * Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17     * Neither the name of MicroNova nor the names of its contributors
18     may be used to endorse or promote products derived from this
19     software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32
33 */

34
35
36 package com.micronova.util.codec;
37
38 import com.micronova.util.*;
39 import java.io.*;
40 import java.util.*;
41 import java.util.regex.*;
42 import javax.activation.*;
43
44 /** file codecs */
45
46 public class CodecFile extends Codec
47 {
48     /** lists directory as list of strings */
49
50     public static Object JavaDoc list(Object JavaDoc object)
51     {
52         Object JavaDoc result = null;
53
54         File file = TypeUtil.isFile(object);
55
56         if (file != null)
57         {
58             result = TypeUtil.isList(file.list());
59         }
60
61         return result;
62     }
63
64     /** lists directory as list of files */
65
66     public static Object JavaDoc listFiles(Object JavaDoc object)
67     {
68         Object JavaDoc result = null;
69
70         File file = TypeUtil.isFile(object);
71
72         if (file != null)
73         {
74             result = TypeUtil.isList(file.listFiles());
75         }
76
77         return result;
78     }
79
80     /** deletes file, returns Boolean.TRUE or Boolean.FALSE */
81
82     public static Object JavaDoc delete(Object JavaDoc object)
83     {
84         Object JavaDoc result = null;
85
86         File file = TypeUtil.isFile(object);
87
88         if (file != null)
89         {
90             result = new Boolean JavaDoc(file.delete());
91         }
92
93         return result;
94     }
95
96     /** renames file, returns renamed file if successful, otherwise null */
97
98     public static Object JavaDoc rename(Object JavaDoc object, Object JavaDoc renameTo)
99     {
100         Object JavaDoc result = null;
101
102         File file = TypeUtil.isFile(object);
103         File to = TypeUtil.isFile(renameTo);
104
105         if ((file != null) && (to != null))
106         {
107             try
108             {
109                 if (file.renameTo(to))
110                 {
111                     result = to;
112                 }
113             }
114             catch (Exception JavaDoc e)
115             {
116             }
117         }
118
119         return result;
120     }
121
122     /** reads file content using given encoding */
123
124     public static Object JavaDoc read(Object JavaDoc object, Object JavaDoc encoding)
125     {
126         Object JavaDoc result = null;
127
128         InputStream inputStream = null;
129         Reader reader = null;
130
131         try
132         {
133             if (object instanceof InputStream)
134             {
135                 inputStream = (InputStream)object;
136             }
137             else if (object instanceof Reader)
138             {
139                 reader = (Reader)object;
140             }
141             else
142             {
143                 File file = TypeUtil.isFile(object);
144             
145                 if (file != null)
146                 {
147                     inputStream = new FileInputStream(file);
148                 }
149             }
150
151             if (inputStream != null)
152             {
153                 result = new String JavaDoc(IOUtil.readAll(inputStream), encoding.toString());
154             }
155
156             if (reader != null)
157             {
158                 result = new String JavaDoc(IOUtil.readAll(reader));
159             }
160         }
161         catch (Exception JavaDoc e)
162         {
163             result = null;
164         }
165         finally
166         {
167             IOUtil.tryClose(inputStream);
168             IOUtil.tryClose(reader);
169         }
170
171         return result;
172     }
173
174     /** read as binary string */
175
176     public static Object JavaDoc read(Object JavaDoc object)
177     {
178         return read(object, "iso-8859-1");
179     }
180
181     /** writes object to a file, returns object if successful, otherwise null */
182     private static Object JavaDoc write(Object JavaDoc object, Object JavaDoc fileObject, String JavaDoc encoding, boolean doesAppend)
183     {
184         Object JavaDoc result = null;
185
186         if (object != null)
187         {
188             File file = TypeUtil.isFile(fileObject);
189             
190             if (file != null)
191             {
192                 OutputStream outputStream = null;
193                 Writer writer = null;
194                 
195                 try
196                 {
197                     if (object instanceof Reader)
198                     {
199                         writer = new FileWriter(file, doesAppend);
200
201                         IOUtil.copy((Reader)object, writer);
202
203                         writer.close();
204
205                         writer = null;
206
207                         result = object;
208                     }
209                     else if (object instanceof InputStream)
210                     {
211                         outputStream = new FileOutputStream(file, doesAppend);
212                         
213                         IOUtil.copy((InputStream)object, outputStream);
214
215                         outputStream.close();
216
217                         outputStream = null;
218
219                         result = object;
220                     }
221                     else
222                     {
223                         outputStream = new FileOutputStream(file, doesAppend);
224                     
225                         outputStream.write(object.toString().getBytes(encoding));
226                         
227                         outputStream.close();
228                         
229                         outputStream = null;
230
231                         result = object;
232                     }
233                 }
234                 catch (Exception JavaDoc e)
235                 {
236                     result = null;
237                 }
238                 finally
239                 {
240                     IOUtil.tryClose(outputStream);
241                     IOUtil.tryClose(writer);
242                 }
243             }
244         }
245
246         return result;
247     }
248
249     /** writes object to a file, returns object if successful, otherwise null */
250     public static Object JavaDoc write(Object JavaDoc object, Object JavaDoc fileObject, Object JavaDoc encoding)
251     {
252         return write(object, fileObject, encoding.toString(), false);
253     }
254
255     /** writes object to a file, returns object if successful, otherwise null */
256     public static Object JavaDoc write(Object JavaDoc object, Object JavaDoc fileObject)
257     {
258         return write(object, fileObject, "iso-8859-1", false);
259     }
260
261     /** appends object to a file, returns object if successful, otherwise null */
262     public static Object JavaDoc append(Object JavaDoc object, Object JavaDoc fileObject, Object JavaDoc encoding)
263     {
264         return write(object, fileObject, encoding.toString(), true);
265     }
266  
267     /** appends object to a file, returns object if successful, otherwise null */
268     public static Object JavaDoc append(Object JavaDoc object, Object JavaDoc fileObject)
269     {
270         return write(object, fileObject, "iso-8859-1", true);
271     }
272
273     /** make a directory creating parent directories if parents is not null */
274
275     public static Object JavaDoc mkdir(Object JavaDoc object, Object JavaDoc parents)
276     {
277         Object JavaDoc result = null;
278
279         File file = TypeUtil.isFile(object);
280
281         if (file != null)
282         {
283             try
284             {
285                 if ((parents == null) ? file.mkdir() : file.mkdirs())
286                 {
287                     result = file;
288                 }
289             }
290             catch (Exception JavaDoc e)
291             {
292             }
293         }
294
295         return result;
296     }
297
298     /** make a directory */
299
300     public static Object JavaDoc mkdir(Object JavaDoc object)
301     {
302         return mkdir(object, null);
303     }
304
305     /** make a directory, creating parents */
306
307     public static Object JavaDoc mkdirs(Object JavaDoc object)
308     {
309         return mkdir(object, Boolean.TRUE);
310     }
311
312     /** return file type */
313     
314     public static Object JavaDoc type(Object JavaDoc fileObject)
315     {
316         File file = TypeUtil.isFile(fileObject);
317
318         if (file != null)
319         {
320             return (new FileDataSource(file)).getContentType();
321         }
322
323         return null;
324     }
325
326     /** temporary file, using "prefix*suffix" format. */
327
328     public static Object JavaDoc tempfile(Object JavaDoc object)
329     {
330         if (object == null)
331         {
332             object = "temp*";
333         }
334
335         try
336         {
337             String JavaDoc prefix = "";
338             String JavaDoc suffix = "";
339             
340             String JavaDoc[] part = object.toString().split("[*]");
341             
342             int partLength = part.length;
343             
344             if (partLength >= 1)
345             {
346                 prefix = part[0];
347             }
348             
349             if (partLength >= 2)
350             {
351                 suffix = part[1];
352             }
353             
354             File directory = null;
355             
356             int slashIndex = prefix.lastIndexOf('/');
357             
358             if (slashIndex >= 0)
359             {
360                 String JavaDoc directoryPrefix = prefix.substring(0, slashIndex);
361                 
362                 directory = new File(directoryPrefix);
363                 
364                 prefix = prefix.substring(slashIndex + 1);
365             }
366             
367             if (directory != null)
368             {
369                 object = File.createTempFile(prefix, suffix, directory);
370             }
371             else
372             {
373                 object = File.createTempFile(prefix, suffix);
374             }
375         }
376         catch (Exception JavaDoc e)
377         {
378             object = null;
379         }
380
381         return object;
382     }
383
384     /** closes stream/reader/writer */
385
386     public static Object JavaDoc close(Object JavaDoc object) throws Exception JavaDoc
387     {
388         if (object instanceof InputStream)
389         {
390             ((InputStream)object).close();
391         }
392         else if (object instanceof OutputStream)
393         {
394             ((OutputStream)object).close();
395         }
396         else if (object instanceof Writer)
397         {
398             ((Writer)object).close();
399         }
400         else if (object instanceof Reader)
401         {
402             ((Reader)object).close();
403         }
404
405         return object;
406     }
407 }
408
Popular Tags