changeset 1473:6c6ce14db6a8

add goodjava.io
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 17 Apr 2020 13:56:57 -0600
parents 60f6741f000a
children 13cbce740e1e
files src/goodjava/io/IoUtils.java src/goodjava/logger/RollingFileAppender.java src/goodjava/lucene/logging/LoggingIndexWriter.java src/luan/modules/IoLuan.java
diffstat 4 files changed, 36 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/io/IoUtils.java	Fri Apr 17 13:56:57 2020 -0600
@@ -0,0 +1,18 @@
+package goodjava.io;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+
+
+public final class IoUtils {
+	private IoUtils() {}  // never
+
+	public static void move( File from, File to ) throws IOException {
+		Files.move( from.toPath(), to.toPath() );
+	}
+
+	public static void delete(File file) throws IOException {
+		Files.deleteIfExists( file.toPath() );
+	}
+}
\ No newline at end of file
--- a/src/goodjava/logger/RollingFileAppender.java	Fri Apr 17 11:16:38 2020 -0600
+++ b/src/goodjava/logger/RollingFileAppender.java	Fri Apr 17 13:56:57 2020 -0600
@@ -3,6 +3,7 @@
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
+import goodjava.io.IoUtils;
 
 
 public class RollingFileAppender extends WriterAppender {
@@ -32,25 +33,17 @@
 	protected void rollOver() {
 		close();
 		File backup = new File(fileName+'.'+backups);
-		if( backup.exists() ) {
-			if( !backup.delete() )
-				debug("couldn't delete "+backup);
-		}
-		for( int i=backups-1; i>=1; i-- ) {
-			File f = backup;
-			backup = new File(fileName+'.'+i);
-			backup.renameTo(f);
-		}
-		if( !file.renameTo(backup) )
-			debug("couldn't rename "+file+" to "+backup);
 		try {
+			IoUtils.delete(backup);
+			for( int i=backups-1; i>=1; i-- ) {
+				File f = backup;
+				backup = new File(fileName+'.'+i);
+				IoUtils.move(backup,f);
+			}
+			IoUtils.move(file,backup);
 			open();
 		} catch(IOException e) {
 			throw new RuntimeException(e);
 		}
 	}
-
-	protected void debug(String msg) {
-		System.err.println("RollingFileAppender: "+msg);
-	}
 }
--- a/src/goodjava/lucene/logging/LoggingIndexWriter.java	Fri Apr 17 11:16:38 2020 -0600
+++ b/src/goodjava/lucene/logging/LoggingIndexWriter.java	Fri Apr 17 13:56:57 2020 -0600
@@ -22,6 +22,7 @@
 import org.apache.lucene.search.TopDocs;
 import org.apache.lucene.store.Directory;
 import org.apache.lucene.store.FSDirectory;
+import goodjava.io.IoUtils;
 import goodjava.lucene.api.GoodIndexWriter;
 import goodjava.lucene.api.LuceneIndexWriter;
 import goodjava.lucene.api.GoodCollector;
@@ -114,7 +115,7 @@
 		return new LogFile(file,"rwd");
 	}
 
-	private void deleteUnusedFiles() {
+	private void deleteUnusedFiles() throws IOException {
 		Set<String> used = new HashSet<String>();
 		used.add( index.getName() );
 		for( LogFile lf : logs ) {
@@ -127,14 +128,13 @@
 		}
 	}
 
-	private static void deleteFile(File file) {
+	private static void deleteFile(File file) throws IOException {
 		if( file.isDirectory() ) {
 			for( File f : file.listFiles() ) {
 				deleteFile(f);
 			}
 		}
-		if( !file.delete() )
-			throw new RuntimeException(file.getName());
+		IoUtils.delete(file);
 	}
 
 	private void writeIndex() throws IOException {
--- a/src/luan/modules/IoLuan.java	Fri Apr 17 11:16:38 2020 -0600
+++ b/src/luan/modules/IoLuan.java	Fri Apr 17 13:56:57 2020 -0600
@@ -32,6 +32,7 @@
 import javax.naming.NameNotFoundException;
 import javax.naming.directory.Attribute;
 import javax.naming.directory.InitialDirContext;
+import goodjava.io.IoUtils;
 import luan.Luan;
 import luan.LuanTable;
 import luan.LuanFunction;
@@ -456,12 +457,11 @@
 			return file.exists();
 		}
 
-		public void rename_to(Luan luan,Object destObj) throws LuanException {
+		public void rename_to(Luan luan,Object destObj) throws LuanException, IOException {
 			File dest = objToFile(luan,destObj);
 			if( dest==null )
 				throw new LuanException( "bad argument #1 to 'rename_to' (string or file table expected)" );
-			if( !file.renameTo(dest) )
-				throw new LuanException("couldn't rename file "+file+" to "+dest);
+			IoUtils.move(file,dest);
 		}
 
 		public void link_to(Luan luan,Object destObj) throws LuanException, IOException {
@@ -487,20 +487,19 @@
 			return new LuanFile(luan,tmp);
 		}
 
-		public void delete() throws LuanException {
+		public void delete() throws IOException {
 			if( file.exists() )
 				delete(file);
 		}
 
-		private static void delete(File file) throws LuanException {
+		private static void delete(File file) throws IOException {
 			File[] children = file.listFiles();
 			if( children != null && !isSymbolicLink(file) ) {
 				for( File child : children ) {
 					delete(child);
 				}
 			}
-			if( !file.delete() )
-				throw new LuanException("couldn't delete file "+file);
+			IoUtils.delete(file);
 		}
 
 		public void mkdir() throws LuanException {