commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1483340 - in /commons/proper/io/trunk/src: main/java/org/apache/commons/io/input/Tailer.java test/java/org/apache/commons/io/input/TailerTest.java
Date Thu, 16 May 2013 13:09:57 GMT
Author: sebb
Date: Thu May 16 13:09:56 2013
New Revision: 1483340

URL: http://svn.apache.org/r1483340
Log:
IO-354 Commons IO Tailer does not respect UTF-8 Charset
Rework to allow charset to be specified

Modified:
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/Tailer.java
    commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/TailerTest.java

Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/Tailer.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/Tailer.java?rev=1483340&r1=1483339&r2=1483340&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/Tailer.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/Tailer.java Thu May
16 13:09:56 2013
@@ -105,6 +105,7 @@ import org.apache.commons.io.IOUtils;
  * </pre>
  * <p>If you interrupt a tailer, the tailer listener is called with the {@link InterruptedException}.</p>
  *
+ * <p>The file is read using the default charset; this can be overriden if necessary</p>
  * @see TailerListener
  * @see TailerListenerAdapter
  * @version $Id$
@@ -119,6 +120,9 @@ public class Tailer implements Runnable 
 
     private static final int DEFAULT_BUFSIZE = 4096;
 
+    // The default charset used for reading files
+    private static final Charset DEFAULT_CHARSET = Charset.defaultCharset();
+
     /**
      * Buffer on top of RandomAccessFile.
      */
@@ -130,6 +134,11 @@ public class Tailer implements Runnable 
     private final File file;
 
     /**
+     * The character set that will be used to read the file.
+     */
+    private final Charset cset;
+
+    /**
      * The amount of time to wait for the file to be updated.
      */
     private final long delayMillis;
@@ -218,6 +227,23 @@ public class Tailer implements Runnable 
      * @param bufSize Buffer size
      */
     public Tailer(final File file, final TailerListener listener, final long delayMillis,
final boolean end, final boolean reOpen, final int bufSize) {
+        this(file, DEFAULT_CHARSET, listener, delayMillis, end, reOpen, bufSize);
+    }
+
+    /**
+     * Creates a Tailer for the given file, with a specified buffer size.
+     * @param file the file to follow.
+     * @param cset the Charset to be used for reading the file
+     * @param listener the TailerListener to use.
+     * @param delayMillis the delay between checks of the file for new content in milliseconds.
+     * @param end Set to true to tail from the end of the file, false to tail from the beginning
of the file.
+     * @param reOpen if true, close and reopen the file between reading chunks
+     * @param bufSize Buffer size
+     * @deprecated
+     */
+    @Deprecated
+    public Tailer(final File file, final Charset cset, final TailerListener listener, final
long delayMillis, final boolean end, final boolean reOpen
+            , final int bufSize) {
         this.file = file;
         this.delayMillis = delayMillis;
         this.end = end;
@@ -228,6 +254,7 @@ public class Tailer implements Runnable 
         this.listener = listener;
         listener.init(this);
         this.reOpen = reOpen;
+        this.cset = cset; 
     }
 
     /**
@@ -257,7 +284,24 @@ public class Tailer implements Runnable 
      */
     public static Tailer create(final File file, final TailerListener listener, final long
delayMillis, final boolean end, final boolean reOpen,
             final int bufSize) {
-        final Tailer tailer = new Tailer(file, listener, delayMillis, end, reOpen, bufSize);
+        return create(file, DEFAULT_CHARSET, listener, delayMillis, end, reOpen, bufSize);
+    }
+
+    /**
+     * Creates and starts a Tailer for the given file.
+     *
+     * @param file the file to follow.
+     * @param charset the character set to use for reading the file
+     * @param listener the TailerListener to use.
+     * @param delayMillis the delay between checks of the file for new content in milliseconds.
+     * @param end Set to true to tail from the end of the file, false to tail from the beginning
of the file.
+     * @param reOpen whether to close/reopen the file between chunks
+     * @param bufSize buffer size.
+     * @return The new tailer
+     */
+    public static Tailer create(final File file, final Charset charset, final TailerListener
listener, final long delayMillis, final boolean end, final boolean reOpen
+            ,final int bufSize) {
+        final Tailer tailer = new Tailer(file, charset, listener, delayMillis, end, reOpen,
bufSize);
         final Thread thread = new Thread(tailer);
         thread.setDaemon(true);
         thread.start();
@@ -453,8 +497,6 @@ public class Tailer implements Runnable 
      * @throws java.io.IOException if an I/O error occurs.
      */
     private long readLines(final RandomAccessFile reader) throws IOException {
-        // Make explicit that the default charset is being used here
-        Charset cset = Charset.defaultCharset();
         ByteArrayOutputStream lineBuf = new ByteArrayOutputStream(64);
         long pos = reader.getFilePointer();
         long rePos = pos; // position to re-read

Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/TailerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/TailerTest.java?rev=1483340&r1=1483339&r2=1483340&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/TailerTest.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/TailerTest.java Thu
May 16 13:09:56 2013
@@ -120,12 +120,12 @@ public class TailerTest extends FileBase
         final TestTailerListener listener = new TestTailerListener();
         final String osname = System.getProperty("os.name");
         final boolean isWindows = osname.startsWith("Windows");
-        tailer = new Tailer(file, listener, delay, false, isWindows);
+        // Need to use UTF-8 to read & write the file otherwise it can be corrupted (depending
on the default charset)
+        final Charset charsetUTF8 = Charset.forName("UTF-8");
+        tailer = new Tailer(file, charsetUTF8, listener, delay, false, isWindows, 4096);
         final Thread thread = new Thread(tailer);
         thread.start();
 
-        // Need to use UTF-8 to read & write the file otherwise it can be corrupted (depending
on the default charset)
-        final Charset charsetUTF8 = Charset.forName("UTF-8");
         Writer out = new OutputStreamWriter(new FileOutputStream(file), charsetUTF8);
         BufferedReader reader = null;
         try{



Mime
View raw message