Class VirtualRandomAccessFile

  • All Implemented Interfaces:
    java.io.DataInput, java.io.DataOutput, StorageRandomAccessFile

    public class VirtualRandomAccessFile
    extends java.lang.Object
    implements StorageRandomAccessFile
    A random access file capable of reading and writing from/into a virtual file whose data is represented by a BlockedByteArray.

    If the file is opened in read-only mode and the caller invokes one of the write methods, it will fail with a NullPointerException.

    • Field Detail

      • _readOnly

        private final boolean _readOnly
        whether the file is read-only
      • fp

        private long fp
        Current position / file pointer.
      • dIs

        private final java.io.DataInputStream dIs
        Data input stream on top of the source input stream.
      • bOut

        private final BlockedByteArrayOutputStream bOut
        Stream used to write into the source entry. Will be null if the file is opened in read-only mode.
      • dOs

        private final java.io.DataOutputStream dOs
        Data output stream on top of the source output stream. Will be null if the file is opened in read-only mode.
    • Constructor Detail

      • VirtualRandomAccessFile

        public VirtualRandomAccessFile​(DataStoreEntry entry,
                                       boolean readOnly)
                                throws java.io.FileNotFoundException
        Creates a new virtual random access file.
        Parameters:
        entry - the source entry
        readOnly - if the file should be opened read-only or not
        Throws:
        java.io.FileNotFoundException - if the denoted path is a directory, or the denoted file has been marked read-only and the file is opened for writing
    • Method Detail

      • seek

        public void seek​(long newFilePointer)
                  throws java.io.IOException
        Description copied from interface: StorageRandomAccessFile
        Set the file pointer. It may be moved beyond the end of the file, but this does not change the length of the file. The length of the file is not changed until data is actually written..
        Specified by:
        seek in interface StorageRandomAccessFile
        Parameters:
        newFilePointer - the new file pointer, measured in bytes from the beginning of the file.
        Throws:
        java.io.IOException - - if newFilePointer is less than 0 or an I/O error occurs.
      • setLength

        public void setLength​(long newLength)
        Description copied from interface: StorageRandomAccessFile
        Sets the length of this file, either extending or truncating it.

        If the file is extended then the contents of the extension are not defined.

        If the file is truncated and the file pointer is greater than the new length then the file pointer is set to the new length.

        Specified by:
        setLength in interface StorageRandomAccessFile
        Parameters:
        newLength - The new file length.
      • sync

        public void sync()
        Description copied from interface: StorageRandomAccessFile
        Force any changes out to the persistent store. If the database is to be transient, that is, if the database does not survive a restart, then the sync method implementation need not do anything.
        Specified by:
        sync in interface StorageRandomAccessFile
      • read

        public int read​(byte[] b,
                        int off,
                        int len)
                 throws java.io.IOException
        Description copied from interface: StorageRandomAccessFile
        Reads up to len bytes of data from this file into an array of bytes. This method blocks until at least one byte of input is available.

        Specified by:
        read in interface StorageRandomAccessFile
        Parameters:
        b - the buffer into which the data is read.
        off - the start offset in array b at which the data is written.
        len - the maximum number of bytes read.
        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.
      • readFully

        public void readFully​(byte[] b)
                       throws java.io.IOException
        Specified by:
        readFully in interface java.io.DataInput
        Throws:
        java.io.IOException
      • readFully

        public void readFully​(byte[] b,
                              int off,
                              int len)
                       throws java.io.IOException
        Specified by:
        readFully in interface java.io.DataInput
        Throws:
        java.io.IOException
      • skipBytes

        public int skipBytes​(int n)
        Specified by:
        skipBytes in interface java.io.DataInput
      • readBoolean

        public boolean readBoolean()
                            throws java.io.IOException
        Specified by:
        readBoolean in interface java.io.DataInput
        Throws:
        java.io.IOException
      • readByte

        public byte readByte()
                      throws java.io.IOException
        Specified by:
        readByte in interface java.io.DataInput
        Throws:
        java.io.IOException
      • readUnsignedByte

        public int readUnsignedByte()
                             throws java.io.IOException
        Specified by:
        readUnsignedByte in interface java.io.DataInput
        Throws:
        java.io.IOException
      • readShort

        public short readShort()
                        throws java.io.IOException
        Specified by:
        readShort in interface java.io.DataInput
        Throws:
        java.io.IOException
      • readUnsignedShort

        public int readUnsignedShort()
                              throws java.io.IOException
        Specified by:
        readUnsignedShort in interface java.io.DataInput
        Throws:
        java.io.IOException
      • readChar

        public char readChar()
                      throws java.io.IOException
        Specified by:
        readChar in interface java.io.DataInput
        Throws:
        java.io.IOException
      • readInt

        public int readInt()
                    throws java.io.IOException
        Specified by:
        readInt in interface java.io.DataInput
        Throws:
        java.io.IOException
      • readLong

        public long readLong()
                      throws java.io.IOException
        Specified by:
        readLong in interface java.io.DataInput
        Throws:
        java.io.IOException
      • readFloat

        public float readFloat()
                        throws java.io.IOException
        Specified by:
        readFloat in interface java.io.DataInput
        Throws:
        java.io.IOException
      • readDouble

        public double readDouble()
                          throws java.io.IOException
        Specified by:
        readDouble in interface java.io.DataInput
        Throws:
        java.io.IOException
      • readLine

        public java.lang.String readLine()
                                  throws java.io.IOException
        Specified by:
        readLine in interface java.io.DataInput
        Throws:
        java.io.IOException
      • readUTF

        public java.lang.String readUTF()
                                 throws java.io.IOException
        Specified by:
        readUTF in interface java.io.DataInput
        Throws:
        java.io.IOException
      • write

        public void write​(int b)
                   throws java.io.IOException
        Specified by:
        write in interface java.io.DataOutput
        Throws:
        java.io.IOException
      • write

        public void write​(byte[] b)
                   throws java.io.IOException
        Specified by:
        write in interface java.io.DataOutput
        Throws:
        java.io.IOException
      • write

        public void write​(byte[] b,
                          int off,
                          int len)
                   throws java.io.IOException
        Specified by:
        write in interface java.io.DataOutput
        Throws:
        java.io.IOException
      • writeBoolean

        public void writeBoolean​(boolean v)
                          throws java.io.IOException
        Specified by:
        writeBoolean in interface java.io.DataOutput
        Throws:
        java.io.IOException
      • writeByte

        public void writeByte​(int v)
                       throws java.io.IOException
        Specified by:
        writeByte in interface java.io.DataOutput
        Throws:
        java.io.IOException
      • writeShort

        public void writeShort​(int v)
                        throws java.io.IOException
        Specified by:
        writeShort in interface java.io.DataOutput
        Throws:
        java.io.IOException
      • writeChar

        public void writeChar​(int v)
                       throws java.io.IOException
        Specified by:
        writeChar in interface java.io.DataOutput
        Throws:
        java.io.IOException
      • writeInt

        public void writeInt​(int v)
                      throws java.io.IOException
        Specified by:
        writeInt in interface java.io.DataOutput
        Throws:
        java.io.IOException
      • writeLong

        public void writeLong​(long v)
                       throws java.io.IOException
        Specified by:
        writeLong in interface java.io.DataOutput
        Throws:
        java.io.IOException
      • writeFloat

        public void writeFloat​(float v)
                        throws java.io.IOException
        Specified by:
        writeFloat in interface java.io.DataOutput
        Throws:
        java.io.IOException
      • writeDouble

        public void writeDouble​(double v)
                         throws java.io.IOException
        Specified by:
        writeDouble in interface java.io.DataOutput
        Throws:
        java.io.IOException
      • writeBytes

        public void writeBytes​(java.lang.String s)
                        throws java.io.IOException
        Specified by:
        writeBytes in interface java.io.DataOutput
        Throws:
        java.io.IOException
      • writeChars

        public void writeChars​(java.lang.String s)
                        throws java.io.IOException
        Specified by:
        writeChars in interface java.io.DataOutput
        Throws:
        java.io.IOException
      • writeUTF

        public void writeUTF​(java.lang.String s)
                      throws java.io.IOException
        Specified by:
        writeUTF in interface java.io.DataOutput
        Throws:
        java.io.IOException