KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > util > DjFileUtil


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.genimen.djeneric.util;
32
33 import java.io.BufferedReader JavaDoc;
34 import java.io.BufferedWriter JavaDoc;
35 import java.io.File JavaDoc;
36 import java.io.FileInputStream JavaDoc;
37 import java.io.FileOutputStream JavaDoc;
38 import java.io.FileReader JavaDoc;
39 import java.io.FileWriter JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.io.InputStream JavaDoc;
42 import java.io.InputStreamReader JavaDoc;
43 import java.io.OutputStream JavaDoc;
44 import java.io.Reader JavaDoc;
45
46 public class DjFileUtil
47 {
48
49   public static void writeFile(String JavaDoc filename, String JavaDoc contents) throws IOException JavaDoc
50   {
51     BufferedWriter JavaDoc w = new BufferedWriter JavaDoc(new FileWriter JavaDoc(filename));
52     w.write(contents);
53     w.close();
54   }
55
56   public static void writeFile(String JavaDoc filename, byte[] contents) throws IOException JavaDoc
57   {
58     FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(new File JavaDoc(filename));
59     fos.write(contents);
60     fos.close();
61   }
62
63   public static String JavaDoc readResource(Class JavaDoc root, String JavaDoc fileName) throws IOException JavaDoc
64   {
65     InputStream JavaDoc is = root.getResourceAsStream(fileName);
66     return readStream(new InputStreamReader JavaDoc(is));
67   }
68
69   public static String JavaDoc readResource(String JavaDoc fileName) throws IOException JavaDoc
70   {
71     InputStream JavaDoc is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
72     return readStream(new InputStreamReader JavaDoc(is));
73   }
74
75   public static String JavaDoc readFile(String JavaDoc p_filenaam) throws IOException JavaDoc
76   {
77     Reader JavaDoc r = new FileReader JavaDoc(p_filenaam);
78     return readStream(r);
79   }
80
81   public static String JavaDoc readFile(String JavaDoc p_filenaam, String JavaDoc p_charset) throws IOException JavaDoc
82   {
83     InputStream JavaDoc is = new FileInputStream JavaDoc(p_filenaam);
84     Reader JavaDoc r = new InputStreamReader JavaDoc(is, p_charset);
85     return readStream(r);
86   }
87
88   public static String JavaDoc readStream(Reader JavaDoc p_reader) throws IOException JavaDoc
89   {
90     BufferedReader JavaDoc br = new BufferedReader JavaDoc(p_reader);
91     StringBuffer JavaDoc src = new StringBuffer JavaDoc(100);
92     String JavaDoc ln;
93     while ((ln = br.readLine()) != null)
94     {
95       src.append(ln);
96       src.append("\n");
97     }
98     br.close();
99     return src.toString();
100   }
101
102   public static boolean createDirectory(String JavaDoc dir)
103   {
104     try
105     {
106       File JavaDoc fd;
107       if (dir.endsWith("/") || dir.endsWith("\\"))
108       {
109         fd = new File JavaDoc(dir.substring(0, dir.length() - 1));
110       }
111       else
112       {
113         fd = new File JavaDoc(dir);
114       }
115       return fd.mkdirs();
116     }
117     catch (Exception JavaDoc iox)
118     {
119       DjLogger.log(iox);
120       return false;
121     }
122   }
123
124   public static String JavaDoc trimLastSlash(String JavaDoc path)
125   {
126     String JavaDoc result = path;
127
128     int idx1 = path.lastIndexOf("/");
129     int idx2 = path.lastIndexOf("\\");
130
131     if (idx1 == -1) idx1 = idx2;
132     if (idx2 == -1) idx2 = idx1;
133
134     idx1 = Math.max(idx1, idx2);
135     if (idx1 != -1)
136     {
137       result = path.substring(0, idx1);
138     }
139     return result;
140   }
141
142   public static void copy(InputStream JavaDoc source, OutputStream JavaDoc target) throws IOException JavaDoc
143   {
144     int chunkSize = 63 * 1024;
145     byte[] ba = new byte[chunkSize];
146
147     while (true)
148     {
149
150       int bytesRead = readBlocking(source, ba, 0, chunkSize);
151       if (bytesRead > 0)
152       {
153         target.write(ba, 0, bytesRead);
154       }
155       else
156       {
157         break; // EOF
158
}
159     }
160   }
161
162   public static final int readBlocking(InputStream JavaDoc in, byte b[], long off, int len) throws IOException JavaDoc
163   {
164     int totalBytesRead = 0;
165
166     while (totalBytesRead < len)
167     {
168       int bytesRead = in.read(b, (int) (off + totalBytesRead), len - totalBytesRead);
169       if (bytesRead < 0)
170       {
171         break;
172       }
173       totalBytesRead += bytesRead;
174     }
175     return totalBytesRead;
176   }
177
178 }
Popular Tags