Skip to content

Commit a41ddc5

Browse files
author
Gary Gregory
committed
A null Charset in FileWriterWithEncoding uses the default Charset.
1 parent 9ffac9e commit a41ddc5

3 files changed

Lines changed: 27 additions & 29 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ The <action> type attribute can be add,update,fix,remove.
5353
<action issue="IO-744" dev="ggregory" type="fix" due-to="RBRi, Gary Gregory">
5454
FileWriterWithEncoding for an existing file no longer truncates the file. #251.
5555
</action>
56+
<action dev="ggregory" type="fix" due-to="Gary Gregory">
57+
A null charset or charset name in FileWriterWithEncoding constructors uses the default Charset.
58+
</action>
5659
<!-- ADD -->
5760
<action dev="ggregory" type="add" due-to="Gary Gregory">
5861
Add BrokenReader.INSTANCE.

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

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.nio.file.StandardOpenOption;
2828
import java.util.Objects;
2929

30+
import org.apache.commons.io.Charsets;
3031
import org.apache.commons.io.FileUtils;
3132
import org.apache.commons.io.IOUtils;
3233

@@ -149,11 +150,11 @@ public FileWriterWithEncoding(final File file, final String charsetName) throws
149150
/**
150151
* Constructs a FileWriterWithEncoding with a file encoding.
151152
*
152-
* @param file the file to write to, not null
153-
* @param charsetName the name of the requested charset, not null
154-
* @param append true if content should be appended, false to overwrite
155-
* @throws NullPointerException if the file or encoding is null
156-
* @throws IOException in case of an I/O error
153+
* @param file the file to write to, not null.
154+
* @param charsetName the name of the requested charset, null uses the default Charset.
155+
* @param append true if content should be appended, false to overwrite.
156+
* @throws NullPointerException if the file is null.
157+
* @throws IOException in case of an I/O error.
157158
*/
158159
public FileWriterWithEncoding(final File file, final String charsetName, final boolean append) throws IOException {
159160
this.out = initWriter(file, charsetName, append);
@@ -174,11 +175,11 @@ public FileWriterWithEncoding(final File file, final Charset charset) throws IOE
174175
/**
175176
* Constructs a FileWriterWithEncoding with a file encoding.
176177
*
177-
* @param file the file to write to, not null
178-
* @param encoding the name of the requested charset, not null
179-
* @param append true if content should be appended, false to overwrite
180-
* @throws NullPointerException if the file or encoding is null
181-
* @throws IOException in case of an I/O error
178+
* @param file the file to write to, not null.
179+
* @param encoding the name of the requested charset, null uses the default Charset.
180+
* @param append true if content should be appended, false to overwrite.
181+
* @throws NullPointerException if the file is null.
182+
* @throws IOException in case of an I/O error.
182183
*/
183184
public FileWriterWithEncoding(final File file, final Charset encoding, final boolean append) throws IOException {
184185
this.out = initWriter(file, encoding, append);
@@ -199,37 +200,34 @@ public FileWriterWithEncoding(final File file, final CharsetEncoder charsetEncod
199200
/**
200201
* Constructs a FileWriterWithEncoding with a file encoding.
201202
*
202-
* @param file the file to write to, not null
203-
* @param charsetEncoder the encoding to use, not null
204-
* @param append true if content should be appended, false to overwrite
205-
* @throws NullPointerException if the file or encoding is null
206-
* @throws IOException in case of an I/O error
203+
* @param file the file to write to, not null.
204+
* @param charsetEncoder the encoding to use, null uses the default Charset.
205+
* @param append true if content should be appended, false to overwrite.
206+
* @throws NullPointerException if the file is null.
207+
* @throws IOException in case of an I/O error.
207208
*/
208209
public FileWriterWithEncoding(final File file, final CharsetEncoder charsetEncoder, final boolean append)
209210
throws IOException {
210211
this.out = initWriter(file, charsetEncoder, append);
211212
}
212213

213214
/**
214-
* Initialize the wrapped file writer.
215-
* Ensure that a cleanup occurs if the writer creation fails.
215+
* Initializes the wrapped file writer. Ensure that a cleanup occurs if the writer creation fails.
216216
*
217-
* @param file the file to be accessed
218-
* @param encoding the encoding to use - may be Charset, CharsetEncoder or String
219-
* @param append true to append
217+
* @param file the file to be accessed
218+
* @param encoding the encoding to use - may be Charset, CharsetEncoder or String, null uses the default Charset.
219+
* @param append true to append
220220
* @return the initialized writer
221-
* @throws NullPointerException if the file or encoding is null
222221
* @throws IOException if an error occurs
223222
*/
224223
private static Writer initWriter(final File file, final Object encoding, final boolean append) throws IOException {
225224
Objects.requireNonNull(file, "file");
226-
Objects.requireNonNull(encoding, "encoding");
227225
OutputStream stream = null;
228226
final boolean fileExistedAlready = file.exists();
229227
try {
230228
stream = Files.newOutputStream(file.toPath(), append ? StandardOpenOption.APPEND : StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
231-
if (encoding instanceof Charset) {
232-
return new OutputStreamWriter(stream, (Charset) encoding);
229+
if (encoding == null || encoding instanceof Charset) {
230+
return new OutputStreamWriter(stream, Charsets.toCharset((Charset) encoding));
233231
}
234232
if (encoding instanceof CharsetEncoder) {
235233
return new OutputStreamWriter(stream, (CharsetEncoder) encoding);

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,8 @@ public void sameEncoding_CharsetEncoder_constructor() throws Exception {
121121

122122
@Test
123123
public void sameEncoding_null_Charset_constructor() throws Exception {
124-
try {
125-
successfulRun(new FileWriterWithEncoding(file2, (Charset) null));
126-
fail();
127-
} catch (final NullPointerException ignore) {
128-
// empty
124+
try (final FileWriterWithEncoding writer = new FileWriterWithEncoding(file2, (Charset) null)) {
125+
successfulRun(writer);
129126
}
130127
}
131128

0 commit comments

Comments
 (0)