Skip to content

Commit

Permalink
Updated FlatFileItemWriter to eliminate empty line at EOF
Browse files Browse the repository at this point in the history
  • Loading branch information
parikshitdutta committed Apr 23, 2021
1 parent d6419f4 commit 39dea77
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
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,8 @@

package org.springframework.batch.item.file;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.springframework.batch.item.file.transform.LineAggregator;
Expand All @@ -39,6 +41,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 +77,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

0 comments on commit 39dea77

Please sign in to comment.