Skip to content

Commit e3eb344

Browse files
committed
FlatFileItemWriter now uses charset to determine default encoding
resolves spring-projects#1154 It now matches the encoding scheme of FlatFileItemReader
1 parent b486e24 commit e3eb344

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemWriter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2018 the original author or authors.
2+
* Copyright 2006-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.batch.item.file;
1818

19+
import java.nio.charset.Charset;
1920
import java.util.List;
2021

2122
import org.springframework.batch.item.file.transform.LineAggregator;
@@ -39,11 +40,15 @@
3940
* @author Dave Syer
4041
* @author Michael Minella
4142
* @author Mahmoud Ben Hassine
43+
* @author Glenn Renfro
4244
*/
4345
public class FlatFileItemWriter<T> extends AbstractFileItemWriter<T> {
4446

4547
protected LineAggregator<T> lineAggregator;
4648

49+
// default encoding for writing to flat files - set to charset of this Java virtual machine.
50+
public static final String DEFAULT_CHARSET = Charset.defaultCharset().name();
51+
4752
public FlatFileItemWriter() {
4853
this.setExecutionContextName(ClassUtils.getShortName(FlatFileItemWriter.class));
4954
}

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/builder/FlatFileItemWriterBuilderTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.io.File;
2020
import java.io.IOException;
2121
import java.io.InputStreamReader;
22+
import java.nio.charset.Charset;
2223
import java.util.Arrays;
2324

2425
import org.junit.Test;
@@ -38,6 +39,7 @@
3839
* @author Michael Minella
3940
* @author Mahmoud Ben Hassine
4041
* @author Drummond Dawson
42+
* @author Glenn Renfro
4143
*/
4244
public class FlatFileItemWriterBuilderTests {
4345

@@ -275,13 +277,13 @@ public void testFlags() throws Exception {
275277
.transactional(false)
276278
.lineAggregator(new PassThroughLineAggregator<>())
277279
.build();
278-
279280
assertFalse((Boolean) ReflectionTestUtils.getField(writer, "saveState"));
280281
assertTrue((Boolean) ReflectionTestUtils.getField(writer, "append"));
281282
assertFalse((Boolean) ReflectionTestUtils.getField(writer, "transactional"));
282283
assertTrue((Boolean) ReflectionTestUtils.getField(writer, "shouldDeleteIfEmpty"));
283284
assertFalse((Boolean) ReflectionTestUtils.getField(writer, "shouldDeleteIfExists"));
284285
assertTrue((Boolean) ReflectionTestUtils.getField(writer, "forceSync"));
286+
assertEquals( Charset.defaultCharset().name(), ReflectionTestUtils.getField(writer, "encoding"));
285287
}
286288

287289
private String readLine(String encoding, Resource outputFile ) throws IOException {

0 commit comments

Comments
 (0)