Class ResourceLineReader

  extended by org.springframework.batch.item.ItemStreamSupport
      extended by org.springframework.batch.item.file.separator.ResourceLineReader
All Implemented Interfaces:
LineReader, ItemReader, ItemStream

public class ResourceLineReader
extends ItemStreamSupport
implements LineReader, ItemReader

An input source that reads lines one by one from a resource.
A line can consist of multiple lines in the input resource, according to the RecordSeparatorPolicy in force. By default a line is either terminated by a newline (as per BufferedReader.readLine()), or can be continued onto the next line if a field surrounded by quotes (\") contains a newline.
Comment lines can be indicated using a line prefix (or collection of prefixes) and they will be ignored. The default is "#", so lines starting with a pound sign will be ignored.
All the public methods that interact with the underlying resource (open, close, read etc.) are synchronized on this.
Package private because this is not intended to be a public API - used internally by the flat file input sources. That makes abuses of the fact that it is stateful easier to control.

Dave Syer, Rob Harrop

Constructor Summary
ResourceLineReader(Resource resource)
ResourceLineReader(Resource resource, String encoding)
Method Summary
 void close(ExecutionContext executionContext)
          Close the reader associated with this input source.
 int getPosition()
          Getter for current line count (not the current number of lines returned).
 void mark()
          Mark the state for return later with reset.
 void open()
          Creates internal state object.
 Object read()
          Read the next line from the input resource, ignoring comments, and according to the RecordSeparatorPolicy.
 void reset()
          Reset the reader to the last mark.
 void setComments(String[] comments)
          Setter for comment prefixes.
 void setRecordSeparatorPolicy(RecordSeparatorPolicy recordSeparatorPolicy)
          Setter for the RecordSeparatorPolicy.
Methods inherited from class org.springframework.batch.item.ItemStreamSupport
open, update
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.batch.item.ItemStream
open, update

Constructor Detail


public ResourceLineReader(Resource resource)
                   throws IOException


public ResourceLineReader(Resource resource,
                          String encoding)
Method Detail


public void setRecordSeparatorPolicy(RecordSeparatorPolicy recordSeparatorPolicy)
Setter for the RecordSeparatorPolicy. Default value is a DefaultRecordSeparatorPolicy. Ideally should not be changed once a reader is in use, but it would not be fatal if it was.

recordSeparatorPolicy - the new RecordSeparatorPolicy


public void setComments(String[] comments)
Setter for comment prefixes. Can be used to ignore header lines as well by using e.g. the first couple of column names as a prefix.

comments - an array of comment line prefixes.


public Object read()
Read the next line from the input resource, ignoring comments, and according to the RecordSeparatorPolicy.

Specified by:
read in interface ItemReader
a String.
See Also:


public void open()
Creates internal state object.


public void close(ExecutionContext executionContext)
Close the reader associated with this input source.

Specified by:
close in interface ItemStream
close in class ItemStreamSupport
executionContext - TODO
See Also:


public int getPosition()
Getter for current line count (not the current number of lines returned).

Specified by:
getPosition in interface LineReader
the current line count.


public void mark()
          throws MarkFailedException
Mark the state for return later with reset. Uses the read-ahead limit from an underlying BufferedReader, which means that there is a limit to how much data can be recovered if the mark needs to be reset.
Mark is supported as long as this ItemStream is used in a single-threaded environment. The state backing the mark is a single counter, keeping track of the current position, so multiple threads cannot be accommodated.

Specified by:
mark in interface ItemReader
MarkFailedException - if the mark could not be set.
See Also:


public void reset()
           throws ResetFailedException
Reset the reader to the last mark.

Specified by:
reset in interface ItemReader
ResetFailedException - if the reset is unsuccessful, e.g. if the read-ahead limit was breached.
See Also:

Copyright © 2008 SpringSource. All Rights Reserved.