Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public class FileWriterWithEncoding extends ProxyWriter {
* FileWriterWithEncoding w = FileWriterWithEncoding.builder()
* .setPath(path)
* .setAppend(false)
* .setCharsetEncoder(StandardCharsets.UTF_8)
* .setCharset(StandardCharsets.UTF_8)
* .get();}
* </pre>
*
Expand Down Expand Up @@ -125,9 +125,6 @@ public FileWriterWithEncoding get() throws IOException {
}

private Object getEncoder() {
if (charsetEncoder != null && getCharset() != null && !charsetEncoder.charset().equals(getCharset())) {
throw new IllegalStateException(String.format("Mismatched Charset(%s) and CharsetEncoder(%s)", getCharset(), charsetEncoder.charset()));
}
return charsetEncoder != null ? charsetEncoder : getCharset();
}

Expand All @@ -142,6 +139,13 @@ public Builder setAppend(final boolean append) {
return this;
}

@Override
public Builder setCharset(final Charset charset) {
super.setCharset(charset);
charsetEncoder = getCharset().newEncoder();
return this;
}

/**
* Sets charsetEncoder to use for encoding.
*
Expand All @@ -150,6 +154,9 @@ public Builder setAppend(final boolean append) {
*/
public Builder setCharsetEncoder(final CharsetEncoder charsetEncoder) {
this.charsetEncoder = charsetEncoder;
if (charsetEncoder != null) {
super.setCharset(charsetEncoder.charset());
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.commons.io.output;

import static org.apache.commons.io.test.TestUtils.checkFile;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand All @@ -32,6 +33,7 @@
import java.nio.charset.CharsetEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -42,6 +44,8 @@
*/
class FileWriterWithEncodingTest {

private static final String NON_ASCII = "caf\u00e9";

@TempDir
public File temporaryFolder;

Expand Down Expand Up @@ -204,6 +208,26 @@ void testDifferentEncoding() throws Exception {
}
}

@Test
void testBuilder_nonDefaultCharset() throws Exception {
for (final Charset charset : Arrays.asList(StandardCharsets.UTF_16, StandardCharsets.ISO_8859_1)) {
try (Writer writer = FileWriterWithEncoding.builder().setFile(file2).setCharset(charset).get()) {
writer.write(NON_ASCII);
}
assertArrayEquals(NON_ASCII.getBytes(charset), Files.readAllBytes(file2.toPath()));
}
}

@Test
void testBuilder_nonDefaultCharsetEncoder() throws Exception {
for (final Charset charset : Arrays.asList(StandardCharsets.UTF_16, StandardCharsets.ISO_8859_1)) {
try (Writer writer = FileWriterWithEncoding.builder().setFile(file2).setCharsetEncoder(charset.newEncoder()).get()) {
writer.write(NON_ASCII);
}
assertArrayEquals(NON_ASCII.getBytes(charset), Files.readAllBytes(file2.toPath()));
}
}

@Test
void testSameEncoding_Charset_constructor() throws Exception {
try (FileWriterWithEncoding writer = new FileWriterWithEncoding(file2, Charset.defaultCharset())) {
Expand Down