Class BufferedRandomAccessFile

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

    public class BufferedRandomAccessFile
    extends java.io.BufferedInputStream
    BufferedInputStream impl with similar facilities as a RandomAccessFile. The advantage is that this class allows buffering larger byte streams from the file upfront, reducing thus the total IO operations to read an entire file.

    BufferedInputStream can be used only for read-operations.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_BUFFER_SIZE_BYTES  
      • Fields inherited from class java.io.BufferedInputStream

        buf, count, marklimit, markpos, pos
      • Fields inherited from class java.io.FilterInputStream

        in
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int bufferSize()
      Retrieves the buffer size used by this instance.
      void close()
      Closes this random access file stream and releases any system resources associated with the stream.
      java.io.FileDescriptor getFD()
      Returns the opaque file descriptor object associated with this stream.
      long getFilePointer()
      Returns the current offset in this file.
      long length()
      Returns the length of this file.
      int read()  
      int read​(byte[] b)  
      int read​(byte[] b, int off, int len)  
      int readInt()
      Reads a signed 32-bit integer from this file.
      void resetBufferPositions()
      Discards the buffer data, by setting the internal pos and count from inherited from BufferedInputStream back to 0.
      void seek​(long newPosition)
      Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs, and resets the internal buffer.
      long skip​(long n)  
      • Methods inherited from class java.io.BufferedInputStream

        available, mark, markSupported, reset
      • Methods inherited from class java.io.InputStream

        nullInputStream, readAllBytes, readNBytes, readNBytes, transferTo
      • Methods inherited from class java.lang.Object

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

      • DEFAULT_BUFFER_SIZE_BYTES

        public static final int DEFAULT_BUFFER_SIZE_BYTES
        See Also:
        Constant Field Values
    • Constructor Detail

      • BufferedRandomAccessFile

        public BufferedRandomAccessFile​(java.lang.String file)
                                 throws java.io.IOException
        Same as BufferedRandomAccessFile(String, int) except that it will create a File object from the file string parameter and it will use the DEFAULT_BUFFER_SIZE_BYTES for bufferSize parameter.
        Throws:
        java.io.IOException
      • BufferedRandomAccessFile

        public BufferedRandomAccessFile​(java.lang.String file,
                                        int bufferSize)
                                 throws java.io.IOException
        Same as BufferedRandomAccessFile(File, int) except it will create a File object from the file string parameter.
        Throws:
        java.io.IOException
      • BufferedRandomAccessFile

        public BufferedRandomAccessFile​(java.io.File file,
                                        int bufferSize)
                                 throws java.io.IOException
        Creates a BufferedRandomAccessFile with the specified buffer size, and creates an input stream from the file argument, which is saved for later use. An internal buffer array of length bufferSize is created and stored in buf.
        Parameters:
        file - the file object from which the underlying input stream will be created.
        bufferSize - the buffer size.
        Throws:
        java.lang.IllegalArgumentException - if file is null or if bufferSize is equal to or less than zero.
        java.io.FileNotFoundException - if the given file object does not denote an existing regular file, or if some other error occurs while opening the file.
        java.lang.SecurityException - if a security manager exists and its checkRead method denies read access to the file.
        java.io.IOException
    • Method Detail

      • close

        public void close()
                   throws java.io.IOException
        Closes this random access file stream and releases any system resources associated with the stream. A closed random access file cannot perform input or output operations and cannot be reopened.

        If this file has an associated channel then the channel is closed as well.

        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.BufferedInputStream
        Throws:
        java.io.IOException - if an I/O error occurs.
      • getFilePointer

        public long getFilePointer()
                            throws java.io.IOException
        Returns the current offset in this file.
        Returns:
        the offset from the beginning of the file, in bytes, at which the next read or write occurs.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • getFD

        public java.io.FileDescriptor getFD()
                                     throws java.io.IOException
        Returns the opaque file descriptor object associated with this stream.
        Returns:
        the file descriptor object associated with this stream.
        Throws:
        java.io.IOException - if an I/O error occurs.
        See Also:
        FileDescriptor
      • length

        public long length()
                    throws java.io.IOException
        Returns the length of this file.
        Returns:
        the length of this file, measured in bytes.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • seek

        public void seek​(long newPosition)
                  throws java.io.IOException
        Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs, and resets the internal buffer. The offset may be set beyond the end of the file. Setting the offset beyond the end of the file does not change the file length. The file length will change only by writing after the offset has been set beyond the end of the file.

        Parameters:
        newPosition - the offset position, measured in bytes from the beginning of the file, at which to set the file pointer.
        Throws:
        java.io.IOException - if newPosition is less than 0 or if an I/O error occurs.
      • resetBufferPositions

        public void resetBufferPositions()
        Discards the buffer data, by setting the internal pos and count from inherited from BufferedInputStream back to 0.
      • bufferSize

        public int bufferSize()
        Retrieves the buffer size used by this instance.
        Returns:
        the number of bytes reserved for buffering.
      • read

        public int read()
                 throws java.io.IOException
        Overrides:
        read in class java.io.BufferedInputStream
        Throws:
        java.io.IOException
      • read

        public int read​(byte[] b)
                 throws java.io.IOException
        Overrides:
        read in class java.io.FilterInputStream
        Throws:
        java.io.IOException
      • read

        public int read​(byte[] b,
                        int off,
                        int len)
                 throws java.io.IOException
        Overrides:
        read in class java.io.BufferedInputStream
        Throws:
        java.io.IOException
      • skip

        public long skip​(long n)
                  throws java.io.IOException
        Overrides:
        skip in class java.io.BufferedInputStream
        Throws:
        java.io.IOException
      • readInt

        public int readInt()
                    throws java.io.IOException
        Reads a signed 32-bit integer from this file. This method reads 4 bytes from the file, starting at the current file pointer. If the bytes read, in order, are b1, b2, b3, and b4, where 0 <= b1, b2, b3, b4 <= 255, then the result is equal to:
             (b1 << 24) | (b2 << 16) + (b3 << 8) + b4
         

        This method blocks until the four bytes are read, the end of the stream is detected, or an exception is thrown.

        Returns:
        the next four bytes of this file, interpreted as an int.
        Throws:
        java.io.EOFException - if this file reaches the end before reading four bytes.
        java.io.IOException - if an I/O error occurs.