001:///////////////////////////////////////////////////////////////////////////////
002://
003:// Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004://
005:// All Rights Reserved
006://
007:// This program is free software; you can redistribute it and/or modify
008:// it under the terms of the GNU General Public License and GNU Library
009:// General Public License as published by the Free Software Foundation;
010:// either version 2, or (at your option) any later version.
011://
012:// This program is distributed in the hope that it will be useful,
013:// but WITHOUT ANY WARRANTY; without even the implied warranty of
014:// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015:// GNU General Public License and GNU Library General Public License
016:// for more details.
017://
018:// You should have received a copy of the GNU General Public License
019:// and GNU Library General Public License along with this program; if
020:// not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021:// MA 02139, USA.
022://
023:///////////////////////////////////////////////////////////////////////////////
024:package org.myoodb.core;
025:
026:import java.io.*;
027:import java.nio.channels.*;
028:
029:public final class FileHelper
030:{
031: private static final org.myoodb.util.Logger LOGGER = org.myoodb.util.Logger.getLogger(FileHelper.class);
032:
033: private static volatile String s_fileWriteMode = "@fileWriteMode@";
034: private static volatile boolean s_memoryResidentFlag = @gameConfiguration@;
035:
036: public static void setFileWriteMode(String mode)
037: {
038: s_fileWriteMode = mode;
039: }
040:
041: public static String getFileWriteMode()
042: {
043: return s_fileWriteMode;
044: }
045:
046: public static void setMemoryResidentFlag(boolean flag)
047: {
048: s_memoryResidentFlag = flag;
049: }
050:
051: public static boolean getMemoryResidentFlag()
052: {
053: return s_memoryResidentFlag;
054: }
055:
056: public static void transfer(File source, File target) throws IOException
057: {
058: InputStream in = null;
059: RandomAccessFile raFile = null;
060: try
061: {
062: in = new FileInputStream(source);
063: raFile = new RandomAccessFile(target, FileHelper.getFileWriteMode());
064:
065: int offset = 0;
066: int size = in.available();
067: byte[] buffer = new byte[size];
068:
069: while (offset < size)
070: {
071: int bytesRead = in.read(buffer, offset, (size - offset));
072: if (bytesRead == -1)
073: {
074: break;
075: }
076:
077: raFile.write(buffer, offset, (size - offset));
078: offset += bytesRead;
079: }
080: }
081: finally
082: {
083: if (raFile != null)
084: {
085: raFile.close();
086: }
087: if (in != null)
088: {
089: in.close();
090: }
091: }
092: }
093:
094: public static void copy(String sourcePath, String targetPath, boolean ignoreThrow) throws IOException
095: {
096: File source = new File(sourcePath);
097: File target = new File(targetPath);
098:
099: if (LOGGER.isDebugEnabled() == true)
100: {
101: LOGGER.debug("Copying \"source\" to \"target\" - " + source + " to " + target);
102: }
103:
104: if (source.exists() == false)
105: {
106: throw new IOException("File not found: " + source);
107: }
108:
109: if (source.isDirectory() == true)
110: {
111: if (target.exists() == false)
112: {
113: if (target.mkdirs() == false)
114: {
115: throw new IOException("Failed to create: " + target);
116: }
117: }
118:
119: String list[] = source.list();
120: for (int i = 0; i < list.length; i++)
121: {
122: try
123: {
124: copy(sourcePath + File.separator + list[i], targetPath + File.separator + list[i], ignoreThrow);
125: }
126: catch (IOException e)
127: {
128: LOGGER.warn("Unable to copy: " + e.getMessage());
129:
130: if (ignoreThrow == false)
131: {
132: throw e;
133: }
134: }
135:
136: // XXX: pace copy in case the copy is large
137: Thread.yield();
138: }
139: }
140: else
141: {
142: transfer(source, target);
143: }
144: }
145:
146: public static void rename(File source, File target) throws IOException
147: {
148: for (int i = 0; i < 10; i++)
149: {
150: if (source.renameTo(target) == true)
151: {
152: return;
153: }
154:
155: LOGGER.warn("Unable to rename(" + i + "): " + source + " to " + target);
156:
157: System.runFinalization();
158: System.gc();
159:
160: try
161: {
162: Thread.sleep(100);
163: }
164: catch (InterruptedException e)
165: {
166: // nothing to do
167: }
168: }
169:
170: //throw new IOException("Unable to rename \"source\" to \"target\" - " + source + " to " + target);
171:
172: LOGGER.error("(Exiting) Unable to rename \"source\" to \"target\" - " + source + " to " + target);
173: System.exit(1);
174: }
175:
176: public static void rename(String sourcePath, String targetPath) throws IOException
177: {
178: File source = new File(sourcePath);
179: File target = new File(targetPath);
180:
181: rename(source, target);
182: }
183:
184: public static void delete(File file) throws IOException
185: {
186: for (int i = 0; i < 10; i++)
187: {
188: if (file.delete() == true)
189: {
190: return;
191: }
192:
193: LOGGER.warn("Unable to delete(" + i + "): " + file);
194:
195: System.runFinalization();
196: System.gc();
197:
198: try
199: {
200: Thread.sleep(100);
201: }
202: catch (InterruptedException e)
203: {
204: // nothing to do
205: }
206: }
207:
208: //throw new IOException("Unable to delete \"file\" - " + file);
209:
210: LOGGER.error("(Exiting) Unable to delete \"file\" - " + file);
211: System.exit(1);
212: }
213:
214: public static void delete(String name) throws IOException
215: {
216: File file = new File(name);
217: if (file.exists() == true)
218: {
219: String[] files = file.list();
220:
221: if ((files != null) && (files.length != 0))
222: {
223: for (int i = 0; i < files.length; i++)
224: {
225: File subfile = new File(name, files[i]);
226:
227: if (subfile.isDirectory() == true)
228: {
229: delete(subfile.getPath());
230: }
231: else
232: {
233: delete(subfile);
234: }
235: }
236: }
237:
238: delete(file);
239: }
240: }
241:}
|