BATCH-2672: add method to set the encoding in the FlatFileItemReaderBuilder

This commit is contained in:
Mahmoud Ben Hassine
2018-01-25 15:05:04 +01:00
parent c78701a871
commit 8af6841fa6
2 changed files with 54 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-2018 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.
@@ -53,6 +53,7 @@ import org.springframework.util.StringUtils;
*
* @author Michael Minella
* @author Glenn Renfro
* @author Mahmoud Ben Hassine
* @since 4.0
* @see FlatFileItemReader
*/
@@ -62,6 +63,8 @@ public class FlatFileItemReaderBuilder<T> {
private boolean strict = true;
private String encoding = FlatFileItemReader.DEFAULT_CHARSET;
private RecordSeparatorPolicy recordSeparatorPolicy =
new SimpleRecordSeparatorPolicy();
@@ -222,6 +225,19 @@ public class FlatFileItemReaderBuilder<T> {
return this;
}
/**
* Configure the encoding used by the reader to read the input source.
* Default value is {@link FlatFileItemReader#DEFAULT_CHARSET}.
*
* @param encoding to use to read the input source.
* @return The current instance of the builder.
* @see FlatFileItemReader#setEncoding(String)
*/
public FlatFileItemReaderBuilder<T> encoding(String encoding) {
this.encoding = encoding;
return this;
}
/**
* The number of lines to skip at the beginning of reading the file.
*
@@ -420,6 +436,10 @@ public class FlatFileItemReaderBuilder<T> {
reader.setName(this.name);
}
if(StringUtils.hasText(this.encoding)) {
reader.setEncoding(this.encoding);
}
reader.setResource(this.resource);
if(this.lineMapper != null) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-2018 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.
@@ -35,6 +35,7 @@ import org.springframework.context.annotation.Scope;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.test.util.ReflectionTestUtils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@@ -43,6 +44,7 @@ import static org.junit.Assert.fail;
/**
* @author Michael Minella
* @author Mahmoud Ben Hassine
*/
public class FlatFileItemReaderBuilderTests {
@@ -403,6 +405,36 @@ public class FlatFileItemReaderBuilderTests {
}
@Test
public void testDefaultEncoding() {
String encoding = FlatFileItemReader.DEFAULT_CHARSET;
FlatFileItemReader<Foo> reader = new FlatFileItemReaderBuilder<Foo>()
.name("fooReader")
.resource(getResource("1,2,3"))
.delimited()
.names(new String[] {"first", "second", "third"})
.targetType(Foo.class)
.build();
assertEquals(encoding, ReflectionTestUtils.getField(reader, "encoding"));
}
@Test
public void testCustomEncoding() {
String encoding = "UTF-8";
FlatFileItemReader<Foo> reader = new FlatFileItemReaderBuilder<Foo>()
.name("fooReader")
.resource(getResource("1 2 3"))
.encoding(encoding)
.fixedLength()
.columns(new Range[] {new Range(1, 3), new Range(4, 6), new Range(7)})
.names(new String[] {"first", "second", "third"})
.targetType(Foo.class)
.build();
assertEquals(encoding, ReflectionTestUtils.getField(reader, "encoding"));
}
private Resource getResource(String contents) {
return new ByteArrayResource(contents.getBytes());
}