Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated FlatFileItemWriter to eliminate empty line at EOF #3895

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
3 changes: 1 addition & 2 deletions spring-batch-core/src/test/resources/expectedOutput.ldif
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,4 @@ objectclass: person
objectclass: organizationalPerson
sn: Jensen
cn: Horatio Jensen
cn: Horatio N Jensen

cn: Horatio N Jensen
Original file line number Diff line number Diff line change
@@ -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.util.Iterator;
import java.util.List;

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

Expand Down Expand Up @@ -74,8 +76,16 @@ public void setLineAggregator(LineAggregator<T> lineAggregator) {
@Override
public String doWrite(List<? extends T> items) {
StringBuilder lines = new StringBuilder();
for (T item : items) {
lines.append(this.lineAggregator.aggregate(item)).append(this.lineSeparator);
Iterator<? extends T> iterator = items.iterator();
if (hasExistingItems() && iterator.hasNext()) {
lines.append(lineSeparator);
}
while (iterator.hasNext()) {
T item = iterator.next();
lines.append(this.lineAggregator.aggregate(item));
if (iterator.hasNext()) {
lines.append(lineSeparator);
}
}
return lines.toString();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 Down Expand Up @@ -59,6 +59,7 @@
* @author Dave Syer
* @author Michael Minella
* @author Mahmoud Ben Hassine
* @author Parikshit Dutta
*
* @since 4.1
*/
Expand Down Expand Up @@ -271,6 +272,7 @@ public void close() {
if (state != null) {
try {
if (footerCallback != null && state.outputBufferedWriter != null) {
state.outputBufferedWriter.write(lineSeparator);
footerCallback.writeFooter(state.outputBufferedWriter);
state.outputBufferedWriter.flush();
}
Expand Down Expand Up @@ -325,7 +327,6 @@ private void doOpen(ExecutionContext executionContext) throws ItemStreamExceptio
if (headerCallback != null) {
try {
headerCallback.writeHeader(outputState.outputBufferedWriter);
outputState.write(lineSeparator);
}
catch (IOException e) {
throw new ItemStreamException("Could not write headers. The file may be corrupt.", e);
Expand Down Expand Up @@ -359,6 +360,15 @@ public void update(ExecutionContext executionContext) {
}
}

protected boolean hasExistingItems() {
try {
return (state.position() > 0);
}
catch (IOException ioException) {
throw new ItemStreamException("ItemStream is not able to read offset position");
}
}

// Returns object representing state.
protected OutputState getOutputState() {
if (state == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 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 Down Expand Up @@ -296,7 +296,7 @@ public void testWriteRecordWithrecordSeparator() throws Exception {
writer.open(executionContext);
writer.write(Arrays.asList(new String[] { "1", "2" }));
String lineFromFile = readLine();
assertEquals("1|2|", lineFromFile);
assertEquals("1|2", lineFromFile);
}

@Test
Expand Down