From fc06eb4da2f8f433bc57531fc79c5d1fa8719e05 Mon Sep 17 00:00:00 2001 From: lucasward Date: Thu, 26 Nov 2009 05:10:44 +0000 Subject: [PATCH] BATCH-1423: Merged FlatFileItemReader from 2.0.x into trunk --- .../batch/item/file/FlatFileItemReader.java | 68 ++++++++++++------- 1 file changed, 44 insertions(+), 24 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 8a91688a7..aab7aba96 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 @@ -177,35 +177,19 @@ public class FlatFileItemReader extends AbstractItemCountingItemStreamItemRea if (noInput) { return null; } + String line = readLine(); - String record = line; - while (line != null && !recordSeparatorPolicy.isEndOfRecord(record)) { - line = readLine(); - if (line == null) { - if (StringUtils.hasText(record)) { - // A record was partially complete since it hasn't ended but - // the line is null - throw new FlatFileParseException("Unexpected end of file before record complete", record, lineCount); - } - else { - // Record has no text but it might still be post processed - // to something (skipping preProcess since that was already done) - break; - } - } - 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); + try{ + return lineMapper.mapLine(line, lineCount); } - catch (Exception ex) { - logger.error("Parsing error at line: " + lineCount + " in resource=" + resource.getDescription() - + ", input=[" + line + "]", ex); + catch(Exception ex){ + logger.error("Parsing error at line: " + lineCount + " in resource=" + + resource.getDescription() + ", input=[" + line + "]", ex); throw ex; } } @@ -235,6 +219,8 @@ public class FlatFileItemReader extends AbstractItemCountingItemStreamItemRea } lineCount++; } + + line = applyRecordSeparatorPolicy(line); } catch (IOException e) { throw new FlatFileParseException("Unable to read from resource: [" + resource + "]", e, line, lineCount); @@ -286,5 +272,39 @@ public class FlatFileItemReader extends AbstractItemCountingItemStreamItemRea public void afterPropertiesSet() throws Exception { Assert.notNull(lineMapper, "LineMapper is required"); } + + @Override + protected void jumpToItem(int itemIndex) throws Exception { + for (int i = 0; i < itemIndex; i++) { + readLine(); + } + } + + private String applyRecordSeparatorPolicy(String line) throws IOException{ + + String record = line; + while (line != null && !recordSeparatorPolicy.isEndOfRecord(record)) { + line = this.reader.readLine(); + if (line == null) { + if (StringUtils.hasText(record)) { + // A record was partially complete since it hasn't ended but + // the line is null + throw new FlatFileParseException("Unexpected end of file before record complete", record, lineCount); + } + else { + // Record has no text but it might still be post processed + // to something (skipping preProcess since that was already done) + break; + } + } else { + lineCount++; + } + record = recordSeparatorPolicy.preProcess(record) + line; + } + + return recordSeparatorPolicy.postProcess(record); + + } + }