Class RandomAccessReader

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.lang.Readable

    public class RandomAccessReader
    extends java.io.Reader
    RandomAccessReader extends Reader to provide a means to create buffered Readers from RandomAccessFiles.
    Since:
    1.2
    Author:
    Keith James
    • Field Summary

      • Fields inherited from class java.io.Reader

        lock
    • Constructor Summary

      Constructors 
      Constructor Description
      RandomAccessReader​(java.io.RandomAccessFile raf)
      Creates a new RandomAccessReader wrapping the RandomAccessFile and using a default-sized buffer (8192 bytes).
      RandomAccessReader​(java.io.RandomAccessFile raf, int sz)
      Creates a new RandomAccessReader wrapping the RandomAccessFile and using a buffer of the specified size.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      close closes the underlying RandomAccessFile.
      long getFilePointer()
      getFilePointer returns the effective position of the pointer in the underlying RandomAccessFile.
      long length()
      length returns the length of the underlying RandomAccessFile.
      int read()
      read reads one byte from the underlying RandomAccessFile.
      int read​(char[] cbuf, int off, int len)
      read reads from the underlying RandomAccessFile into an array.
      void seek​(long pos)
      seek moves the pointer to the specified position.
      • Methods inherited from class java.io.Reader

        mark, markSupported, nullReader, read, read, ready, reset, skip, transferTo
      • Methods inherited from class java.lang.Object

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

      • RandomAccessReader

        public RandomAccessReader​(java.io.RandomAccessFile raf)
                           throws java.io.IOException
        Creates a new RandomAccessReader wrapping the RandomAccessFile and using a default-sized buffer (8192 bytes).
        Parameters:
        raf - a RandomAccessFile to wrap.
        Throws:
        java.io.IOException - if an error occurs.
      • RandomAccessReader

        public RandomAccessReader​(java.io.RandomAccessFile raf,
                                  int sz)
                           throws java.io.IOException
        Creates a new RandomAccessReader wrapping the RandomAccessFile and using a buffer of the specified size.
        Parameters:
        raf - a RandomAccessFile to wrap.
        sz - an int buffer size.
        Throws:
        java.io.IOException
    • Method Detail

      • close

        public void close()
                   throws java.io.IOException
        close closes the underlying RandomAccessFile.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in class java.io.Reader
        Throws:
        java.io.IOException - if an error occurs.
      • length

        public long length()
                    throws java.io.IOException
        length returns the length of the underlying RandomAccessFile.
        Returns:
        a long.
        Throws:
        java.io.IOException - if an error occurs.
      • read

        public final int read()
                       throws java.io.IOException
        read reads one byte from the underlying RandomAccessFile.
        Overrides:
        read in class java.io.Reader
        Returns:
        an int, -1 if the end of the stream has been reached.
        Throws:
        java.io.IOException - if an error occurs.
      • read

        public int read​(char[] cbuf,
                        int off,
                        int len)
                 throws java.io.IOException
        read reads from the underlying RandomAccessFile into an array.
        Specified by:
        read in class java.io.Reader
        Parameters:
        cbuf - a char [] array to read into.
        off - an int offset in the array at which to start storing chars.
        len - an int maximum number of char to read.
        Returns:
        an int number of chars read, or -1 if the end of the stream has been reached.
        Throws:
        java.io.IOException - if an error occurs.
      • getFilePointer

        public long getFilePointer()
                            throws java.io.IOException
        getFilePointer returns the effective position of the pointer in the underlying RandomAccessFile.
        Returns:
        a long offset.
        Throws:
        java.io.IOException - if an error occurs.
      • seek

        public void seek​(long pos)
                  throws java.io.IOException
        seek moves the pointer to the specified position.
        Parameters:
        pos - a long offset.
        Throws:
        java.io.IOException - if an error occurs.