Class RandomAccessFile

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, RandomAccess, RandomAccessRead, RandomAccessWrite

    public class RandomAccessFile
    extends java.lang.Object
    implements RandomAccess
    A RandomAccess implementation which allows data to be stored in a scratch file on the disk to reduce memory consumption.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean isClosed  
      private java.io.RandomAccessFile ras  
    • Constructor Summary

      Constructors 
      Constructor Description
      RandomAccessFile​(java.io.File file, java.lang.String mode)
      Constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int available()
      Returns an estimate of the number of bytes that can be read.
      private void checkClosed()
      Ensure that the RandomAccessFile is not closed
      void clear()
      Clears all data of the buffer.
      void close()  
      long getPosition()
      Returns offset of next byte to be returned by a read method.
      boolean isClosed()
      Returns true if this stream has been closed.
      boolean isEOF()
      A simple test to see if we are at the end of the data.
      long length()
      The total number of bytes that are available.
      int peek()
      This will peek at the next byte.
      int read()
      Read a single byte of data.
      int read​(byte[] b)
      Read a buffer of data.
      int read​(byte[] b, int offset, int length)
      Read a buffer of data.
      byte[] readFully​(int length)
      Reads a given number of bytes.
      void rewind​(int bytes)
      Seek backwards the given number of bytes.
      void seek​(long position)
      Seek to a position in the data.
      void write​(byte[] b)
      Write a buffer of data to the stream.
      void write​(byte[] b, int offset, int length)
      Write a buffer of data to the stream.
      void write​(int b)
      Write a byte to the stream.
      • Methods inherited from class java.lang.Object

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

      • ras

        private final java.io.RandomAccessFile ras
      • isClosed

        private boolean isClosed
    • Constructor Detail

      • RandomAccessFile

        public RandomAccessFile​(java.io.File file,
                                java.lang.String mode)
                         throws java.io.FileNotFoundException
        Constructor.
        Parameters:
        file - The file to write the data to.
        mode - The writing mode.
        Throws:
        java.io.FileNotFoundException - If the file cannot be created.
    • Method Detail

      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • clear

        public void clear()
                   throws java.io.IOException
        Description copied from interface: RandomAccessWrite
        Clears all data of the buffer.
        Specified by:
        clear in interface RandomAccessWrite
        Throws:
        java.io.IOException
      • seek

        public void seek​(long position)
                  throws java.io.IOException
        Description copied from interface: RandomAccessRead
        Seek to a position in the data.
        Specified by:
        seek in interface RandomAccessRead
        Parameters:
        position - The position to seek to.
        Throws:
        java.io.IOException - If there is an error while seeking.
      • getPosition

        public long getPosition()
                         throws java.io.IOException
        Description copied from interface: RandomAccessRead
        Returns offset of next byte to be returned by a read method.
        Specified by:
        getPosition in interface RandomAccessRead
        Returns:
        offset of next byte which will be returned with next RandomAccessRead.read() (if no more bytes are left it returns a value >= length of source)
        Throws:
        java.io.IOException
      • read

        public int read()
                 throws java.io.IOException
        Description copied from interface: RandomAccessRead
        Read a single byte of data.
        Specified by:
        read in interface RandomAccessRead
        Returns:
        The byte of data that is being read.
        Throws:
        java.io.IOException - If there is an error while reading the data.
      • read

        public int read​(byte[] b)
                 throws java.io.IOException
        Description copied from interface: RandomAccessRead
        Read a buffer of data.
        Specified by:
        read in interface RandomAccessRead
        Parameters:
        b - The buffer to write the data to.
        Returns:
        The number of bytes that were actually read.
        Throws:
        java.io.IOException - If there was an error while reading the data.
      • read

        public int read​(byte[] b,
                        int offset,
                        int length)
                 throws java.io.IOException
        Description copied from interface: RandomAccessRead
        Read a buffer of data.
        Specified by:
        read in interface RandomAccessRead
        Parameters:
        b - The buffer to write the data to.
        offset - Offset into the buffer to start writing.
        length - The amount of data to attempt to read.
        Returns:
        The number of bytes that were actually read.
        Throws:
        java.io.IOException - If there was an error while reading the data.
      • length

        public long length()
                    throws java.io.IOException
        Description copied from interface: RandomAccessRead
        The total number of bytes that are available.
        Specified by:
        length in interface RandomAccessRead
        Returns:
        The number of bytes available.
        Throws:
        java.io.IOException - If there is an IO error while determining the length of the data stream.
      • checkClosed

        private void checkClosed()
                          throws java.io.IOException
        Ensure that the RandomAccessFile is not closed
        Throws:
        java.io.IOException
      • write

        public void write​(byte[] b,
                          int offset,
                          int length)
                   throws java.io.IOException
        Description copied from interface: RandomAccessWrite
        Write a buffer of data to the stream.
        Specified by:
        write in interface RandomAccessWrite
        Parameters:
        b - The buffer to get the data from.
        offset - An offset into the buffer to get the data from.
        length - The length of data to write.
        Throws:
        java.io.IOException - If there is an error while writing the data.
      • write

        public void write​(byte[] b)
                   throws java.io.IOException
        Description copied from interface: RandomAccessWrite
        Write a buffer of data to the stream.
        Specified by:
        write in interface RandomAccessWrite
        Parameters:
        b - The buffer to get the data from.
        Throws:
        java.io.IOException - If there is an error while writing the data.
      • write

        public void write​(int b)
                   throws java.io.IOException
        Description copied from interface: RandomAccessWrite
        Write a byte to the stream.
        Specified by:
        write in interface RandomAccessWrite
        Parameters:
        b - The byte to write.
        Throws:
        java.io.IOException - If there is an IO error while writing.
      • peek

        public int peek()
                 throws java.io.IOException
        Description copied from interface: RandomAccessRead
        This will peek at the next byte.
        Specified by:
        peek in interface RandomAccessRead
        Returns:
        The next byte on the stream, leaving it as available to read.
        Throws:
        java.io.IOException - If there is an error reading the next byte.
      • rewind

        public void rewind​(int bytes)
                    throws java.io.IOException
        Description copied from interface: RandomAccessRead
        Seek backwards the given number of bytes.
        Specified by:
        rewind in interface RandomAccessRead
        Parameters:
        bytes - the number of bytes to be seeked backwards
        Throws:
        java.io.IOException - If there is an error while seeking
      • readFully

        public byte[] readFully​(int length)
                         throws java.io.IOException
        Description copied from interface: RandomAccessRead
        Reads a given number of bytes.
        Specified by:
        readFully in interface RandomAccessRead
        Parameters:
        length - the number of bytes to be read
        Returns:
        a byte array containing the bytes just read
        Throws:
        java.io.IOException - if an I/O error occurs while reading data
      • isEOF

        public boolean isEOF()
                      throws java.io.IOException
        Description copied from interface: RandomAccessRead
        A simple test to see if we are at the end of the data.
        Specified by:
        isEOF in interface RandomAccessRead
        Returns:
        true if we are at the end of the data.
        Throws:
        java.io.IOException - If there is an error reading the next byte.
      • available

        public int available()
                      throws java.io.IOException
        Description copied from interface: RandomAccessRead
        Returns an estimate of the number of bytes that can be read.
        Specified by:
        available in interface RandomAccessRead
        Returns:
        the number of bytes that can be read
        Throws:
        java.io.IOException - if this random access has been closed