You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2012/03/30 16:10:33 UTC
svn commit: r1307431 - in /commons/proper/io/trunk/src:
main/java/org/apache/commons/io/output/LockableFileWriter.java
test/java/org/apache/commons/io/output/LockableFileWriterTest.java
Author: ggregory
Date: Fri Mar 30 14:10:33 2012
New Revision: 1307431
URL: http://svn.apache.org/viewvc?rev=1307431&view=rev
Log:
[IO-318] Add Charset sister APIs to method that take a String charset name. LockableFileWriterTest.
Modified:
commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/LockableFileWriter.java
commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/LockableFileWriterTest.java
Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/LockableFileWriter.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/LockableFileWriter.java?rev=1307431&r1=1307430&r2=1307431&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/LockableFileWriter.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/LockableFileWriter.java Fri Mar 30 14:10:33 2012
@@ -22,8 +22,12 @@ import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
+import java.io.UnsupportedEncodingException;
import java.io.Writer;
+import java.nio.charset.Charset;
+import java.nio.charset.UnsupportedCharsetException;
+import org.apache.commons.io.Charsets;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
@@ -130,7 +134,7 @@ public class LockableFileWriter extends
* @throws IOException in case of an I/O error
*/
public LockableFileWriter(File file, boolean append, String lockDir) throws IOException {
- this(file, null, append, lockDir);
+ this(file, Charset.defaultCharset(), append, lockDir);
}
/**
@@ -140,6 +144,22 @@ public class LockableFileWriter extends
* @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
+ * @since 2.3
+ */
+ public LockableFileWriter(File file, Charset 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
+ * @throws NullPointerException if the file is null
+ * @throws IOException in case of an I/O error
+ * @throws UnsupportedCharsetException
+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
+ * supported.
*/
public LockableFileWriter(File file, String encoding) throws IOException {
this(file, encoding, false, null);
@@ -154,8 +174,9 @@ public class LockableFileWriter extends
* @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
+ * @since 2.3
*/
- public LockableFileWriter(File file, String encoding, boolean append,
+ public LockableFileWriter(File file, Charset encoding, boolean append,
String lockDir) throws IOException {
super();
// init file to create/append
@@ -183,6 +204,24 @@ public class LockableFileWriter extends
out = initWriter(file, encoding, append);
}
+ /**
+ * 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
+ * @throws UnsupportedCharsetException
+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
+ * supported.
+ */
+ public LockableFileWriter(File file, String encoding, boolean append,
+ String lockDir) throws IOException {
+ this(file, Charsets.toCharset(encoding), append, lockDir);
+ }
+
//-----------------------------------------------------------------------
/**
* Tests that we can write to the lock directory.
@@ -227,7 +266,7 @@ public class LockableFileWriter extends
* @return The initialised writer
* @throws IOException if an error occurs
*/
- private Writer initWriter(File file, String encoding, boolean append) throws IOException {
+ private Writer initWriter(File file, Charset encoding, boolean append) throws IOException {
boolean fileExistedAlready = file.exists();
OutputStream stream = null;
Writer writer = null;
Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/LockableFileWriterTest.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/LockableFileWriterTest.java?rev=1307431&r1=1307430&r2=1307431&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/LockableFileWriterTest.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/LockableFileWriterTest.java Fri Mar 30 14:10:33 2012
@@ -19,6 +19,7 @@ package org.apache.commons.io.output;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
+import java.nio.charset.UnsupportedCharsetException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.testtools.FileBasedTestCase;
@@ -159,12 +160,12 @@ public class LockableFileWriterTest exte
}
//-----------------------------------------------------------------------
- public void testConstructor_File_encoding_badEncoding() {
+ public void testConstructor_File_encoding_badEncoding() throws IOException {
Writer writer = null;
try {
writer = new LockableFileWriter(file, "BAD-ENCODE");
fail();
- } catch (IOException ex) {
+ } catch (UnsupportedCharsetException ex) {
// expected
assertFalse(file.exists());
assertFalse(lockFile.exists());