Source Code Cross Referenced for DiskIO.java in  » Mail-Clients » columba-1.4 » org » columba » core » io » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Mail Clients » columba 1.4 » org.columba.core.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        //The contents of this file are subject to the Mozilla Public License Version 1.1
002:        //(the "License"); you may not use this file except in compliance with the
003:        //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004:        //
005:        //Software distributed under the License is distributed on an "AS IS" basis,
006:        //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007:        //for the specific language governing rights and
008:        //limitations under the License.
009:        //
010:        //The Original Code is "The Columba Project"
011:        //
012:        //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013:        //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014:        //
015:        //All Rights Reserved.
016:
017:        package org.columba.core.io;
018:
019:        import java.io.BufferedOutputStream;
020:        import java.io.BufferedReader;
021:        import java.io.BufferedWriter;
022:        import java.io.File;
023:        import java.io.FileInputStream;
024:        import java.io.FileOutputStream;
025:        import java.io.IOException;
026:        import java.io.InputStream;
027:        import java.io.InputStreamReader;
028:        import java.io.OutputStreamWriter;
029:        import java.net.URL;
030:        import java.util.logging.Logger;
031:
032:        /**
033:         * Utility methods for handling files and directories.
034:         */
035:        public final class DiskIO {
036:
037:            private static final Logger LOG = Logger
038:                    .getLogger("org.columba.core.io");
039:
040:            private static String resourceFolder = "";
041:
042:            /**
043:             * Private constructor for utility class.
044:             */
045:            private DiskIO() {
046:                // don't instantiate this
047:            }
048:
049:            /**
050:             * Ensures the existence of the directory specified by the parameter. If the
051:             * directory does not exist, an attempt is performed to create it including
052:             * all necessary parent directories that may be implied by the
053:             * specification. ### HELPME : against what should a relative pathname be
054:             * made absolute? ### ### We need to set the installation directory
055:             * somewhere! ####
056:             * 
057:             * @param dir
058:             *            File specifying the intended directory name; if the specified
059:             *            name is a relative path, it is always made absolute against
060:             *            the program's <b>installationDirectory </b>.
061:             * @return <b>true </b> if and only if the specified file exists and is a
062:             *         directory
063:             */
064:            public static boolean ensureDirectory(File dir) {
065:                if (dir == null) {
066:                    throw new IllegalArgumentException("dir = null");
067:                }
068:                boolean success = true;
069:                if (!dir.isDirectory()) {
070:                    success = !dir.isFile() && dir.mkdirs();
071:
072:                    if (success) {
073:                        LOG.info("Created directory: " + dir.toString());
074:                    } else {
075:                        LOG.severe("failed while trying to create directory: "
076:                                + dir.toString());
077:                    }
078:                }
079:
080:                return success;
081:            }
082:
083:            // ensureDirectory
084:            public static boolean ensureDirectory(String path) {
085:                return ensureDirectory(new File(path));
086:            }
087:
088:            public static void saveStringInFile(File toFile, String insertString)
089:                    throws IOException {
090:                BufferedWriter out;
091:
092:                out = new BufferedWriter(new OutputStreamWriter(
093:                        new FileOutputStream(toFile), "ISO-8859-1"));
094:                out.write(insertString);
095:                out.flush();
096:                out.close();
097:            }
098:
099:            public static void saveStreamInFile(File toFile, InputStream is)
100:                    throws IOException {
101:
102:                BufferedOutputStream out = new BufferedOutputStream(
103:                        new FileOutputStream(toFile));
104:
105:                StreamUtils.streamCopy(is, out);
106:
107:                out.flush();
108:                out.close();
109:
110:                is.close();
111:            }
112:
113:            // saveStringInFile
114:            public static String readFileInString(File fromFile)
115:                    throws IOException {
116:                StringBuffer strbuf = new StringBuffer((int) fromFile.length());
117:
118:                BufferedReader in = new BufferedReader(new InputStreamReader(
119:                        new FileInputStream(fromFile), "ISO-8859-1"));
120:                String str;
121:                strbuf = new StringBuffer((int) fromFile.length());
122:
123:                while ((str = in.readLine()) != null) {
124:                    strbuf.append(str + "\n");
125:                }
126:
127:                in.close();
128:
129:                return strbuf.toString();
130:
131:                /*
132:                 * int lineNumber = 0; byte[] buffer = new byte[1024]; int read;
133:                 * StringBuffer out = new StringBuffer((int)fromFile.length());
134:                 * FileInputStream in = new FileInputStream( fromFile );
135:                 * 
136:                 * read = in.read(buffer); while ( read == 1024 ) { out.append(new
137:                 * String(buffer,"ISO-8859-1")); read = in.read(buffer); }
138:                 * 
139:                 * out.append(new String(buffer,0,read,"ISO-8859-1")); in.close();
140:                 * 
141:                 * return out.toString();
142:                 */
143:            }
144:
145:            // saveStringInFile
146:
147:            /**
148:             * Deletes the directory specified by the parameter and all of its contents.
149:             * This does recurse into subdirectories. Function reports errors. If the
150:             * parameter does not denote a directory, <b>false </b> is always returned.
151:             * 
152:             * @param dir
153:             *            a File representing the directory to be delete
154:             * @return <b>true </b> if and only if the directory does not exist on
155:             *         termination; a return value <b>false </b> does not imply that
156:             *         there were no files deleted
157:             * @throws IllegalArgumentException
158:             *             if the parameter is <b>null </b>
159:             */
160:            public static boolean deleteDirectory(File dir) {
161:                File[] files;
162:                File f;
163:                int i;
164:                boolean success = true;
165:
166:                if (dir == null) {
167:                    throw new IllegalArgumentException("dir = null");
168:                }
169:
170:                if (!dir.isDirectory()) {
171:                    return false;
172:                }
173:
174:                files = dir.listFiles();
175:
176:                for (i = 0; i < files.length; i++) {
177:                    if ((f = files[i]).isDirectory()) {
178:                        deleteDirectory(f);
179:                    } else if (!f.delete()) {
180:                        LOG.severe("*** failed to delete file "
181:                                + f.getAbsolutePath());
182:                    }
183:                }
184:
185:                success = dir.delete();
186:
187:                if (!success) {
188:                    LOG.severe("*** failed to delete directory "
189:                            + dir.getAbsolutePath());
190:                }
191:
192:                return success;
193:            }
194:
195:            // deleteDirectory
196:
197:            /**
198:             * Deletes the contents of an existing directory. (May be applied to
199:             * non-existing files without causing error.)
200:             * 
201:             * @return <b>true </b> if and only if on termination the directory exists
202:             *         and is empty
203:             */
204:            public static boolean emptyDirectory(File dir) {
205:                boolean success;
206:
207:                if (dir == null) {
208:                    throw new IllegalArgumentException("dir = null");
209:                }
210:
211:                if ((success = dir.exists() && deleteDirectory(dir))) {
212:                    dir.mkdirs();
213:                }
214:
215:                return success && dir.exists();
216:            }
217:
218:            // emptyDirectory
219:
220:            /**
221:             * General use columba resource InputStream getter.
222:             * 
223:             * @param path
224:             *            the full path and filename of the resource requested. If
225:             *            <code>path</code> begins with "#" it is resolved against the
226:             *            program's standard resource folder after removing "#"
227:             * @return an InputStream to read the resource data, or <b>null </b> if the
228:             *         resource could not be obtained
229:             * @throws java.io.IOException
230:             *             if there was an error opening the input stream
231:             */
232:            public static InputStream getResourceStream(String path)
233:                    throws java.io.IOException {
234:                URL url;
235:
236:                if ((url = getResourceURL(path)) == null) {
237:                    return null;
238:                }
239:                return url.openStream();
240:            }
241:
242:            // getResourceStream
243:
244:            /**
245:             * General use columba resource URL getter.
246:             * 
247:             * @param path
248:             *            the full path and filename of the resource requested. If
249:             *            <code>path</code> begins with "#" it is resolved against the
250:             *            program's standard resource folder after removing "#"
251:             * @return an URL instance, or <b>null </b> if the resource could not be
252:             *         obtained
253:             * @throws java.io.IOException
254:             *             if there was an error opening the input stream
255:             */
256:            public static URL getResourceURL(String path) // throws
257:            // java.io.IOException
258:            {
259:                URL url;
260:
261:                if (path == null) {
262:                    throw new IllegalArgumentException("path = null");
263:                }
264:
265:                if (path.startsWith("#")) {
266:                    path = resourceFolder + path.substring(1);
267:                }
268:
269:                // url = ClassLoader.getSystemResource(path);
270:                url = DiskIO.class.getResource("/" + path);
271:
272:                if (url == null) {
273:                    LOG.info("*** failed locating resource: " + path);
274:
275:                    return null;
276:                }
277:
278:                return url;
279:            }
280:
281:            // getResourceURL
282:            public static void setResourceRoot(String path) {
283:                if (path == null) {
284:                    resourceFolder = "";
285:                } else {
286:                    if (!path.endsWith("/")) {
287:                        path += "/";
288:                    }
289:
290:                    resourceFolder = path;
291:                }
292:            }
293:
294:            // setResourceRoot
295:            public static String getResourceRoot() {
296:                return resourceFolder;
297:            }
298:
299:            /**
300:             * Copies the contents of any disk file to the specified output file. The
301:             * output file will be overridden if it exist. Function reports errors.
302:             * 
303:             * @param inputFile
304:             *            a File object
305:             * @param outputFile
306:             *            a File object
307:             * @throws java.io.IOException
308:             *             if the function could not be completed because of an IO error
309:             */
310:            public static void copyFile(File inputFile, File outputFile)
311:                    throws java.io.IOException {
312:                FileInputStream in;
313:                FileOutputStream out;
314:
315:                byte[] buffer = new byte[512];
316:                int len;
317:
318:                try {
319:                    out = new FileOutputStream(outputFile);
320:                    in = new FileInputStream(inputFile);
321:
322:                    while ((len = in.read(buffer)) != -1) {
323:                        out.write(buffer, 0, len);
324:                    }
325:
326:                    in.close();
327:                    out.close();
328:                } catch (IOException e) {
329:                    LOG.info("*** error during file copy "
330:                            + outputFile.getAbsolutePath() + ": "
331:                            + e.getMessage());
332:                    throw e;
333:                }
334:            }
335:
336:            // copyFile
337:
338:            /**
339:             * Copies a system resource to the specified output file. The output file
340:             * will be overridden if it exist, so the calling routine has to take care
341:             * about unwanted deletions of content. Function reports errors.
342:             * 
343:             * @param resource
344:             *            a full resource path. If the value begins with "#", it is
345:             *            resolved against the program's standard resource folder after
346:             *            removing "#"
347:             * @return <b>true </b> if and only if the operation was successful,
348:             *         <b>false </b> if the resource was not found
349:             * @throws java.io.IOException
350:             *             if there was an IO error
351:             */
352:            public static boolean copyResource(String resource, File outputFile)
353:                    throws java.io.IOException {
354:                InputStream in;
355:                FileOutputStream out;
356:                byte[] buffer = new byte[512];
357:                int len;
358:
359:                // attempt
360:                try {
361:                    if ((in = DiskIO.getResourceStream(resource)) == null) {
362:                        return false;
363:                    }
364:
365:                    out = new FileOutputStream(outputFile);
366:
367:                    while ((len = in.read(buffer)) != -1) {
368:                        out.write(buffer, 0, len);
369:                    }
370:
371:                    out.close();
372:                    in.close();
373:
374:                    LOG.fine("created : " + outputFile.getAbsolutePath());
375:                } catch (IOException e) {
376:                    LOG.severe("*** error during resource file copy "
377:                            + outputFile.getAbsolutePath() + ": "
378:                            + e.getMessage());
379:                    throw e;
380:                }
381:
382:                return true;
383:            }
384:
385:            // copyResource
386:            public static String readStringFromResource(String resource)
387:                    throws java.io.IOException {
388:                InputStream in;
389:
390:                StringBuffer result = new StringBuffer();
391:
392:                // attempt
393:                try {
394:                    if ((in = DiskIO.getResourceStream(resource)) == null) {
395:                        return "";
396:                    }
397:
398:                    BufferedReader reader = new BufferedReader(
399:                            new InputStreamReader(in));
400:
401:                    String nextLine = reader.readLine();
402:
403:                    while (nextLine != null) {
404:                        result.append(nextLine);
405:                        result.append("\n");
406:                        nextLine = reader.readLine();
407:                    }
408:
409:                    in.close();
410:                } catch (IOException e) {
411:                    e.printStackTrace();
412:                    throw e;
413:                }
414:
415:                return result.toString();
416:            }
417:
418:            /**
419:             * Results equal
420:             * <code>copyResource ( String resource, new File (outputFile) )</code>.
421:             */
422:            public static boolean copyResource(String resource,
423:                    String outputFile) throws java.io.IOException {
424:                return copyResource(resource, new File(outputFile));
425:            }
426:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.