commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject svn commit: r292340 - in /jakarta/commons/proper/io/trunk: RELEASE-NOTES.txt project.xml src/java/org/apache/commons/io/output/LockableFileWriter.java src/test/org/apache/commons/io/output/LockableFileWriterTest.java
Date Wed, 28 Sep 2005 23:02:27 GMT
Author: scolebourne
Date: Wed Sep 28 16:02:19 2005
New Revision: 292340

URL: http://svn.apache.org/viewcvs?rev=292340&view=rev
Log:
LockableFileWriter, add encoding support, and general tidy
bug 36825, from Andy Lehane

Modified:
    jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt
    jakarta/commons/proper/io/trunk/project.xml
    jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/output/LockableFileWriter.java
    jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/output/LockableFileWriterTest.java

Modified: jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt?rev=292340&r1=292339&r2=292340&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt (original)
+++ jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt Wed Sep 28 16:02:19 2005
@@ -171,6 +171,9 @@
 - DeferredFileOutputStream  [34142]
     Performance optimizations avoiding double buffering
 
+- LockableFileWriter - encoding support [36825]
+    Add support for character encodings to LockableFileWriter
+
 - IOUtils and EndianUtils are no longer final  [28978]
     Allows developers to have subclasses if desired
 

Modified: jakarta/commons/proper/io/trunk/project.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/project.xml?rev=292340&r1=292339&r2=292340&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/project.xml (original)
+++ jakarta/commons/proper/io/trunk/project.xml Wed Sep 28 16:02:19 2005
@@ -175,6 +175,9 @@
       <name>Chris Eldredge</name>
     </contributor>
     <contributor>
+      <name>Andy Lehane</name>
+    </contributor>
+    <contributor>
       <name>Marcelo Liberato</name>
     </contributor>
     <contributor>

Modified: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/output/LockableFileWriter.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/output/LockableFileWriter.java?rev=292340&r1=292339&r2=292340&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/output/LockableFileWriter.java
(original)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/output/LockableFileWriter.java
Wed Sep 28 16:02:19 2005
@@ -16,41 +16,50 @@
 package org.apache.commons.io.output;
 
 import java.io.File;
+import java.io.FileOutputStream;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.io.OutputStreamWriter;
 import java.io.Writer;
 
 /**
  * FileWriter that will create and honor lock files to allow simple
- * cross thread file lock handling.  If <code>Writer</code> attributes
- * are unspecified, the default behavior is to overwrite (rather than
- * to append), and to use the value of the system property
- * <code>java.io.tmpdir</code> for the lock file directory.
+ * cross thread file lock handling.
+ * <p>
+ * This class provides a simple alternative to <code>FileWriter</code>
+ * that will use a lock file to prevent duplicate writes.
+ * <p>
+ * By default, the file will be overwritten, but this may be changed to append.
+ * The lock directory may be specified, but defaults to the system property
+ * <code>java.io.tmpdir</code>.
+ * The encoding may also be specified, and defaults to the platform default.
  *
  * @author <a href="mailto:sanders@apache.org">Scott Sanders</a>
  * @author <a href="mailto:ms@collab.net">Michael Salmon</a>
  * @author <a href="mailto:jon@collab.net">Jon S. Stevens</a>
  * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
  * @author Stephen Colebourne
+ * @author Andy Lehane
  * @version $Id$
  */
 public class LockableFileWriter extends Writer {
+    // Cannot extend ProxyWriter, as requires writer to be
+    // known when super() is called
 
     /** The extension for the lock file. */
     private static final String LCK = ".lck";
 
+    /** The writer to decorate. */
+    private Writer out;
     /** The lock file. */
     private File lockFile;
-    /** The write used to write to the file. */
-    private Writer writer;
-    /** Should we append to the file or not. */
-    private boolean append;
 
     /**
      * Constructs a LockableFileWriter.
      * If the file exists, it is overwritten.
      *
-     * @param fileName  the file to write to
+     * @param fileName  the file to write to, not null
+     * @throws NullPointerException if the file is null
      * @throws IOException in case of an I/O error
      */
     public LockableFileWriter(String fileName) throws IOException {
@@ -60,8 +69,9 @@
     /**
      * Constructs a LockableFileWriter.
      *
-     * @param fileName  file to write to
+     * @param fileName  file to write to, not null
      * @param append  true if content should be appended, false to overwrite
+     * @throws NullPointerException if the file is null
      * @throws IOException in case of an I/O error
      */
     public LockableFileWriter(String fileName, boolean append) throws IOException {
@@ -71,9 +81,10 @@
     /**
      * Constructs a LockableFileWriter.
      *
-     * @param fileName  the file to write to
+     * @param fileName  the file to write to, not null
      * @param append  true if content should be appended, false to overwrite
      * @param lockDir  the directory in which the lock file should be held
+     * @throws NullPointerException if the file is null
      * @throws IOException in case of an I/O error
      */
     public LockableFileWriter(String fileName, boolean append, String lockDir) throws IOException
{
@@ -84,7 +95,8 @@
      * Constructs a LockableFileWriter.
      * If the file exists, it is overwritten.
      *
-     * @param file  the file to write to
+     * @param file  the file to write to, not null
+     * @throws NullPointerException if the file is null
      * @throws IOException in case of an I/O error
      */
     public LockableFileWriter(File file) throws IOException {
@@ -94,8 +106,9 @@
     /**
      * Constructs a LockableFileWriter.
      *
-     * @param file  the file to write to
+     * @param file  the file to write to, not null
      * @param append  true if content should be appended, false to overwrite
+     * @throws NullPointerException if the file is null
      * @throws IOException in case of an I/O error
      */
     public LockableFileWriter(File file, boolean append) throws IOException {
@@ -105,22 +118,68 @@
     /**
      * Constructs a LockableFileWriter.
      *
-     * @param file  the file to write to
+     * @param file  the file to write to, not null
      * @param append  true if content should be appended, false to overwrite
      * @param lockDir  the directory in which the lock file should be held
+     * @throws NullPointerException if the file is null
      * @throws IOException in case of an I/O error
      */
     public LockableFileWriter(File file, boolean append, String lockDir) throws IOException
{
-        this.append = append;
+        this(file, null, append, lockDir);
+    }
+
+    /**
+     * Constructs a LockableFileWriter with a file encoding.
+     *
+     * @param file  the file to write to, not null
+     * @param encoding  the encoding to use, null means platform default
+     * @throws NullPointerException if the file is null
+     * @throws IOException in case of an I/O error
+     */
+    public LockableFileWriter(File file, String encoding) throws IOException {
+        this(file, encoding, false, null);
+    }
 
+    /**
+     * Constructs a LockableFileWriter with a file encoding.
+     *
+     * @param file  the file to write to, not null
+     * @param encoding  the encoding to use, null means platform default
+     * @param append  true if content should be appended, false to overwrite
+     * @param lockDir  the directory in which the lock file should be held
+     * @throws NullPointerException if the file is null
+     * @throws IOException in case of an I/O error
+     */
+    public LockableFileWriter(File file, String encoding, boolean append,
+            String lockDir) throws IOException {
+        super();
+        file = file.getAbsoluteFile();
+        if (file.exists()) {
+            if (file.isDirectory()) {
+                throw new IOException("File specified is a directory");
+            }
+        } else if (file.getParentFile() != null) {
+            file.getParentFile().mkdirs();
+        }
         if (lockDir == null) {
             lockDir = System.getProperty("java.io.tmpdir");
         }
         testLockDir(new File(lockDir));
         this.lockFile = new File(lockDir, file.getName() + LCK);
-        createLock();
+        try {
+            createLock();
+            if (encoding == null) {
+                out = new FileWriter(file.getAbsolutePath(), append);
+            } else {
+                FileOutputStream fos = new FileOutputStream(file.getAbsolutePath(), append);
+                out = new OutputStreamWriter(fos, encoding);
+            }
+        } catch (IOException ioe) {
+            this.lockFile.delete();
+            throw ioe;
+        }
 
-        this.writer = new FileWriter(file.getAbsolutePath(), this.append);
+        this.out = new FileWriter(file.getAbsolutePath(), append);
     }
 
     //-----------------------------------------------------------------------
@@ -164,31 +223,41 @@
      */
     public void close() throws IOException {
         try {
-            writer.close();
+            out.close();
         } finally {
             lockFile.delete();
         }
     }
 
     //-----------------------------------------------------------------------
-    /**
-     * Write a portion of a string.
-     *
-     * @param  cbuf  The characters to write
-     * @param  off  Offset from which to start writing characters
-     * @param  len  Number of characters to write
-     *
-     * @exception  IOException  If an I/O error occurs
-     */
-    public void write(char[] cbuf, int off, int len) throws IOException {
-        writer.write(cbuf, off, len);
+    /** @see java.io.Writer#write(int) */
+    public void write(int idx) throws IOException {
+        out.write(idx);
     }
 
-    /**
-     * Flushes the file writer.
-     */
+    /** @see java.io.Writer#write(char[]) */
+    public void write(char[] chr) throws IOException {
+        out.write(chr);
+    }
+
+    /** @see java.io.Writer#write(char[], int, int) */
+    public void write(char[] chr, int st, int end) throws IOException {
+        out.write(chr, st, end);
+    }
+
+    /** @see java.io.Writer#write(String) */
+    public void write(String str) throws IOException {
+        out.write(str);
+    }
+
+    /** @see java.io.Writer#write(String, int, int) */
+    public void write(String str, int st, int end) throws IOException {
+        out.write(str, st, end);
+    }
+
+    /** @see java.io.Writer#flush() */
     public void flush() throws IOException {
-        writer.flush();
+        out.flush();
     }
 
 }

Modified: jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/output/LockableFileWriterTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/output/LockableFileWriterTest.java?rev=292340&r1=292339&r2=292340&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/output/LockableFileWriterTest.java
(original)
+++ jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/output/LockableFileWriterTest.java
Wed Sep 28 16:02:19 2005
@@ -15,8 +15,8 @@
  */
 package org.apache.commons.io.output;
 
-import java.io.IOException;
 import java.io.File;
+import java.io.IOException;
 
 import junit.framework.TestCase;
 
@@ -31,19 +31,25 @@
 public class LockableFileWriterTest extends TestCase {
 
     private File file;
+    private File lockDir;
+    private File lockFile;
 
     public LockableFileWriterTest(String name) {
         super(name);
     }
 
     public void setUp() {
-        this.file = new File("testlockfile");
+        file = new File("testlockfile");
+        lockDir = new File(System.getProperty("java.io.tmpdir"));
+        lockFile = new File(lockDir, file.getName() + ".lck");
     }
 
     public void tearDown() {
-        this.file.delete();
+        file.delete();
+        lockFile.delete();
     }
 
+    //-----------------------------------------------------------------------
     public void testFileLocked() throws IOException {
         LockableFileWriter lfw = new LockableFileWriter(this.file);
         try {
@@ -56,6 +62,7 @@
         } finally {
             lfw.close();
         }
+        assertEquals(false, lockFile.exists());
     }
 
     public void testFileNotLocked() throws IOException {
@@ -70,6 +77,31 @@
                 fail("Somehow unable to open a unlocked file. ");
             }
         }
+        assertEquals(false, lockFile.exists());
+    }
+
+    public void testConstructor_File_encoding_badEncoding() throws IOException {
+        try {
+            new LockableFileWriter(file, "BAD-ENCODE");
+            fail();
+        } catch (IOException ex) {}
+        assertEquals(false, lockFile.exists());
+    }
+
+    public void testConstructor_File_nullFile() throws IOException {
+        try {
+            new LockableFileWriter((File) null);
+            fail();
+        } catch (NullPointerException ex) {}
+        assertEquals(false, lockFile.exists());
+    }
+
+    public void testConstructor_fileName_nullFile() throws IOException {
+        try {
+            new LockableFileWriter((String) null);
+            fail();
+        } catch (NullPointerException ex) {}
+        assertEquals(false, lockFile.exists());
     }
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message