Skip to content

Commit df8dac1

Browse files
committed
Change default encoding to UTF-8 in file-based readers and writers
This commit overrides the fix in #3910 by using UTF-8 as default encoding in file-based item readers and writers. Issue #1154
1 parent 12c5cb9 commit df8dac1

File tree

4 files changed

+9
-8
lines changed

4 files changed

+9
-8
lines changed

spring-batch-docs/src/main/asciidoc/readersAndWriters.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ interpreted, as described in the following table:
285285
|===============
286286
|Property|Type|Description
287287
|comments|String[]|Specifies line prefixes that indicate comment rows.
288-
|encoding|String|Specifies what text encoding to use. The default is the value of `Charset.defaultCharset()`.
288+
|encoding|String|Specifies what text encoding to use. The default value is `UTF-8`.
289289
|lineMapper|`LineMapper`|Converts a `String` to an `Object` representing the item.
290290
|linesToSkip|int|Number of lines to ignore at the top of the file.
291291
|recordSeparatorPolicy|RecordSeparatorPolicy|Used to determine where the line endings are

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2020 the original author or authors.
2+
* Copyright 2006-2022 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.BufferedReader;
2020
import java.io.IOException;
2121
import java.nio.charset.Charset;
22+
import java.nio.charset.StandardCharsets;
2223

2324
import org.apache.commons.logging.Log;
2425
import org.apache.commons.logging.LogFactory;
@@ -48,8 +49,7 @@ public class FlatFileItemReader<T> extends AbstractItemCountingItemStreamItemRea
4849

4950
private static final Log logger = LogFactory.getLog(FlatFileItemReader.class);
5051

51-
// default encoding for input files
52-
public static final String DEFAULT_CHARSET = Charset.defaultCharset().name();
52+
public static final String DEFAULT_CHARSET = StandardCharsets.UTF_8.name();
5353

5454
public static final String[] DEFAULT_COMMENT_PREFIXES = new String[] { "#" };
5555

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractFileItemWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.nio.channels.Channels;
2525
import java.nio.channels.FileChannel;
2626
import java.nio.charset.Charset;
27+
import java.nio.charset.StandardCharsets;
2728
import java.nio.charset.UnsupportedCharsetException;
2829
import java.util.List;
2930

@@ -74,8 +75,7 @@ public abstract class AbstractFileItemWriter<T> extends AbstractItemStreamItemWr
7475

7576
public static final String DEFAULT_LINE_SEPARATOR = System.getProperty("line.separator");
7677

77-
// default encoding for writing to flat files - set to charset of this Java virtual machine.
78-
public static final String DEFAULT_CHARSET = Charset.defaultCharset().name();
78+
public static final String DEFAULT_CHARSET = StandardCharsets.UTF_8.name();
7979

8080
private static final String WRITTEN_STATISTICS_NAME = "written";
8181

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemReader.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2020 the original author or authors.
2+
* Copyright 2006-2022 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.IOException;
2020
import java.io.InputStream;
2121
import java.nio.charset.Charset;
22+
import java.nio.charset.StandardCharsets;
2223
import java.util.ArrayList;
2324
import java.util.List;
2425
import java.util.NoSuchElementException;
@@ -64,7 +65,7 @@ public class StaxEventItemReader<T> extends AbstractItemCountingItemStreamItemRe
6465

6566
private static final Log logger = LogFactory.getLog(StaxEventItemReader.class);
6667

67-
public static final String DEFAULT_ENCODING = Charset.defaultCharset().name();
68+
public static final String DEFAULT_ENCODING = StandardCharsets.UTF_8.name();
6869

6970
private FragmentEventReader fragmentReader;
7071

0 commit comments

Comments
 (0)