Methods Summary |
---|
public static void | backupFile(java.lang.String _filename, boolean _make_copy)Backup the given file to filename.bak, removing the old .bak file if necessary.
If _make_copy is true, the original file will copied to backup, rather than moved.
backupFile( new File( _filename ), _make_copy );
|
public static void | backupFile(java.io.File _file, boolean _make_copy)Backup the given file to filename.bak, removing the old .bak file if necessary.
If _make_copy is true, the original file will copied to backup, rather than moved.
if ( _file.length() > 0L ) {
File bakfile = new File( _file.getAbsolutePath() + ".bak" );
if ( bakfile.exists() ) bakfile.delete();
if ( _make_copy ) {
copyFile( _file, bakfile );
}
else {
_file.renameTo( bakfile );
}
}
|
public static java.io.File | canonise(java.io.File file)
try {return file.getCanonicalFile();}
catch (IOException ioe) {return file;}
|
public static java.lang.String | convertOSSpecificChars(java.lang.String file_name_in)
// this rule originally from DiskManager
char[] chars = file_name_in.toCharArray();
for (int i=0;i<chars.length;i++){
if ( chars[i] == '"" ){
chars[i] = '\'";
}
}
if ( !Constants.isOSX ){
if ( Constants.isWindows ){
// this rule originally from DiskManager
// The definitive list of characters permitted for Windows is defined here:
// http://support.microsoft.com/kb/q120138/
String not_allowed = "\\/:?*<>|";
for (int i=0;i<chars.length;i++){
if (not_allowed.indexOf(chars[i]) != -1) {
chars[i] = '_";
}
}
}
// '/' is valid in mac file names, replace with space
// so it seems are cr/lf
for (int i=0;i<chars.length;i++){
char c = chars[i];
if ( c == '/" || c == '\r" || c == '\n" ){
chars[i] = ' ";
}
}
}
String file_name_out = new String(chars);
try{
// mac file names can end in space - fix this up by getting
// the canonical form which removes this on Windows
// however, for soem reason getCanonicalFile can generate high CPU usage on some user's systems
// in java.io.Win32FileSystem.canonicalize
// so changing this to only be used on non-windows
if ( Constants.isWindows ){
while( file_name_out.endsWith( " " )){
file_name_out = file_name_out.substring(0,file_name_out.length()-1);
}
}else{
String str = new File(file_name_out).getCanonicalFile().toString();
int p = str.lastIndexOf( File.separator );
file_name_out = str.substring(p+1);
}
}catch( Throwable e ){
// ho hum, carry on, it'll fail later
//e.printStackTrace();
}
//System.out.println( "convertOSSpecificChars: " + file_name_in + " ->" + file_name_out );
return( file_name_out );
|
public static boolean | copyFile(java.lang.String _source_name, java.lang.String _dest_name)Copy the given source file to the given destination file.
Returns file copy success or not.
return copyFile( new File(_source_name), new File(_dest_name));
|
public static boolean | copyFile(java.io.File _source, java.io.File _dest)Copy the given source file to the given destination file.
Returns file copy success or not.
try {
copyFile( new FileInputStream( _source ), new FileOutputStream( _dest ) );
return true;
}
catch( Throwable e ) {
Debug.printStackTrace( e );
return false;
}
|
public static boolean | copyFile(java.io.File _source, java.io.OutputStream _dest, boolean closeInputStream)
try {
copyFile( new FileInputStream( _source ), _dest, closeInputStream );
return true;
}
catch( Throwable e ) {
Debug.printStackTrace( e );
return false;
}
|
public static void | copyFile(java.io.InputStream _source, java.io.File _dest)copys the input stream to the file. always closes the input stream
FileOutputStream dest = null;
boolean close_input = true;
try{
dest = new FileOutputStream(_dest);
// copyFile will close from now on, we don't need to
close_input = false;
copyFile( _source, dest, true );
}finally{
try{
if(close_input){
_source.close();
}
}catch( IOException e ){
}
if ( dest != null ){
dest.close();
}
}
|
public static void | copyFile(java.io.InputStream is, java.io.OutputStream os)
copyFile(is,os,true);
|
public static void | copyFile(java.io.InputStream is, java.io.OutputStream os, boolean closeInputStream)
try{
if ( !(is instanceof BufferedInputStream )){
is = new BufferedInputStream(is);
}
byte[] buffer = new byte[65536*2];
while(true){
int len = is.read(buffer);
if ( len == -1 ){
break;
}
os.write( buffer, 0, len );
}
}finally{
try{
if(closeInputStream){
is.close();
}
}catch( IOException e ){
}
os.close();
}
|
public static void | copyFileOrDirectory(java.io.File from_file_or_dir, java.io.File to_parent_dir)
if ( !from_file_or_dir.exists()){
throw( new IOException( "File '" + from_file_or_dir.toString() + "' doesn't exist" ));
}
if ( !to_parent_dir.exists()){
throw( new IOException( "File '" + to_parent_dir.toString() + "' doesn't exist" ));
}
if ( !to_parent_dir.isDirectory()){
throw( new IOException( "File '" + to_parent_dir.toString() + "' is not a directory" ));
}
if ( from_file_or_dir.isDirectory()){
File[] files = from_file_or_dir.listFiles();
File new_parent = new File( to_parent_dir, from_file_or_dir.getName());
FileUtil.mkdirs(new_parent);
for (int i=0;i<files.length;i++){
File from_file = files[i];
copyFileOrDirectory( from_file, new_parent );
}
}else{
File target = new File( to_parent_dir, from_file_or_dir.getName());
if ( !copyFile( from_file_or_dir, target )){
throw( new IOException( "File copy from " + from_file_or_dir + " to " + target + " failed" ));
}
}
|
public static boolean | deleteWithRecycle(java.io.File file)
if ( COConfigurationManager.getBooleanParameter("Move Deleted Data To Recycle Bin" )){
try{
final PlatformManager platform = PlatformManagerFactory.getPlatformManager();
if (platform.hasCapability(PlatformManagerCapabilities.RecoverableFileDelete)){
platform.performRecoverableFileDelete( file.getAbsolutePath());
return( true );
}else{
return( file.delete());
}
}catch( PlatformManagerException e ){
return( file.delete());
}
}else{
return( file.delete());
}
|
public static java.io.File | getApplicationFile(java.lang.String filename)
String path = SystemProperties.getApplicationPath();
if(Constants.isOSX) {
path = path + "/" + SystemProperties.getApplicationName() + ".app/Contents/";
}
return new File(path, filename);
|
public static java.lang.String | getCanonicalFileName(java.lang.String filename)
// Sometimes Windows use filename in 8.3 form and cannot
// match .torrent extension. To solve this, canonical path
// is used to get back the long form
String canonicalFileName = filename;
try {
canonicalFileName = new File(filename).getCanonicalPath();
}
catch (IOException ignore) {}
return canonicalFileName;
|
public static java.lang.String | getExtension(java.lang.String fName)
final int fileSepIndex = fName.lastIndexOf(File.separator);
final int fileDotIndex = fName.lastIndexOf('.");
if (fileSepIndex == fName.length() - 1 || fileDotIndex == -1
|| fileSepIndex > fileDotIndex) {
return "";
}
return fName.substring(fileDotIndex);
|
public static java.io.File | getFileOrBackup(java.lang.String _filename)Returns the file handle for the given filename or it's
equivalent .bak backup file if the original doesn't exist
or is 0-sized. If neither the original nor the backup are
available, a null handle is returned.
try {
File file = new File( _filename );
//make sure the file exists and isn't zero-length
if ( file.length() <= 1L ) {
//if so, try using the backup file
File bakfile = new File( _filename + ".bak" );
if ( bakfile.length() <= 1L ) {
return null;
}
else return bakfile;
}
else return file;
}
catch (Exception e) {
Debug.out( e );
return null;
}
|
public static long | getFileOrDirectorySize(java.io.File file)
if ( file.isFile()){
return( file.length());
}else{
long res = 0;
File[] files = file.listFiles();
if ( files != null ){
for (int i=0;i<files.length;i++){
res += getFileOrDirectorySize( files[i] );
}
}
return( res );
}
|
public static java.io.File | getJarFileFromClass(java.lang.Class cla)
try{
String str = cla.getName();
str = str.replace( '.", '/" ) + ".class";
URL url = cla.getClassLoader().getResource( str );
if ( url != null ){
String url_str = url.toExternalForm();
if ( url_str.startsWith("jar:file:")){
File jar_file = FileUtil.getJarFileFromURL(url_str);
if ( jar_file.exists()){
return( jar_file );
}
}
}
}catch( Throwable e ){
Debug.printStackTrace(e);
}
return( null );
|
public static java.io.File | getJarFileFromURL(java.lang.String url_str)
if (url_str.startsWith("jar:file:")) {
// java web start returns a url like "jar:file:c:/sdsd" which then fails as the file
// part doesn't start with a "/". Add it in!
// here's an example
// jar:file:C:/Documents%20and%20Settings/stuff/.javaws/cache/http/Dparg.homeip.net/P9090/DMazureus-jnlp/DMlib/XMAzureus2.jar1070487037531!/org/gudy/azureus2/internat/MessagesBundle.properties
// also on Mac we don't get the spaces escaped
url_str = url_str.replaceAll(" ", "%20" );
if ( !url_str.startsWith("jar:file:/")){
url_str = "jar:file:/".concat(url_str.substring(9));
}
try{
// you can see that the '!' must be present and that we can safely use the last occurrence of it
int posPling = url_str.lastIndexOf('!");
String jarName = url_str.substring(4, posPling);
// System.out.println("jarName: " + jarName);
URI uri = URI.create(jarName);
File jar = new File(uri);
return( jar );
}catch( Throwable e ){
Debug.printStackTrace( e );
}
}
return( null );
|
private static void | getReservedFileHandles()
try{
class_mon.enter();
while( reserved_file_handles.size() > 0 ){
// System.out.println( "releasing reserved file handle");
InputStream is = (InputStream)reserved_file_handles.remove(0);
try{
is.close();
}catch( Throwable e ){
Debug.printStackTrace( e );
}
}
}finally{
class_mon.exit();
}
|
public static final long | getUsableSpace(java.io.File f)
try
{
return ((Long)reflectOnUsableSpace.invoke(f, null)).longValue();
} catch (Exception e)
{
return -1;
}
|
public static final boolean | getUsableSpaceSupported()
return reflectOnUsableSpace != null;
|
public static java.io.File | getUserFile(java.lang.String filename)
return new File(SystemProperties.getUserPath(), filename);
|
public static boolean | isAncestorOf(java.io.File parent, java.io.File child)
try
{
reflectOnUsableSpace = File.class.getMethod("getUsableSpace", (Class[])null);
} catch (NoSuchMethodException e)
{
reflectOnUsableSpace = null;
}
parent = canonise(parent);
child = canonise(child);
if (parent.equals(child)) {return true;}
String parent_s = parent.getPath();
String child_s = child.getPath();
if (parent_s.charAt(parent_s.length()-1) != File.separatorChar) {
parent_s += File.separatorChar;
}
return child_s.startsWith(parent_s);
|
public static boolean | mkdirs(java.io.File f)Makes Directories as long as the directory isn't directly in Volumes (OSX)
if (Constants.isOSX) {
Pattern pat = Pattern.compile("^(/Volumes/[^/]+)");
Matcher matcher = pat.matcher(f.getParent());
if (matcher.find()) {
String sVolume = matcher.group();
File fVolume = new File(sVolume);
if (!fVolume.isDirectory()) {
Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING, sVolume
+ " is not mounted or not available."));
return false;
}
}
}
return f.mkdirs();
|
public static java.lang.String | readFileAsString(java.io.File file, int size_limit, java.lang.String charset)
FileInputStream fis = new FileInputStream(file);
try {
return readInputStreamAsString(fis, size_limit, charset);
} finally {
fis.close();
}
|
public static java.lang.String | readFileAsString(java.io.File file, int size_limit)
FileInputStream fis = new FileInputStream(file);
try {
return readInputStreamAsString(fis, size_limit);
} finally {
fis.close();
}
|
public static java.lang.String | readFileEndAsString(java.io.File file, int size_limit)
FileInputStream fis = new FileInputStream( file );
try{
if (file.length() > size_limit) {
fis.skip(file.length() - size_limit);
}
StringBuffer result = new StringBuffer(1024);
byte[] buffer = new byte[1024];
while( true ){
int len = fis.read( buffer );
if ( len <= 0 ){
break;
}
result.append( new String( buffer, 0, len, "ISO-8859-1" ));
if ( result.length() > size_limit ){
result.setLength( size_limit );
break;
}
}
return( result.toString());
}finally{
fis.close();
}
|
public static java.lang.String | readInputStreamAsString(java.io.InputStream is, int size_limit)
return readInputStreamAsString(is, size_limit, "ISO-8859-1");
|
public static java.lang.String | readInputStreamAsString(java.io.InputStream is, int size_limit, java.lang.String charSet)
StringBuffer result = new StringBuffer(1024);
byte[] buffer = new byte[1024];
while (true) {
int len = is.read(buffer);
if (len <= 0) {
break;
}
result.append(new String(buffer, 0, len, charSet));
if (size_limit >= 0 && result.length() > size_limit) {
result.setLength(size_limit);
break;
}
}
return (result.toString());
|
public static java.util.Map | readResilientConfigFile(java.lang.String file_name)
File parent_dir = new File(SystemProperties.getUserPath());
boolean use_backups = COConfigurationManager.getBooleanParameter("Use Config File Backups" );
return( readResilientFile( parent_dir, file_name, use_backups ));
|
public static java.util.Map | readResilientConfigFile(java.lang.String file_name, boolean use_backups)
File parent_dir = new File(SystemProperties.getUserPath());
if ( !use_backups ){
// override if a backup file exists. This is needed to cope with backups
// of the main config file itself as when boostrapping we can't get the
// "use backups"
if ( new File( parent_dir, file_name + ".bak").exists()){
use_backups = true;
}
}
return( readResilientFile( parent_dir, file_name, use_backups ));
|
public static java.util.Map | readResilientFile(java.io.File file)
return( readResilientFile( file.getParentFile(),file.getName(),false));
|
public static java.util.Map | readResilientFile(java.io.File parent_dir, java.lang.String file_name, boolean use_backup)
File backup_file = new File( parent_dir, file_name + ".bak" );
if ( use_backup ){
use_backup = backup_file.exists();
}
// if we've got a backup, don't attempt recovery here as the .bak file may be
// fully OK
Map res = readResilientFileSupport( parent_dir, file_name, !use_backup );
if ( res == null && use_backup ){
// try backup without recovery
res = readResilientFileSupport( parent_dir, file_name + ".bak", false );
if ( res != null ){
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_WARNING,
"Backup file '" + backup_file
+ "' has been used for recovery purposes"));
// rewrite the good data, don't use backups here as we want to
// leave the original backup in place for the moment
writeResilientFile( parent_dir, file_name, res, false );
}else{
// neither main nor backup file ok, retry main file with recovery
res = readResilientFileSupport( parent_dir, file_name, true );
}
}
if ( res == null ){
res = new HashMap();
}
return( res );
|
private static java.util.Map | readResilientFile(java.lang.String original_file_name, java.io.File parent_dir, java.lang.String file_name, int fail_count, boolean recovery_mode)
// logging in here is only done during "non-recovery" mode to prevent subsequent recovery
// attempts logging everything a second time.
// recovery-mode allows the decoding process to "succeed" with a partially recovered file
boolean using_backup = file_name.endsWith(".saving");
File file = new File( parent_dir, file_name );
//make sure the file exists and isn't zero-length
if ( (!file.exists()) || file.length() <= 1L ){
if ( using_backup ){
if ( !recovery_mode ){
if ( fail_count == 1 ){
// we only alert the user if at least one file was found and failed
// otherwise it could be start of day when neither file exists yet
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_ERROR,
"Load of '" + original_file_name + "' fails, no usable file or backup"));
}else{
// drop this log, it doesn't really help to inform about the failure to
// find a .saving file
//if (Logger.isEnabled())
// Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR, "Load of '"
// + file_name + "' fails, file not found"));
}
}
return( null );
}
if ( !recovery_mode ){
// kinda confusing log this as we get it under "normal" circumstances (loading a config
// file that doesn't exist legitimately, e.g. shares or bad-ips
// if (Logger.isEnabled())
// Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR, "Load of '"
// + file_name + "' failed, " + "file not found or 0-sized."));
}
return( readResilientFile( original_file_name, parent_dir, file_name + ".saving", 0, recovery_mode ));
}
BufferedInputStream bin = null;
try{
int retry_limit = 5;
while(true){
try{
bin = new BufferedInputStream( new FileInputStream(file), 16384 );
break;
}catch( IOException e ){
if ( --retry_limit == 0 ){
throw( e );
}
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "Failed to open '" + file.toString() + "', retrying", e));
Thread.sleep(500);
}
}
BDecoder decoder = new BDecoder();
if ( recovery_mode ){
decoder.setRecoveryMode( true );
}
Map res = decoder.decodeStream(bin);
if ( using_backup && !recovery_mode ){
Logger.log(
new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_WARNING,
"Load of '" + original_file_name + "' had to revert to backup file"));
}
return( res );
}catch( Throwable e ){
Debug.printStackTrace( e );
try {
if (bin != null){
bin.close();
bin = null;
}
} catch (Exception x) {
Debug.printStackTrace( x );
}
// if we're not recovering then backup the file
if ( !recovery_mode ){
// Occurs when file is there but b0rked
// copy it in case it actually contains useful data, so it won't be overwritten next save
File bad;
int bad_id = 0;
while(true){
File test = new File( parent_dir, file.getName() + ".bad" + (bad_id==0?"":(""+bad_id)));
if ( !test.exists()){
bad = test;
break;
}
bad_id++;
}
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING, "Read of '"
+ original_file_name + "' failed, decoding error. " + "Renaming to "
+ bad.getName()));
// copy it so its left in place for possible recovery
copyFile( file, bad );
}
if ( using_backup ){
if ( !recovery_mode ){
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_ERROR,
"Load of '" + original_file_name + "' fails, no usable file or backup"));
}
return( null );
}
return( readResilientFile( original_file_name, parent_dir, file_name + ".saving", 1, recovery_mode ));
}finally{
try {
if (bin != null){
bin.close();
}
}catch (Exception e) {
Debug.printStackTrace( e );
}
}
|
private static java.util.Map | readResilientFileSupport(java.io.File parent_dir, java.lang.String file_name, boolean attempt_recovery)
try{
class_mon.enter();
try{
getReservedFileHandles();
Map res = null;
try{
res = readResilientFile( file_name, parent_dir, file_name, 0, false );
}catch( Throwable e ){
// ignore, it'll be rethrown if we can't recover below
}
if ( res == null && attempt_recovery ){
res = readResilientFile( file_name, parent_dir, file_name, 0, true );
if ( res != null ){
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_WARNING,
"File '" + file_name + "' has been partially recovered, "
+ "information may have been lost!"));
}
}
return( res );
}catch( Throwable e ){
Debug.printStackTrace( e );
return( null );
}finally{
releaseReservedFileHandles();
}
}finally{
class_mon.exit();
}
|
public static boolean | recursiveDelete(java.io.File f)Deletes the given dir and all files/dirs underneath
String defSaveDir = COConfigurationManager.getStringParameter("Default save path");
String moveToDir = COConfigurationManager.getStringParameter("Completed Files Directory", "");
try{
moveToDir = new File(moveToDir).getCanonicalPath();
}catch( Throwable e ){
}
try{
defSaveDir = new File(defSaveDir).getCanonicalPath();
}catch( Throwable e ){
}
try {
if (f.getCanonicalPath().equals(moveToDir)) {
System.out.println("FileUtil::recursiveDelete:: not allowed to delete the MoveTo dir !");
return( false );
}
if (f.getCanonicalPath().equals(defSaveDir)) {
System.out.println("FileUtil::recursiveDelete:: not allowed to delete the default data dir !");
return( false );
}
if (f.isDirectory()) {
File[] files = f.listFiles();
for (int i = 0; i < files.length; i++) {
if ( !recursiveDelete(files[i])){
return( false );
}
}
if ( !f.delete()){
return( false );
}
}
else {
if ( !f.delete()){
return( false );
}
}
} catch (Exception ignore) {/*ignore*/}
return( true );
|
protected static void | recursiveEmptyDirDelete(java.io.File f, java.util.Set ignore_set, boolean log_warnings)
try {
String defSaveDir = COConfigurationManager.getStringParameter("Default save path");
String moveToDir = COConfigurationManager.getStringParameter("Completed Files Directory", "");
if ( defSaveDir.trim().length() > 0 ){
defSaveDir = new File(defSaveDir).getCanonicalPath();
}
if ( moveToDir.trim().length() > 0 ){
moveToDir = new File(moveToDir).getCanonicalPath();
}
if ( f.isDirectory()){
File[] files = f.listFiles();
if ( files == null ){
if (log_warnings ){
Debug.out("Empty folder delete: failed to list contents of directory " + f );
}
return;
}
for (int i = 0; i < files.length; i++) {
File x = files[i];
if ( x.isDirectory()){
recursiveEmptyDirDelete(files[i],ignore_set,log_warnings);
}else{
if ( ignore_set.contains( x.getName().toLowerCase())){
if ( !x.delete()){
if ( log_warnings ){
Debug.out("Empty folder delete: failed to delete file " + x );
}
}
}
}
}
if (f.getCanonicalPath().equals(moveToDir)) {
if ( log_warnings ){
Debug.out("Empty folder delete: not allowed to delete the MoveTo dir !");
}
return;
}
if (f.getCanonicalPath().equals(defSaveDir)) {
if ( log_warnings ){
Debug.out("Empty folder delete: not allowed to delete the default data dir !");
}
return;
}
File[] files_inside = f.listFiles();
if (files_inside.length == 0) {
if ( !f.delete()){
if ( log_warnings ){
Debug.out("Empty folder delete: failed to delete directory " + f );
}
}
}else{
if ( log_warnings ){
Debug.out("Empty folder delete: " + files_inside.length + " file(s)/folder(s) still in \"" + f + "\" - first listed item is \"" + files_inside[0].getName() + "\". Not removing.");
}
}
}
} catch (Exception e) { Debug.out(e.toString()); }
|
private static void | releaseReservedFileHandles()
try{
class_mon.enter();
File lock_file = new File(SystemProperties.getUserPath() + ".lock");
lock_file.createNewFile();
while( reserved_file_handles.size() < RESERVED_FILE_HANDLE_COUNT ){
// System.out.println( "getting reserved file handle");
InputStream is = new FileInputStream( lock_file );
reserved_file_handles.add(is);
}
}catch( Throwable e ){
Debug.printStackTrace( e );
}finally{
class_mon.exit();
}
|
public static boolean | renameFile(java.io.File from_file, java.io.File to_file)
return renameFile(from_file, to_file, true);
|
public static boolean | renameFile(java.io.File from_file, java.io.File to_file, boolean fail_on_existing_directory)
return renameFile(from_file, to_file, fail_on_existing_directory, null);
|
public static boolean | renameFile(java.io.File from_file, java.io.File to_file, boolean fail_on_existing_directory, java.io.FileFilter file_filter)
if ( !from_file.exists()){
Logger
.log(new LogAlert(LogAlert.REPEATABLE, LogAlert.AT_ERROR,
"renameFile: source file '" + from_file
+ "' doesn't exist, failing"));
return( false );
}
/**
* If the destination exists, we only fail if requested.
*/
if (to_file.exists() && (fail_on_existing_directory || from_file.isFile() || to_file.isFile())) {
Logger.log(new LogAlert(LogAlert.REPEATABLE, LogAlert.AT_ERROR,
"renameFile: target file '" + to_file + "' already exists, failing"));
return( false );
}
File to_file_parent = to_file.getParentFile();
if (!to_file_parent.exists()) {FileUtil.mkdirs(to_file_parent);}
if ( from_file.isDirectory()){
File[] files = null;
if (file_filter != null) {files = from_file.listFiles(file_filter);}
else {files = from_file.listFiles();}
if ( files == null ){
// empty dir
return( true );
}
int last_ok = 0;
if (!to_file.exists()) {to_file.mkdir();}
for (int i=0;i<files.length;i++){
File ff = files[i];
File tf = new File( to_file, ff.getName());
try{
if ( renameFile( ff, tf, fail_on_existing_directory, file_filter )){
last_ok++;
}else{
break;
}
}catch( Throwable e ){
Logger.log(new LogAlert(LogAlert.REPEATABLE,
"renameFile: failed to rename file '" + ff.toString() + "' to '"
+ tf.toString() + "'", e));
break;
}
}
if ( last_ok == files.length ){
File[] remaining = from_file.listFiles();
if ( remaining != null && remaining.length > 0 ){
// This might be important or not. We'll make it a debug message if we had a filter,
// or log it normally otherwise.
if (file_filter == null) {
Logger.log(new LogAlert(LogAlert.REPEATABLE, LogAlert.AT_ERROR,
"renameFile: files remain in '" + from_file.toString()
+ "', not deleting"));
}
else {
/* Should we log this? How should we log this? */
return true;
}
}else{
if ( !from_file.delete()){
Logger.log(new LogAlert(LogAlert.REPEATABLE, LogAlert.AT_ERROR,
"renameFile: failed to delete '" + from_file.toString() + "'"));
}
}
return( true );
}
// recover by moving files back
for (int i=0;i<last_ok;i++){
File ff = files[i];
File tf = new File( to_file, ff.getName());
try{
// null - We don't want to use the file filter, it only refers to source paths.
if ( !renameFile( tf, ff, false, null )){
Logger.log(new LogAlert(LogAlert.REPEATABLE, LogAlert.AT_ERROR,
"renameFile: recovery - failed to move file '" + tf.toString()
+ "' to '" + ff.toString() + "'"));
}
}catch( Throwable e ){
Logger.log(new LogAlert(LogAlert.REPEATABLE,
"renameFile: recovery - failed to move file '" + tf.toString()
+ "' to '" + ff.toString() + "'", e));
}
}
return( false );
}else{
if ( (!COConfigurationManager.getBooleanParameter("Copy And Delete Data Rather Than Move")) &&
from_file.renameTo( to_file )){
return( true );
}else{
boolean success = false;
// can't rename across file systems under Linux - try copy+delete
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream( from_file );
fos = new FileOutputStream( to_file );
byte[] buffer = new byte[65536];
while( true ){
int len = fis.read( buffer );
if ( len <= 0 ){
break;
}
fos.write( buffer, 0, len );
}
fos.close();
fos = null;
fis.close();
fis = null;
if ( !from_file.delete()){
Logger.log(new LogAlert(LogAlert.REPEATABLE,
LogAlert.AT_ERROR, "renameFile: failed to delete '"
+ from_file.toString() + "'"));
throw( new Exception( "Failed to delete '" + from_file.toString() + "'"));
}
success = true;
return( true );
}catch( Throwable e ){
Logger.log(new LogAlert(LogAlert.REPEATABLE,
"renameFile: failed to rename '" + from_file.toString()
+ "' to '" + to_file.toString() + "'", e));
return( false );
}finally{
if ( fis != null ){
try{
fis.close();
}catch( Throwable e ){
}
}
if ( fos != null ){
try{
fos.close();
}catch( Throwable e ){
}
}
// if we've failed then tidy up any partial copy that has been performed
if ( !success ){
if ( to_file.exists()){
to_file.delete();
}
}
}
}
}
|
public static void | runAsTask(com.aelitis.azureus.core.AzureusCoreOperationTask task)
AzureusCore core = AzureusCoreFactory.getSingleton();
core.createOperation( AzureusCoreOperation.OP_FILE_MOVE, task );
|
public static java.lang.String | translateMoveFilePath(java.lang.String old_root, java.lang.String new_root, java.lang.String file_to_move)
if (!file_to_move.startsWith(old_root)) {return null;}
String file_suffix = file_to_move.substring(old_root.length());
if (new_root.endsWith(File.separator)) {new_root = new_root.substring(0, new_root.length()-1);}
if (file_suffix.startsWith(File.separator)) {file_suffix = file_suffix.substring(1);}
return new_root + File.separator + file_suffix;
|
public static void | writeBytesAsFile(java.lang.String filename, byte[] file_data)
try{
File file = new File( filename );
FileOutputStream out = new FileOutputStream( file );
out.write( file_data );
out.close();
}
catch( Throwable t ) {
Debug.out( "writeBytesAsFile:: error: ", t );
}
|
public static void | writeResilientConfigFile(java.lang.String file_name, java.util.Map data)
File parent_dir = new File(SystemProperties.getUserPath());
boolean use_backups = COConfigurationManager.getBooleanParameter("Use Config File Backups" );
writeResilientFile( parent_dir, file_name, data, use_backups );
|
public static void | writeResilientFile(java.io.File file, java.util.Map data)
writeResilientFile( file.getParentFile(), file.getName(), data, false );
|
public static void | writeResilientFile(java.io.File parent_dir, java.lang.String file_name, java.util.Map data, boolean use_backup)
if ( use_backup ){
File originator = new File( parent_dir, file_name );
if ( originator.exists()){
backupFile( originator, true );
}
}
writeResilientFile( parent_dir, file_name, data );
|
private static void | writeResilientFile(java.io.File parent_dir, java.lang.String file_name, java.util.Map data)
try{
class_mon.enter();
try{
getReservedFileHandles();
File temp = new File( parent_dir, file_name + ".saving");
BufferedOutputStream baos = null;
try{
byte[] encoded_data = BEncoder.encode(data);
FileOutputStream tempOS = new FileOutputStream( temp, false );
baos = new BufferedOutputStream( tempOS, 8192 );
baos.write( encoded_data );
baos.flush();
tempOS.getFD().sync();
baos.close();
baos = null;
//only use newly saved file if it got this far, i.e. it saved successfully
if ( temp.length() > 1L ) {
File file = new File( parent_dir, file_name );
if ( file.exists() ){
file.delete();
}
temp.renameTo( file );
}
}catch (Exception e) {
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, "Save of '"
+ file_name + "' fails", e));
}finally{
try {
if (baos != null){
baos.close();
}
}catch( Exception e){
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, "Save of '"
+ file_name + "' fails", e));
}
}
}finally{
releaseReservedFileHandles();
}
}finally{
class_mon.exit();
}
|