Source Code Cross Referenced for FileUtilities.java in  » Source-Control » jcvsweb » com » ice » 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 » Source Control » jcvsweb » com.ice.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         ** Tim Endres' utilities package.
003:         ** Copyright (c) 1997 by Tim Endres
004:         ** 
005:         ** This program is free software.
006:         ** 
007:         ** You may redistribute it and/or modify it under the terms of the GNU
008:         ** General Public License as published by the Free Software Foundation.
009:         ** Version 2 of the license should be included with this distribution in
010:         ** the file LICENSE, as well as License.html. If the license is not
011:         ** included	with this distribution, you may find a copy at the FSF web
012:         ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
013:         ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
014:         **
015:         ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
016:         ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
017:         ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
018:         ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
019:         ** REDISTRIBUTION OF THIS SOFTWARE. 
020:         ** 
021:         */
022:
023:        package com.ice.util;
024:
025:        import java.io.*;
026:
027:        public class FileUtilities {
028:            static public final String RCS_ID = "$Id: FileUtilities.java,v 1.4 1999/03/09 19:44:39 time Exp $";
029:            static public final String RCS_REV = "$Revision: 1.4 $";
030:            static public final String RCS_NAME = "$Name:  $";
031:
032:            public static void copyFile(File from, File to) throws IOException {
033:                int bytes;
034:                long length;
035:                long fileSize;
036:
037:                BufferedInputStream in = null;
038:                BufferedOutputStream out = null;
039:
040:                try {
041:                    in = new BufferedInputStream(new FileInputStream(from));
042:                } catch (IOException ex) {
043:                    throw new IOException(
044:                            "FileUtilities.copyFile: opening input stream '"
045:                                    + from.getPath() + "', " + ex.getMessage());
046:                }
047:
048:                try {
049:                    out = new BufferedOutputStream(new FileOutputStream(to));
050:                } catch (Exception ex) {
051:                    try {
052:                        in.close();
053:                    } catch (IOException ex1) {
054:                    }
055:                    throw new IOException(
056:                            "FileUtilities.copyFile: opening output stream '"
057:                                    + to.getPath() + "', " + ex.getMessage());
058:                }
059:
060:                byte[] buffer;
061:                buffer = new byte[8192];
062:                fileSize = from.length();
063:
064:                for (length = fileSize; length > 0;) {
065:                    bytes = (int) (length > 8192 ? 8192 : length);
066:
067:                    try {
068:                        bytes = in.read(buffer, 0, bytes);
069:                    } catch (IOException ex) {
070:                        try {
071:                            in.close();
072:                            out.close();
073:                        } catch (IOException ex1) {
074:                        }
075:                        throw new IOException(
076:                                "FileUtilities.copyFile: reading input stream, "
077:                                        + ex.getMessage());
078:                    }
079:
080:                    if (bytes < 0)
081:                        break;
082:
083:                    length -= bytes;
084:
085:                    try {
086:                        out.write(buffer, 0, bytes);
087:                    } catch (IOException ex) {
088:                        try {
089:                            in.close();
090:                            out.close();
091:                        } catch (IOException ex1) {
092:                        }
093:                        throw new IOException(
094:                                "FileUtilities.copyFile: writing output stream, "
095:                                        + ex.getMessage());
096:                    }
097:                }
098:
099:                try {
100:                    in.close();
101:                    out.close();
102:                } catch (IOException ex) {
103:                    throw new IOException(
104:                            "FileUtilities.copyFile: closing file streams, "
105:                                    + ex.getMessage());
106:                }
107:            }
108:
109:            public static boolean fileEqualsExtension(String fileName,
110:                    String extension) {
111:                boolean result = false;
112:
113:                int fnLen = fileName.length();
114:                int exLen = extension.length();
115:
116:                if (fnLen > exLen) {
117:                    String fileSuffix = fileName.substring(fnLen - exLen);
118:
119:                    if (FileUtilities.caseSensitivePathNames()) {
120:                        result = fileSuffix.equals(extension);
121:                    } else {
122:                        result = fileSuffix.equalsIgnoreCase(extension);
123:                    }
124:                }
125:
126:                return result;
127:            }
128:
129:            static public boolean caseSensitivePathNames() {
130:                boolean result = true;
131:
132:                String osname = System.getProperty("os.name");
133:
134:                if (osname != null) {
135:                    if (osname.startsWith("macos"))
136:                        result = false;
137:                    else if (osname.startsWith("Windows"))
138:                        result = false;
139:                }
140:
141:                return result;
142:            }
143:
144:            /**
145:             * Determines if a filename matches a 'globbing' pattern.
146:             * The pattern can contain the following special symbols:
147:             * <ul>
148:             * <li> * - Matches zero or more of any character
149:             * <li> ? - Matches exactly one of any character
150:             * <li> [...] - Matches one of any character in the list or range
151:             * </ul>
152:             * 
153:             * @param fileName The name of the file to check.
154:             * @param matchExpr The expression to check against.
155:             * @return If the file name matches the expression, true, else false.
156:             */
157:            public static boolean isPatternString(String pattern) {
158:                if (pattern.indexOf("*") >= 0)
159:                    return true;
160:                if (pattern.indexOf("?") >= 0)
161:                    return true;
162:
163:                int index = pattern.indexOf("[");
164:                if ((index >= 0) && (pattern.indexOf("]") > index + 1))
165:                    return true;
166:
167:                return false;
168:            }
169:
170:            public static boolean matchPattern(String fileName, String pattern) {
171:                return FileUtilities.recurseMatchPattern(fileName, pattern, 0,
172:                        0);
173:            }
174:
175:            /**
176:             * An internal routine to implement expression matching.
177:             * This routine is based on a self-recursive algorithm.
178:             * 
179:             * @param string The string to be compared.
180:             * @param pattern The expression to compare <em>string</em> to.
181:             * @param sIdx The index of where we are in <em>string</em>.
182:             * @param pIdx The index of where we are in <em>pattern</em>.
183:             * @return True if <em>string</em> matched pattern, else false.
184:             */
185:            private static boolean recurseMatchPattern(String string,
186:                    String pattern, int sIdx, int pIdx) {
187:                int pLen = pattern.length();
188:                int sLen = string.length();
189:
190:                for (;;) {
191:                    if (pIdx >= pLen) {
192:                        if (sIdx >= sLen)
193:                            return true;
194:                        else
195:                            return false;
196:                    }
197:
198:                    if (sIdx >= sLen && pattern.charAt(pIdx) != '*') {
199:                        return false;
200:                    }
201:
202:                    // Check for a '*' as the next pattern char.
203:                    // This is handled by a recursive call for
204:                    // each postfix of the name.
205:                    if (pattern.charAt(pIdx) == '*') {
206:                        if (++pIdx >= pLen)
207:                            return true;
208:
209:                        for (;;) {
210:                            if (FileUtilities.recurseMatchPattern(string,
211:                                    pattern, sIdx, pIdx))
212:                                return true;
213:
214:                            if (sIdx >= sLen)
215:                                return false;
216:
217:                            ++sIdx;
218:                        }
219:                    }
220:
221:                    // Check for '?' as the next pattern char.
222:                    // This matches the current character.
223:                    if (pattern.charAt(pIdx) == '?') {
224:                        ++pIdx;
225:                        ++sIdx;
226:                        continue;
227:                    }
228:
229:                    // Check for '[' as the next pattern char.
230:                    // This is a list of acceptable characters,
231:                    // which can include character ranges.
232:                    if (pattern.charAt(pIdx) == '[') {
233:                        for (++pIdx;; ++pIdx) {
234:                            if (pIdx >= pLen || pattern.charAt(pIdx) == ']')
235:                                return false;
236:
237:                            if (pattern.charAt(pIdx) == string.charAt(sIdx))
238:                                break;
239:
240:                            if (pIdx < (pLen - 1)
241:                                    && pattern.charAt(pIdx + 1) == '-') {
242:                                if (pIdx >= (pLen - 2))
243:                                    return false;
244:
245:                                char chStr = string.charAt(sIdx);
246:                                char chPtn = pattern.charAt(pIdx);
247:                                char chPtn2 = pattern.charAt(pIdx + 2);
248:
249:                                if ((chPtn <= chStr) && (chPtn2 >= chStr))
250:                                    break;
251:
252:                                if ((chPtn >= chStr) && (chPtn2 <= chStr))
253:                                    break;
254:
255:                                pIdx += 2;
256:                            }
257:                        }
258:
259:                        for (; pattern.charAt(pIdx) != ']'; ++pIdx) {
260:                            if (pIdx >= pLen) {
261:                                --pIdx;
262:                                break;
263:                            }
264:                        }
265:
266:                        ++pIdx;
267:                        ++sIdx;
268:                        continue;
269:                    }
270:
271:                    // Check for backslash escapes
272:                    // We just skip over them to match the next char.
273:                    if (pattern.charAt(pIdx) == '\\') {
274:                        if (++pIdx >= pLen)
275:                            return false;
276:                    }
277:
278:                    if (pIdx < pLen && sIdx < sLen)
279:                        if (pattern.charAt(pIdx) != string.charAt(sIdx))
280:                            return false;
281:
282:                    ++pIdx;
283:                    ++sIdx;
284:                }
285:            }
286:
287:            public static String getUserHomeDirectory() {
288:                String userDirName = System.getProperty("user.home", null);
289:
290:                if (userDirName == null) {
291:                    userDirName = System.getProperty("user.dir", null);
292:                }
293:
294:                return userDirName;
295:            }
296:
297:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.