001: /*
002: * $Id: FileUtil.java,v 1.5 2005/04/14 00:30:02 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2005 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.io;
042:
043: import java.io.File;
044: import java.io.FileInputStream;
045: import java.io.FileNotFoundException;
046: import java.io.FileOutputStream;
047: import java.io.IOException;
048: import java.nio.channels.FileLock;
049:
050: import org.axiondb.AxionException;
051:
052: /**
053: * Common file utils.
054: *
055: * @version $Revision: 1.5 $ $Date: 2005/04/14 00:30:02 $
056: * @author Ahimanikya Satapathy
057: */
058: public class FileUtil {
059:
060: public static void assertFileNotLocked(File file)
061: throws AxionException {
062: FileOutputStream fos = null;
063: FileLock lock = null;
064: try {
065: fos = new FileOutputStream(file);
066: lock = fos.getChannel().tryLock(0L, Long.MAX_VALUE, false);
067: if (lock == null) {
068: throw new AxionException(
069: "Unable to get a lock for file " + file);
070: }
071: } catch (FileNotFoundException fnf) {
072: //ignore
073: } catch (IOException ioe) {
074: //ignore
075: } finally {
076: try {
077: if (lock != null) {
078: lock.release();
079: }
080: if (fos != null) {
081: fos.close();
082: }
083: } catch (IOException ioe) {
084: // ignore
085: }
086: }
087: }
088:
089: /**
090: * Get rid of File file, whether a true file or dir.
091: */
092: public static boolean delete(File file) throws AxionException {
093: if (file == null || !file.exists()) {
094: return true;
095: }
096: try {
097: if (file.isFile()) {
098: return file.delete();
099: }
100: return fullyDelete(file);
101: } catch (IOException e) {
102: throw new AxionException("Unable to delete " + file);
103: }
104: }
105:
106: public static long getLength(File file) throws IOException {
107: FileInputStream fis = new FileInputStream(file);
108: long len = fis.getChannel().size();
109: fis.close();
110: return len;
111: }
112:
113: public static void renameFile(File dir, String old, String name,
114: String ext) {
115: File oldfile = new File(dir, old + ext);
116: File newfile = new File(dir, name + ext);
117: if (newfile.exists()) {
118: newfile.delete();
119: }
120: oldfile.renameTo(newfile);
121: }
122:
123: public static void renameToUpperCase(File dir) throws IOException {
124: File files[] = dir.listFiles();
125: if (files != null) {
126: for (int i = 0; i < files.length; i++) {
127: files[i].renameTo(new File(files[i].getParentFile(),
128: files[i].getName().toUpperCase()));
129: if (files[i].isDirectory()) {
130: renameToUpperCase(files[i]);
131: }
132: }
133: }
134: }
135:
136: /**
137: * Truncate file to a given length
138: *
139: * @see {@link FileChannel.truncate()}
140: */
141: public static void truncate(File file, long length)
142: throws AxionException {
143: FileOutputStream out = null;
144: try {
145: out = new FileOutputStream(file, true);
146: out.getChannel().truncate(length);
147: } catch (IOException e) {
148: throw new AxionException(e);
149: } finally {
150: try {
151: out.close();
152: } catch (Exception e) {
153: }
154: }
155: }
156:
157: private static boolean fullyDelete(File dir) throws IOException {
158: File contents[] = dir.listFiles();
159: if (contents != null) {
160: for (int i = 0; i < contents.length; i++) {
161: if (contents[i].isFile()) {
162: if (!contents[i].delete()) {
163: return false;
164: }
165: } else {
166: if (!fullyDelete(contents[i])) {
167: return false;
168: }
169: }
170: }
171: }
172: return dir.delete();
173: }
174:
175: }
|