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: }
|