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.store.fs;
007:
008: import java.io.IOException;
009: import java.io.InputStream;
010: import java.io.OutputStream;
011: import java.sql.SQLException;
012:
013: /**
014: * The file system is a storage abstraction.
015: */
016: public abstract class FileSystem {
017:
018: public static final String MEMORY_PREFIX = "memFS:";
019: public static final String MEMORY_PREFIX_LZF = "memLZF:";
020: public static final String DB_PREFIX = "jdbc:";
021: public static final String ZIP_PREFIX = "zip:";
022:
023: /**
024: * Get the file system object.
025: *
026: * @param fileName the file name or prefix
027: * @return the file system
028: */
029: public static FileSystem getInstance(String fileName) {
030: if (isInMemory(fileName)) {
031: return FileSystemMemory.getInstance();
032: } else if (fileName.startsWith(DB_PREFIX)) {
033: return FileSystemDatabase.getInstance(fileName);
034: } else if (fileName.startsWith(ZIP_PREFIX)) {
035: return FileSystemZip.getInstance();
036: }
037: return FileSystemDisk.getInstance();
038: }
039:
040: private static boolean isInMemory(String fileName) {
041: return fileName != null
042: && (fileName.startsWith(MEMORY_PREFIX) || fileName
043: .startsWith(MEMORY_PREFIX_LZF));
044: }
045:
046: /**
047: * Get the length of a file.
048: *
049: * @param fileName the file name
050: * @return the length in bytes
051: */
052: public abstract long length(String fileName);
053:
054: /**
055: * Rename a file if this is allowed.
056: *
057: * @param oldName the old fully qualified file name
058: * @param newName the new fully qualified file name
059: * @throws SQLException
060: */
061: public abstract void rename(String oldName, String newName)
062: throws SQLException;
063:
064: /**
065: * Create a new file.
066: *
067: * @param fileName the file name
068: * @return true if creating was successful
069: */
070: public abstract boolean createNewFile(String fileName)
071: throws SQLException;
072:
073: /**
074: * Checks if a file exists.
075: *
076: * @param fileName the file name
077: * @return true if it exists
078: */
079: public abstract boolean exists(String fileName);
080:
081: /**
082: * Delete a file.
083: *
084: * @param fileName the file name
085: */
086: public abstract void delete(String fileName) throws SQLException;
087:
088: /**
089: * Try to delete a file.
090: *
091: * @param fileName the file name
092: * @return true if it could be deleted
093: */
094: public abstract boolean tryDelete(String fileName);
095:
096: /**
097: * Create a new temporary file.
098: *
099: * @param prefix the file name prefix
100: * @param suffix the file name suffix
101: * @param deleteOnExit if the file should be deleted when the system exists
102: * @param inTempDir if the file should be stored in the temp file
103: * @return the file name
104: */
105: public abstract String createTempFile(String prefix, String suffix,
106: boolean deleteOnExit, boolean inTempDir) throws IOException;
107:
108: /**
109: * List the files in the given directory.
110: *
111: * @param directory the directory
112: * @return the list of fully qualified file names
113: */
114: public abstract String[] listFiles(String directory)
115: throws SQLException;
116:
117: /**
118: * Delete a directory or file and all subdirectories and files.
119: *
120: * @param directory the directory
121: */
122: public abstract void deleteRecursive(String directory)
123: throws SQLException;
124:
125: /**
126: * Check if a file is read-only.
127: *
128: * @param fileName the file name
129: * @return if it is read only
130: */
131: public abstract boolean isReadOnly(String fileName);
132:
133: /**
134: * Normalize a file name.
135: *
136: * @param fileName the file name
137: * @return the normalized file name
138: */
139: public abstract String normalize(String fileName)
140: throws SQLException;
141:
142: /**
143: * Get the parent directory of a file or directory.
144: *
145: * @param fileName the file or directory name
146: * @return the parent directory name
147: */
148: public abstract String getParent(String fileName);
149:
150: /**
151: * Check if it is a file or a directory.
152: *
153: * @param fileName the file or directory name
154: * @return true if it is a directory
155: */
156: public abstract boolean isDirectory(String fileName);
157:
158: /**
159: * Check if the file name includes a path.
160: *
161: * @param fileName the file name
162: * @return if the file name is absolute
163: */
164: public abstract boolean isAbsolute(String fileName);
165:
166: /**
167: * Get the absolute file name.
168: *
169: * @param fileName the file name
170: * @return the absolute file name
171: */
172: public abstract String getAbsolutePath(String fileName);
173:
174: /**
175: * Get the last modified date of a file
176: *
177: * @param fileName the file name
178: * @return the last modified date
179: */
180: public abstract long getLastModified(String fileName);
181:
182: /**
183: * Check if the file is writable.
184: *
185: * @param fileName the file name
186: * @return if the file is writable
187: */
188: public abstract boolean canWrite(String fileName);
189:
190: /**
191: * Copy a file from one directory to another, or to another file.
192: *
193: * @param original the original file name
194: * @param copy the file name of the copy
195: */
196: public abstract void copy(String original, String copy)
197: throws SQLException;
198:
199: /**
200: * Create all required directories.
201: *
202: * @param directoryName the directory name
203: */
204: public void mkdirs(String directoryName) throws SQLException {
205: createDirs(directoryName + "/x");
206: }
207:
208: /**
209: * Create all required directories that are required for this file.
210: *
211: * @param fileName the file name (not directory name)
212: */
213: public abstract void createDirs(String fileName)
214: throws SQLException;
215:
216: /**
217: * Get the file name (without directory part).
218: *
219: * @param name the directory and file name
220: * @return just the file name
221: */
222: public abstract String getFileName(String name) throws SQLException;
223:
224: /**
225: * Check if a file starts with a given prefix.
226: *
227: * @param fileName the complete file name
228: * @param prefix the prefix
229: * @return true if it starts with the prefix
230: */
231: public abstract boolean fileStartsWith(String fileName,
232: String prefix);
233:
234: /**
235: * Create an output stream to write into the file.
236: *
237: * @param fileName the file name
238: * @param append if true, the file will grow, if false, the file will be
239: * truncated first
240: * @return the output stream
241: */
242: public abstract OutputStream openFileOutputStream(String fileName,
243: boolean append) throws SQLException;
244:
245: /**
246: * Open a random access file object.
247: *
248: * @param fileName the file name
249: * @param mode the access mode. Supported are r, rw, rws, rwd
250: * @return the file object
251: */
252: public abstract FileObject openFileObject(String fileName,
253: String mode) throws IOException;
254:
255: /**
256: * Create an input stream to read from the file.
257: *
258: * @param fileName the file name
259: * @return the input stream
260: */
261: public abstract InputStream openFileInputStream(String fileName)
262: throws IOException;
263: }
|