org.springframework.batch.item.file.separator
Class ResourceLineReader

java.lang.Object
  extended by org.springframework.batch.item.file.separator.ResourceLineReader
All Implemented Interfaces:
LineReader, ItemReader

public class ResourceLineReader
extends Object
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.

Author:
Dave Syer, Rob Harrop

Constructor Summary
ResourceLineReader(Resource resource)
           
ResourceLineReader(Resource resource, String encoding)
           
 
Method Summary
 void close()
          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 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ResourceLineReader

public ResourceLineReader(Resource resource)
                   throws IOException
Throws:
IOException

ResourceLineReader

public ResourceLineReader(Resource resource,
                          String encoding)
Method Detail

setRecordSeparatorPolicy

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.

Parameters:
recordSeparatorPolicy - the new RecordSeparatorPolicy

setComments

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.

Parameters:
comments - an array of comment line prefixes.

read

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

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

open

public void open()
Creates internal state object.

Specified by:
open in interface LineReader

close

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

Specified by:
close in interface LineReader

getPosition

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

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

mark

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
Throws:
MarkFailedException - if the mark could not be set.
See Also:
reset()

reset

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

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


Copyright © 2009 SpringSource. All Rights Reserved.