Package blbutil
Class BlockLineReader
- java.lang.Object
-
- blbutil.BlockLineReader
-
- All Implemented Interfaces:
FileIt<java.lang.String[]>,java.io.Closeable,java.lang.AutoCloseable,java.util.Iterator<java.lang.String[]>
public class BlockLineReader extends java.lang.Object implements FileIt<java.lang.String[]>
Class
BlockLineReaderis ablbutil.FileItthat reads blocks of lines from a file. The order of lines in the source file is preserved by the returned string arrays. ThehasNext()method always returnstrue. After the final block of lines is returned by thenext()method, thenext()method returnsBlockLineReader.SENTINALon all subsequent invocations.BlockLineReader.SENTINALis guaranteed to be the only returned array that has length 0.Instances of class
BlockLineReaderare thread-safe.Methods of this class will terminate the Java Virtual Machine with an error message if an I/O Exception is encountered.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String[]SENTINALThe string array returned bynext()after all blocks of lines have been read.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Stops reading data elements and releases any system resources that are held by this object.static BlockLineReadercreate(FileIt<java.lang.String> it, int blockSize, int nBlocks)Constructs and returns a newBlockLineReaderfor the specified data.java.io.Filefile()Returns the file from which the data are read, ornullif the data are read from standard input or if the data source is unknown.booleanhasNext()Returns the next element in the iteration.java.lang.String[]next()Returnstrueif the iteration has more elements, and returnsfalseotherwise.voidremove()Theremovemethod is not supported by this iterator.
-
-
-
Method Detail
-
create
public static BlockLineReader create(FileIt<java.lang.String> it, int blockSize, int nBlocks)
Constructs and returns a newBlockLineReaderfor the specified data. Theclose()method of the returned object will invoke theclose()method on the specifiedFileIt<String>iterator. The calling thread should not directly invoke any methods of the specifiedFileIt<String>after it is passed to theBlockLineReader.create()method.- Parameters:
it- a file iterator that returns the lines of textblockSize- the maximum length a string array returned bynext()nBlocks- the maximum number of non-empty string arrays that will be buffered- Returns:
- a
BlockLineReaderfor the specified data. - Throws:
java.lang.IllegalArgumentException- ifblockSize < 1 || nBlocks < 1java.lang.NullPointerException- ifit == null
-
file
public java.io.File file()
Description copied from interface:FileItReturns the file from which the data are read, ornullif the data are read from standard input or if the data source is unknown.
-
close
public void close()
Description copied from interface:FileItStops reading data elements and releases any system resources that are held by this object. Buffered data elements may remain accessible via thehasNext()andnext()methods after invokingclose(). After invokingclose(), further invocations ofclose()have no effect.
-
hasNext
public boolean hasNext()
Returns the next element in the iteration.- Specified by:
hasNextin interfacejava.util.Iterator<java.lang.String[]>- Returns:
- the next element in the iteration
- Throws:
java.util.NoSuchElementException- if the iteration has no more elements
-
next
public java.lang.String[] next()
Returnstrueif the iteration has more elements, and returnsfalseotherwise.- Specified by:
nextin interfacejava.util.Iterator<java.lang.String[]>- Returns:
trueif the iteration has more elements
-
remove
public void remove()
Theremovemethod is not supported by this iterator.- Specified by:
removein interfacejava.util.Iterator<java.lang.String[]>- Throws:
java.lang.UnsupportedOperationException- if this method is invoked
-
-