Class ReverseLineReader


  • public class ReverseLineReader
    extends java.lang.Object
    Reads in a single line of text from a given input file, in reverse order. CONOPS: 1. Start at a given position in the file and read BUFFER_SIZE bytes into a byte array 2. From the end of the array, read a character 3. If the character represents a newline (or carriage return), the line is finished, so return. 4. If not, continue reading.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      java.io.RandomAccessFile raf  
    • Constructor Summary

      Constructors 
      Constructor Description
      ReverseLineReader​(java.lang.String encoding, java.io.RandomAccessFile raf)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String readLine()
      Reads a single line from the current file pointer position, in reverse.
      void setFilePos​(long position)
      Moves the file pointer to the given byte location.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • raf

        public java.io.RandomAccessFile raf
    • Constructor Detail

      • ReverseLineReader

        public ReverseLineReader​(java.lang.String encoding,
                                 java.io.RandomAccessFile raf)
                          throws java.io.IOException
        Parameters:
        encoding -
        raf -
        Throws:
        java.io.IOException
    • Method Detail

      • setFilePos

        public void setFilePos​(long position)
        Moves the file pointer to the given byte location.
        Parameters:
        position -
      • readLine

        public java.lang.String readLine()
                                  throws java.io.IOException
        Reads a single line from the current file pointer position, in reverse. To do this we do the following: 1. Read a 'large enough' number of bytes into a buffer (enough to guarantee a full line of text. 2. Move backwards through the bytes just read until a newline or carriage return is found. 3. Throw away the rest of the bytes and return the line found.
        Returns:
        Throws:
        java.io.IOException