Package blbutil

Class 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 BlockLineReader is a blbutil.FileIt that reads blocks of lines from a file. The order of lines in the source file is preserved by the returned string arrays. The hasNext() method always returns true. After the final block of lines is returned by the next() method, the next() method returns BlockLineReader.SENTINAL on all subsequent invocations. BlockLineReader.SENTINAL is guaranteed to be the only returned array that has length 0.

    Instances of class BlockLineReader are 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[] SENTINAL
      The string array returned by next() after all blocks of lines have been read.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Stops reading data elements and releases any system resources that are held by this object.
      static BlockLineReader create​(FileIt<java.lang.String> it, int blockSize, int nBlocks)
      Constructs and returns a new BlockLineReader for the specified data.
      java.io.File file()
      Returns the file from which the data are read, or null if the data are read from standard input or if the data source is unknown.
      boolean hasNext()
      Returns the next element in the iteration.
      java.lang.String[] next()
      Returns true if the iteration has more elements, and returns false otherwise.
      void remove()
      The remove method is not supported by this iterator.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Iterator

        forEachRemaining
    • Field Detail

      • SENTINAL

        public static java.lang.String[] SENTINAL
        The string array returned by next() after all blocks of lines have been read.
    • Method Detail

      • create

        public static BlockLineReader create​(FileIt<java.lang.String> it,
                                             int blockSize,
                                             int nBlocks)
        Constructs and returns a new BlockLineReader for the specified data. The close() method of the returned object will invoke the close() method on the specified FileIt<String> iterator. The calling thread should not directly invoke any methods of the specified FileIt<String> after it is passed to the BlockLineReader.create() method.
        Parameters:
        it - a file iterator that returns the lines of text
        blockSize - the maximum length a string array returned by next()
        nBlocks - the maximum number of non-empty string arrays that will be buffered
        Returns:
        a BlockLineReader for the specified data.
        Throws:
        java.lang.IllegalArgumentException - if blockSize < 1 || nBlocks < 1
        java.lang.NullPointerException - if it == null
      • file

        public java.io.File file()
        Description copied from interface: FileIt
        Returns the file from which the data are read, or null if the data are read from standard input or if the data source is unknown.
        Specified by:
        file in interface FileIt<java.lang.String[]>
        Returns:
        the file from which the data are read, or null if the data are read from standard input or if the data source is unknown
      • close

        public void close()
        Description copied from interface: FileIt
        Stops reading data elements and releases any system resources that are held by this object. Buffered data elements may remain accessible via the hasNext() and next() methods after invoking close(). After invoking close(), further invocations of close() have no effect.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in interface FileIt<java.lang.String[]>
      • hasNext

        public boolean hasNext()
        Returns the next element in the iteration.
        Specified by:
        hasNext in interface java.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()
        Returns true if the iteration has more elements, and returns false otherwise.
        Specified by:
        next in interface java.util.Iterator<java.lang.String[]>
        Returns:
        true if the iteration has more elements
      • remove

        public void remove()
        The remove method is not supported by this iterator.
        Specified by:
        remove in interface java.util.Iterator<java.lang.String[]>
        Throws:
        java.lang.UnsupportedOperationException - if this method is invoked