From 172506250e35062ec78c674325ecc6b98dfa64fa Mon Sep 17 00:00:00 2001 From: lucasward Date: Wed, 30 Sep 2009 04:50:33 +0000 Subject: [PATCH] BATCH-1418: Added jumpToItem implementation in FFIR to all for correct restart --- .../batch/item/file/FlatFileItemReader.java | 44 +++++++++++++------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemReader.java index b7f9e42b9..c6b23080d 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemReader.java @@ -163,24 +163,15 @@ public class FlatFileItemReader extends AbstractItemCountingItemStreamItemRea if (noInput) { return null; } + String line = readLine(); - String record = line; - if (line != null) { - while (line != null && !recordSeparatorPolicy.isEndOfRecord(record)) { - line = readLine(); - if (line==null) { - throw new FlatFileParseException("Unexpected end of file before record complete", record, lineCount); - } - record = recordSeparatorPolicy.preProcess(record) + line; - } - } - String logicalLine = recordSeparatorPolicy.postProcess(record); - if (logicalLine == null) { + + if (line == null) { return null; } else { try{ - return lineMapper.mapLine(logicalLine, lineCount); + return lineMapper.mapLine(line, lineCount); } catch(Exception ex){ logger.error("Parsing error at line: " + lineCount + " in resource=" + @@ -200,13 +191,13 @@ public class FlatFileItemReader extends AbstractItemCountingItemStreamItemRea } String line = null; - try { line = this.reader.readLine(); if (line == null) { return null; } lineCount++; + while (isComment(line)) { line = reader.readLine(); if (line == null) { @@ -214,12 +205,30 @@ public class FlatFileItemReader extends AbstractItemCountingItemStreamItemRea } lineCount++; } + + line = applyRecordSeparatorPolicy(line); } catch (IOException e) { throw new FlatFileParseException("Unable to read from resource: [" + resource + "]", e, line, lineCount); } return line; } + + private String applyRecordSeparatorPolicy(String line) throws IOException{ + + String record = line; + while (line != null && !recordSeparatorPolicy.isEndOfRecord(record)) { + line = this.reader.readLine(); + if (line==null) { + throw new FlatFileParseException("Unexpected end of file before record complete", record, lineCount); + } + record = recordSeparatorPolicy.preProcess(record) + line; + lineCount++; + } + + return recordSeparatorPolicy.postProcess(record); + + } private boolean isComment(String line) { for (String prefix : comments) { @@ -261,6 +270,13 @@ public class FlatFileItemReader extends AbstractItemCountingItemStreamItemRea } } } + + @Override + protected void jumpToItem(int itemIndex) throws Exception { + for (int i = 0; i < itemIndex; i++) { + readLine(); + } + } public void afterPropertiesSet() throws Exception { Assert.notNull(lineMapper, "LineMapper is required");