diff --git a/src/fr/pandacube/java/logviewer/Main.java b/src/fr/pandacube/java/logviewer/Main.java index f5697d6..7790e77 100644 --- a/src/fr/pandacube/java/logviewer/Main.java +++ b/src/fr/pandacube/java/logviewer/Main.java @@ -60,15 +60,25 @@ public class Main { long previousLength = 0; while(true) { - if (reader.length() < previousLength) { - reader.seek(0); - } if ((line = readUTF8LineFromFile(reader)) != null) { outln(line); - previousLength = reader.length(); } else { - Thread.sleep(10); + previousLength = reader.length(); + reader.close(); + Thread.sleep(250); + + // tant que l'exeption est lancée, on réessaye + for(;;) { + try { + reader = new RandomAccessFile(f, "r"); + break; + } catch (FileNotFoundException e) { + Thread.sleep(250); + } + } + if (reader.length() >= previousLength) + reader.seek(previousLength); } } diff --git a/src/fr/pandacube/java/logviewer/test/Test.java b/src/fr/pandacube/java/logviewer/test/Test.java index 121c0bc..d61d3e3 100644 --- a/src/fr/pandacube/java/logviewer/test/Test.java +++ b/src/fr/pandacube/java/logviewer/test/Test.java @@ -3,6 +3,9 @@ package fr.pandacube.java.logviewer.test; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; +import java.nio.file.FileSystemException; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; import java.util.Random; import java.util.concurrent.atomic.AtomicBoolean; @@ -42,11 +45,21 @@ public class Test { wr.write(randomLine()); wr.flush(); - Thread.sleep(r.nextInt(10)); + Thread.sleep(r.nextInt(50)); - if (r.nextInt(10) == 0) { + if (r.nextInt(100) == 0) { wr.close(); - f.delete(); + + // tant que l'exeption est lancée, on réessaye + for(;;) { + try { + Files.move(f.toPath(), new File(r.nextInt(10)+".old").toPath(), StandardCopyOption.ATOMIC_MOVE); + break; + } catch (FileSystemException e) { + Thread.sleep(250); + } + } + f.createNewFile(); wr = new BufferedWriter(new FileWriter(f)); } @@ -82,7 +95,7 @@ public class Test { private static Random r = new Random(); - private static char[] chars = new char[] {' ', ' ', ' ', 'a', 'b', 'c', 'd', 'e', 'f', '♪', '♫', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; + private static char[] chars = new char[] {' ', ' ', ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; public static String randomLine() { int length = r.nextInt(100)+15;