001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.foundation.io;
022:
023: import java.io.*;
024:
025: import com.db4o.*;
026:
027: /**
028: * @sharpen.ignore
029: */
030: public class File4 {
031:
032: public static void rename(String oldPath, String newPath)
033: throws IOException {
034: if (!new File(oldPath).renameTo(new File(newPath))) {
035: throw new IOException("Could not rename '" + oldPath
036: + "' to '" + newPath + "'.");
037: }
038: }
039:
040: public static void copy(String source, String target)
041: throws IOException {
042: java.io.File sourceFile = new java.io.File(source);
043:
044: java.io.File targetFile = new java.io.File(target);
045: targetFile.mkdirs();
046: targetFile.delete();
047:
048: if (sourceFile.isDirectory()) {
049: copyDirectory(sourceFile, targetFile);
050: } else {
051: copyFile(sourceFile, targetFile);
052: }
053: }
054:
055: public static void copyFile(File source, File target)
056: throws IOException {
057: final int bufferSize = 64000;
058:
059: RandomAccessFile rafIn = new RandomAccessFile(source
060: .getAbsolutePath(), "r");
061: RandomAccessFile rafOut = new RandomAccessFile(target
062: .getAbsolutePath(), "rw");
063: final long len = rafIn.length();
064: final byte[] bytes = new byte[bufferSize];
065:
066: long totalRead = 0;
067: while (totalRead < len) {
068: int numRead = rafIn.read(bytes, 0, bufferSize);
069: rafOut.write(bytes, 0, numRead);
070: totalRead += numRead;
071: }
072:
073: rafIn.close();
074: rafOut.close();
075: }
076:
077: private static void copyDirectory(File source, File target)
078: throws IOException {
079: String[] files = source.list();
080: if (files != null) {
081: for (int i = 0; i < files.length; i++) {
082: copy(Path4.combine(source.getAbsolutePath(), files[i]),
083: Path4.combine(target.getAbsolutePath(),
084: files[i]));
085: }
086: }
087: }
088:
089: public static void delete(String fname) {
090: final File file = new File(fname);
091: if (!file.exists()) {
092: return;
093: }
094: if (!file.delete()) {
095: throw new Db4oIOException("Could not delete '" + fname
096: + "'.");
097: }
098: }
099:
100: public static boolean exists(String fname) {
101: return new File(fname).exists();
102: }
103:
104: public static void mkdirs(String path) {
105: new File(path).mkdirs();
106: }
107: }
|