Skip to content

Commit c8f8c3a

Browse files
committed
[IO-318] Add Charset sister APIs to method that take a String charset name. LockableFileWriterTest.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1307431 13f79535-47bb-0310-9956-ffa450edef68
1 parent 032df74 commit c8f8c3a

2 files changed

Lines changed: 45 additions & 5 deletions

File tree

src/main/java/org/apache/commons/io/output/LockableFileWriter.java

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@
2222
import java.io.IOException;
2323
import java.io.OutputStream;
2424
import java.io.OutputStreamWriter;
25+
import java.io.UnsupportedEncodingException;
2526
import java.io.Writer;
27+
import java.nio.charset.Charset;
28+
import java.nio.charset.UnsupportedCharsetException;
2629

30+
import org.apache.commons.io.Charsets;
2731
import org.apache.commons.io.FileUtils;
2832
import org.apache.commons.io.IOUtils;
2933

@@ -130,7 +134,7 @@ public LockableFileWriter(File file, boolean append) throws IOException {
130134
* @throws IOException in case of an I/O error
131135
*/
132136
public LockableFileWriter(File file, boolean append, String lockDir) throws IOException {
133-
this(file, null, append, lockDir);
137+
this(file, Charset.defaultCharset(), append, lockDir);
134138
}
135139

136140
/**
@@ -140,6 +144,22 @@ public LockableFileWriter(File file, boolean append, String lockDir) throws IOEx
140144
* @param encoding the encoding to use, null means platform default
141145
* @throws NullPointerException if the file is null
142146
* @throws IOException in case of an I/O error
147+
* @since 2.3
148+
*/
149+
public LockableFileWriter(File file, Charset encoding) throws IOException {
150+
this(file, encoding, false, null);
151+
}
152+
153+
/**
154+
* Constructs a LockableFileWriter with a file encoding.
155+
*
156+
* @param file the file to write to, not null
157+
* @param encoding the encoding to use, null means platform default
158+
* @throws NullPointerException if the file is null
159+
* @throws IOException in case of an I/O error
160+
* @throws UnsupportedCharsetException
161+
* thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
162+
* supported.
143163
*/
144164
public LockableFileWriter(File file, String encoding) throws IOException {
145165
this(file, encoding, false, null);
@@ -154,8 +174,9 @@ public LockableFileWriter(File file, String encoding) throws IOException {
154174
* @param lockDir the directory in which the lock file should be held
155175
* @throws NullPointerException if the file is null
156176
* @throws IOException in case of an I/O error
177+
* @since 2.3
157178
*/
158-
public LockableFileWriter(File file, String encoding, boolean append,
179+
public LockableFileWriter(File file, Charset encoding, boolean append,
159180
String lockDir) throws IOException {
160181
super();
161182
// init file to create/append
@@ -183,6 +204,24 @@ public LockableFileWriter(File file, String encoding, boolean append,
183204
out = initWriter(file, encoding, append);
184205
}
185206

207+
/**
208+
* Constructs a LockableFileWriter with a file encoding.
209+
*
210+
* @param file the file to write to, not null
211+
* @param encoding the encoding to use, null means platform default
212+
* @param append true if content should be appended, false to overwrite
213+
* @param lockDir the directory in which the lock file should be held
214+
* @throws NullPointerException if the file is null
215+
* @throws IOException in case of an I/O error
216+
* @throws UnsupportedCharsetException
217+
* thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
218+
* supported.
219+
*/
220+
public LockableFileWriter(File file, String encoding, boolean append,
221+
String lockDir) throws IOException {
222+
this(file, Charsets.toCharset(encoding), append, lockDir);
223+
}
224+
186225
//-----------------------------------------------------------------------
187226
/**
188227
* Tests that we can write to the lock directory.
@@ -227,7 +266,7 @@ private void createLock() throws IOException {
227266
* @return The initialised writer
228267
* @throws IOException if an error occurs
229268
*/
230-
private Writer initWriter(File file, String encoding, boolean append) throws IOException {
269+
private Writer initWriter(File file, Charset encoding, boolean append) throws IOException {
231270
boolean fileExistedAlready = file.exists();
232271
OutputStream stream = null;
233272
Writer writer = null;

src/test/java/org/apache/commons/io/output/LockableFileWriterTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.File;
2020
import java.io.IOException;
2121
import java.io.Writer;
22+
import java.nio.charset.UnsupportedCharsetException;
2223

2324
import org.apache.commons.io.IOUtils;
2425
import org.apache.commons.io.testtools.FileBasedTestCase;
@@ -159,12 +160,12 @@ public void testFileNotLocked() throws IOException {
159160
}
160161

161162
//-----------------------------------------------------------------------
162-
public void testConstructor_File_encoding_badEncoding() {
163+
public void testConstructor_File_encoding_badEncoding() throws IOException {
163164
Writer writer = null;
164165
try {
165166
writer = new LockableFileWriter(file, "BAD-ENCODE");
166167
fail();
167-
} catch (IOException ex) {
168+
} catch (UnsupportedCharsetException ex) {
168169
// expected
169170
assertFalse(file.exists());
170171
assertFalse(lockFile.exists());

0 commit comments

Comments
 (0)