Skip to content

Commit

Permalink
FlatFileItemWriter now uses charset to determine default encoding
Browse files Browse the repository at this point in the history
resolves #1154

It now matches the encoding scheme of FlatFileItemReader
  • Loading branch information
Glenn Renfro committed May 17, 2021
1 parent b486e24 commit e3eb344
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2018 the original author or authors.
* Copyright 2006-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@

package org.springframework.batch.item.file;

import java.nio.charset.Charset;
import java.util.List;

import org.springframework.batch.item.file.transform.LineAggregator;
Expand All @@ -39,11 +40,15 @@
* @author Dave Syer
* @author Michael Minella
* @author Mahmoud Ben Hassine
* @author Glenn Renfro
*/
public class FlatFileItemWriter<T> extends AbstractFileItemWriter<T> {

protected LineAggregator<T> lineAggregator;

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

public FlatFileItemWriter() {
this.setExecutionContextName(ClassUtils.getShortName(FlatFileItemWriter.class));
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Arrays;

import org.junit.Test;
Expand All @@ -38,6 +39,7 @@
* @author Michael Minella
* @author Mahmoud Ben Hassine
* @author Drummond Dawson
* @author Glenn Renfro
*/
public class FlatFileItemWriterBuilderTests {

Expand Down Expand Up @@ -275,13 +277,13 @@ public void testFlags() throws Exception {
.transactional(false)
.lineAggregator(new PassThroughLineAggregator<>())
.build();

assertFalse((Boolean) ReflectionTestUtils.getField(writer, "saveState"));
assertTrue((Boolean) ReflectionTestUtils.getField(writer, "append"));
assertFalse((Boolean) ReflectionTestUtils.getField(writer, "transactional"));
assertTrue((Boolean) ReflectionTestUtils.getField(writer, "shouldDeleteIfEmpty"));
assertFalse((Boolean) ReflectionTestUtils.getField(writer, "shouldDeleteIfExists"));
assertTrue((Boolean) ReflectionTestUtils.getField(writer, "forceSync"));
assertEquals( Charset.defaultCharset().name(), ReflectionTestUtils.getField(writer, "encoding"));
}

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

0 comments on commit e3eb344

Please sign in to comment.