Source Code Cross Referenced for FileUtils.java in  » Database-DBMS » h2database » org » h2 » 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 » Database DBMS » h2database » org.h2.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003:         * (http://h2database.com/html/license.html).
004:         * Initial Developer: H2 Group
005:         */
006:        package org.h2.util;
007:
008:        import java.io.BufferedWriter;
009:        import java.io.File;
010:        import java.io.IOException;
011:        import java.io.InputStream;
012:        import java.io.OutputStream;
013:        import java.io.OutputStreamWriter;
014:        import java.io.RandomAccessFile;
015:        import java.io.UnsupportedEncodingException;
016:        import java.io.Writer;
017:        import java.sql.SQLException;
018:        import java.util.Properties;
019:
020:        import org.h2.constant.SysProperties;
021:        import org.h2.message.Message;
022:        import org.h2.message.TraceSystem;
023:        import org.h2.store.fs.FileSystem;
024:
025:        /**
026:         * This utility class supports basic operations on files
027:         */
028:        public class FileUtils {
029:
030:            public static void setLength(RandomAccessFile file, long newLength)
031:                    throws IOException {
032:                try {
033:                    trace("setLength", null, file);
034:                    file.setLength(newLength);
035:                } catch (IOException e) {
036:                    long length = file.length();
037:                    if (newLength < length) {
038:                        throw e;
039:                    } else {
040:                        long pos = file.getFilePointer();
041:                        file.seek(length);
042:                        long remaining = newLength - length;
043:                        int maxSize = 1024 * 1024;
044:                        int block = (int) Math.min(remaining, maxSize);
045:                        byte[] buffer = new byte[block];
046:                        while (remaining > 0) {
047:                            int write = (int) Math.min(remaining, maxSize);
048:                            file.write(buffer, 0, write);
049:                            remaining -= write;
050:                        }
051:                        file.seek(pos);
052:                    }
053:                }
054:            }
055:
056:            public static synchronized Properties loadProperties(String fileName)
057:                    throws IOException {
058:                Properties prop = new SortedProperties();
059:                if (exists(fileName)) {
060:                    InputStream in = openFileInputStream(fileName);
061:                    try {
062:                        prop.load(in);
063:                    } finally {
064:                        in.close();
065:                    }
066:                }
067:                return prop;
068:            }
069:
070:            public static boolean getBooleanProperty(Properties prop,
071:                    String key, boolean def) {
072:                String value = prop.getProperty(key, "" + def);
073:                try {
074:                    return Boolean.valueOf(value).booleanValue();
075:                } catch (Exception e) {
076:                    TraceSystem.traceThrowable(e);
077:                    return def;
078:                }
079:            }
080:
081:            public static int getIntProperty(Properties prop, String key,
082:                    int def) {
083:                String value = prop.getProperty(key, "" + def);
084:                try {
085:                    return MathUtils.decodeInt(value);
086:                } catch (Exception e) {
087:                    TraceSystem.traceThrowable(e);
088:                    return def;
089:                }
090:            }
091:
092:            public static String getFileInUserHome(String fileName) {
093:                String userDir = SysProperties.USER_HOME;
094:                if (userDir == null) {
095:                    return fileName;
096:                }
097:                File file = new File(userDir, fileName);
098:                return file.getAbsolutePath();
099:            }
100:
101:            public static void trace(String method, String fileName, Object o) {
102:                if (SysProperties.TRACE_IO) {
103:                    System.out.println("FileUtils." + method + " " + fileName
104:                            + " " + o);
105:                }
106:            }
107:
108:            public static String getFileName(String name) throws SQLException {
109:                return FileSystem.getInstance(name).getFileName(name);
110:            }
111:
112:            public static String normalize(String fileName) throws SQLException {
113:                return FileSystem.getInstance(fileName).normalize(fileName);
114:            }
115:
116:            public static void tryDelete(String fileName) {
117:                FileSystem.getInstance(fileName).tryDelete(fileName);
118:            }
119:
120:            public static boolean isReadOnly(String fileName) {
121:                return FileSystem.getInstance(fileName).isReadOnly(fileName);
122:            }
123:
124:            public static boolean exists(String fileName) {
125:                return FileSystem.getInstance(fileName).exists(fileName);
126:            }
127:
128:            public static long length(String fileName) {
129:                return FileSystem.getInstance(fileName).length(fileName);
130:            }
131:
132:            /**
133:             * Create a new temporary file.
134:             * 
135:             * @param prefix the prefix of the file name (including directory name if
136:             *            required)
137:             * @param suffix the suffix
138:             * @param deleteOnExit if the file should be deleted when the virtual
139:             *            machine exists
140:             * @param inTempDir if the file should be stored in the temporary directory
141:             * @return the name of the created file
142:             */
143:            public static String createTempFile(String prefix, String suffix,
144:                    boolean deleteOnExit, boolean inTempDir)
145:                    throws IOException, SQLException {
146:                return FileSystem.getInstance(prefix).createTempFile(prefix,
147:                        suffix, deleteOnExit, inTempDir);
148:            }
149:
150:            public static String getParent(String fileName) {
151:                return FileSystem.getInstance(fileName).getParent(fileName);
152:            }
153:
154:            public static String[] listFiles(String path) throws SQLException {
155:                return FileSystem.getInstance(path).listFiles(path);
156:            }
157:
158:            public static boolean isDirectory(String fileName) {
159:                return FileSystem.getInstance(fileName).isDirectory(fileName);
160:            }
161:
162:            public static boolean isAbsolute(String fileName) {
163:                return FileSystem.getInstance(fileName).isAbsolute(fileName);
164:            }
165:
166:            public static String getAbsolutePath(String fileName) {
167:                return FileSystem.getInstance(fileName).getAbsolutePath(
168:                        fileName);
169:            }
170:
171:            public static Writer openFileWriter(String fileName, boolean append)
172:                    throws SQLException {
173:                OutputStream out = FileSystem.getInstance(fileName)
174:                        .openFileOutputStream(fileName, append);
175:                try {
176:                    return new BufferedWriter(new OutputStreamWriter(out,
177:                            "UTF-8"));
178:                } catch (UnsupportedEncodingException e) {
179:                    throw Message.convert(e);
180:                }
181:            }
182:
183:            public static boolean fileStartsWith(String fileName, String prefix) {
184:                return FileSystem.getInstance(fileName).fileStartsWith(
185:                        fileName, prefix);
186:            }
187:
188:            public static InputStream openFileInputStream(String fileName)
189:                    throws IOException {
190:                return FileSystem.getInstance(fileName).openFileInputStream(
191:                        fileName);
192:            }
193:
194:            public static OutputStream openFileOutputStream(String fileName,
195:                    boolean append) throws SQLException {
196:                return FileSystem.getInstance(fileName).openFileOutputStream(
197:                        fileName, append);
198:            }
199:
200:            public static void rename(String oldName, String newName)
201:                    throws SQLException {
202:                FileSystem.getInstance(oldName).rename(oldName, newName);
203:            }
204:
205:            public static void createDirs(String fileName) throws SQLException {
206:                FileSystem.getInstance(fileName).createDirs(fileName);
207:            }
208:
209:            public static void delete(String fileName) throws SQLException {
210:                FileSystem.getInstance(fileName).delete(fileName);
211:            }
212:
213:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.