Class BufferedRandomAccessFile

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

    public class BufferedRandomAccessFile
    extends java.io.RandomAccessFile
    This class is a version of the one published at https://code.google.com/p/jmzreader/wiki/BufferedRandomAccessFile augmented to handle unsigned bytes. The original class is published under Apache 2.0 license. Fix is marked below This is an optimized version of the RandomAccessFile class as described by Nick Zhang on JavaWorld.com. The article can be found at http://www.javaworld.com/javaworld/javatips/jw-javatip26.html
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int bufend  
      private byte[] buffer
      Uses a byte instead of a char buffer for efficiency reasons.
      private int bufpos  
      private int BUFSIZE
      Buffer size.
      private long realpos
      The position inside the actual file.
    • Constructor Summary

      Constructors 
      Constructor Description
      BufferedRandomAccessFile​(java.io.File file, java.lang.String mode, int bufsize)
      Creates a new instance of the BufferedRandomAccessFile.
      BufferedRandomAccessFile​(java.lang.String filename, java.lang.String mode, int bufsize)
      Creates a new instance of the BufferedRandomAccessFile.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private int fillBuffer()
      Reads the next BUFSIZE bytes into the internal buffer.
      long getFilePointer()
      private void invalidate()
      Clears the local buffer.
      int read()
      int read​(byte[] b, int off, int len)
      void seek​(long pos)
      • Methods inherited from class java.io.RandomAccessFile

        close, getChannel, getFD, length, read, readBoolean, readByte, readChar, readDouble, readFloat, readFully, readFully, readInt, readLine, readLong, readShort, readUnsignedByte, readUnsignedShort, readUTF, setLength, skipBytes, write, write, write, writeBoolean, writeByte, writeBytes, writeChar, writeChars, writeDouble, writeFloat, writeInt, writeLong, writeShort, writeUTF
      • Methods inherited from class java.lang.Object

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

      • buffer

        private final byte[] buffer
        Uses a byte instead of a char buffer for efficiency reasons.
      • bufend

        private int bufend
      • bufpos

        private int bufpos
      • realpos

        private long realpos
        The position inside the actual file.
      • BUFSIZE

        private final int BUFSIZE
        Buffer size.
    • Constructor Detail

      • BufferedRandomAccessFile

        public BufferedRandomAccessFile​(java.lang.String filename,
                                        java.lang.String mode,
                                        int bufsize)
                                 throws java.io.FileNotFoundException
        Creates a new instance of the BufferedRandomAccessFile.
        Parameters:
        filename - The path of the file to open.
        mode - Specifies the mode to use ("r", "rw", etc.) See the BufferedLineReader documentation for more information.
        bufsize - The buffer size (in bytes) to use.
        Throws:
        java.io.FileNotFoundException - If the mode is "r" but the given string does not denote an existing regular file, or if the mode begins with "rw" but the given string does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file.
      • BufferedRandomAccessFile

        public BufferedRandomAccessFile​(java.io.File file,
                                        java.lang.String mode,
                                        int bufsize)
                                 throws java.io.FileNotFoundException
        Creates a new instance of the BufferedRandomAccessFile.
        Parameters:
        file - The file to open.
        mode - Specifies the mode to use ("r", "rw", etc.) See the BufferedLineReader documentation for more information.
        bufsize - The buffer size (in bytes) to use.
        Throws:
        java.io.FileNotFoundException - If the mode is "r" but the given file path does not denote an existing regular file, or if the mode begins with "rw" but the given file path does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file.
    • Method Detail

      • read

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

        private int fillBuffer()
                        throws java.io.IOException
        Reads the next BUFSIZE bytes into the internal buffer.
        Returns:
        The total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.
        Throws:
        java.io.IOException - If the first byte cannot be read for any reason other than end of file, or if the random access file has been closed, or if some other I/O error occurs.
      • invalidate

        private void invalidate()
                         throws java.io.IOException
        Clears the local buffer.
        Throws:
        java.io.IOException - If an I/O error occurs.
      • read

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

        public long getFilePointer()
                            throws java.io.IOException
        Overrides:
        getFilePointer in class java.io.RandomAccessFile
        Throws:
        java.io.IOException
      • seek

        public void seek​(long pos)
                  throws java.io.IOException
        Overrides:
        seek in class java.io.RandomAccessFile
        Throws:
        java.io.IOException