Source Code Cross Referenced for IOUtilities.java in  » Swing-Library » jEdit » org » gjt » sp » util » 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 » Swing Library » jEdit » org.gjt.sp.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * IOUtilities.java - IO related functions
003:         * :tabSize=8:indentSize=8:noTabs=false:
004:         * :folding=explicit:collapseFolds=1:
005:         *
006:         * Copyright (C) 2006 Matthieu Casanova
007:         *
008:         * This program is free software; you can redistribute it and/or
009:         * modify it under the terms of the GNU General Public License
010:         * as published by the Free Software Foundation; either version 2
011:         * of the License, or any later version.
012:         *
013:         * This program is distributed in the hope that it will be useful,
014:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
015:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016:         * GNU General Public License for more details.
017:         *
018:         * You should have received a copy of the GNU General Public License
019:         * along with this program; if not, write to the Free Software
020:         * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
021:         */
022:
023:        package org.gjt.sp.util;
024:
025:        import java.io.*;
026:
027:        /**
028:         * IO tools that depends on JDK only.
029:         *
030:         * @author Matthieu Casanova
031:         * @version $Id: IOUtilities.java 9728 2007-06-09 02:00:10Z Vampire0 $
032:         * @since 4.3pre5
033:         */
034:        public class IOUtilities {
035:            //{{{ moveFile() method
036:            /**
037:             * Moves the source file to the destination.
038:             *
039:             * If the destination cannot be created or is a read-only file, the
040:             * method returns <code>false</code>. Otherwise, the contents of the
041:             * source are copied to the destination, the source is deleted,
042:             * and <code>true</code> is returned.
043:             *
044:             * @param source The source file to move.
045:             * @param dest   The destination where to move the file.
046:             * @return true on success, false otherwise.
047:             *
048:             * @since jEdit 4.3pre9
049:             */
050:            public static boolean moveFile(File source, File dest) {
051:                boolean ok = false;
052:
053:                if ((dest.exists() && dest.canWrite())
054:                        || (!dest.exists() && dest.getParentFile().canWrite())) {
055:                    OutputStream fos = null;
056:                    InputStream fis = null;
057:                    try {
058:                        fos = new FileOutputStream(dest);
059:                        fis = new FileInputStream(source);
060:                        ok = copyStream(32768, null, fis, fos, false);
061:                    } catch (IOException ioe) {
062:                        Log.log(Log.WARNING, IOUtilities.class,
063:                                "Error moving file: " + ioe + " : "
064:                                        + ioe.getMessage());
065:                    } finally {
066:                        closeQuietly(fos);
067:                        closeQuietly(fis);
068:                    }
069:
070:                    if (ok)
071:                        source.delete();
072:                }
073:                return ok;
074:            } //}}}
075:
076:            //{{{ copyStream() method
077:            /**
078:             * Copy an input stream to an output stream.
079:             *
080:             * @param bufferSize the size of the buffer
081:             * @param progress the progress observer it could be null
082:             * @param in the input stream
083:             * @param out the output stream
084:             * @param canStop if true, the copy can be stopped by interrupting the thread
085:             * @return <code>true</code> if the copy was done, <code>false</code> if it was interrupted
086:             * @throws IOException  IOException If an I/O error occurs
087:             */
088:            public static boolean copyStream(int bufferSize,
089:                    ProgressObserver progress, InputStream in,
090:                    OutputStream out, boolean canStop) throws IOException {
091:                byte[] buffer = new byte[bufferSize];
092:                int n;
093:                long copied = 0L;
094:                while (-1 != (n = in.read(buffer))) {
095:                    out.write(buffer, 0, n);
096:                    copied += n;
097:                    if (progress != null)
098:                        progress.setValue(copied);
099:                    if (canStop && Thread.interrupted())
100:                        return false;
101:                }
102:                return true;
103:            } //}}}
104:
105:            //{{{ copyStream() method
106:            /**
107:             * Copy an input stream to an output stream with a buffer of 4096 bytes.
108:             *
109:             * @param progress the progress observer it could be null
110:             * @param in the input stream
111:             * @param out the output stream
112:             * @param canStop if true, the copy can be stopped by interrupting the thread
113:             * @return <code>true</code> if the copy was done, <code>false</code> if it was interrupted
114:             * @throws IOException  IOException If an I/O error occurs
115:             */
116:            public static boolean copyStream(ProgressObserver progress,
117:                    InputStream in, OutputStream out, boolean canStop)
118:                    throws IOException {
119:                return copyStream(4096, progress, in, out, canStop);
120:            } //}}}
121:
122:            //{{{ fileLength() method
123:            /**
124:             * Returns the length of a file. If it is a directory it will calculate recursively the length.
125:             *
126:             * @param file the file or directory
127:             * @return the length of the file or directory. If the file doesn't exists it will return 0
128:             * @since 4.3pre10
129:             */
130:            public static long fileLength(File file) {
131:                long length = 0L;
132:                if (file.isFile())
133:                    length = file.length();
134:                else if (file.isDirectory()) {
135:                    File[] files = file.listFiles();
136:                    for (int i = 0; i < files.length; i++) {
137:                        length += fileLength(files[i]);
138:                    }
139:                }
140:                return length;
141:            } // }}}
142:
143:            //{{{ closeQuietly() method
144:            /**
145:             * Method that will close an {@link InputStream} ignoring it if it is null and ignoring exceptions.
146:             *
147:             * @param in the InputStream to close.
148:             */
149:            public static void closeQuietly(InputStream in) {
150:                if (in != null) {
151:                    try {
152:                        in.close();
153:                    } catch (IOException e) {
154:                        //ignore
155:                    }
156:                }
157:            } //}}}
158:
159:            //{{{ closeQuietly() method
160:            /**
161:             * Method that will close an {@link OutputStream} ignoring it if it is null and ignoring exceptions.
162:             *
163:             * @param out the OutputStream to close.
164:             */
165:            public static void closeQuietly(OutputStream out) {
166:                if (out != null) {
167:                    try {
168:                        out.close();
169:                    } catch (IOException e) {
170:                        //ignore
171:                    }
172:                }
173:            } //}}}
174:
175:            //{{{ closeQuietly() method
176:            /**
177:             * Method that will close an {@link Reader} ignoring it if it is null and ignoring exceptions.
178:             *
179:             * @param r the Reader to close.
180:             * @since jEdit 4.3pre5
181:             */
182:            public static void closeQuietly(Reader r) {
183:                if (r != null) {
184:                    try {
185:                        r.close();
186:                    } catch (IOException e) {
187:                        //ignore
188:                    }
189:                }
190:            } //}}}
191:
192:            //{{{ closeQuietly() method
193:            /**
194:             * Method that will close an {@link java.io.Closeable} ignoring it if it is null and ignoring exceptions.
195:             *
196:             * @param closeable the closeable to close.
197:             * @since jEdit 4.3pre8
198:             */
199:            public static void closeQuietly(Closeable closeable) {
200:                if (closeable != null) {
201:                    try {
202:                        closeable.close();
203:                    } catch (IOException e) {
204:                        //ignore
205:                    }
206:                }
207:            } //}}}
208:
209:            //{{{ IOUtilities() constructor
210:            private IOUtilities() {
211:            } //}}}
212:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.