Class RegularFile


  • final class RegularFile
    extends File
    A mutable, resizable store for bytes. Bytes are stored in fixed-sized byte arrays (blocks) allocated by a HeapDisk.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int blockCount
      Block count for the the file, which also acts as the head of the block list.
      private byte[][] blocks
      Block list for the file.
      private boolean deleted  
      private HeapDisk disk  
      private java.util.concurrent.locks.ReadWriteLock lock  
      private int openCount  
      private long size  
    • Constructor Summary

      Constructors 
      Constructor Description
      RegularFile​(int id, java.nio.file.attribute.FileTime creationTime, HeapDisk disk, byte[][] blocks, int blockCount, long size)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) void addBlock​(byte[] block)
      Adds the given block to the end of this file.
      (package private) int blockCount()
      Returns the number of blocks this file contains.
      private byte[] blockForWrite​(int index)
      Gets the block at the given index, expanding to create the block if necessary.
      private int blockIndex​(long position)  
      private long bytesToRead​(long pos, long max)
      Returns the number of bytes that can be read starting at position pos (up to a maximum of max) or -1 if pos is greater than or equal to the current size.
      void closed()
      Called when a stream or channel to this file is closed.
      (package private) java.util.concurrent.locks.ReadWriteLock contentLock()
      Returns the read-write lock for this file's content, or null if there is no content lock.
      (package private) void copyBlocksTo​(RegularFile target, int count)
      Copies the last count blocks from this file to the end of the given target file.
      (package private) void copyContentTo​(File file)
      Copies the content of this file to the given file.
      (package private) RegularFile copyWithoutContent​(int id, java.nio.file.attribute.FileTime creationTime)
      Creates a new file of the same type as this file with the given ID and creation time.
      static RegularFile create​(int id, java.nio.file.attribute.FileTime creationTime, HeapDisk disk)
      Creates a new regular file with the given ID and using the given disk.
      private void deleteContents()
      Deletes the contents of this file.
      void deleted()
      Marks this file as deleted.
      private void expandIfNecessary​(int minBlockCount)  
      private static int get​(byte[] block, int offset, byte[] b, int off, int len)
      Reads len bytes starting at the given offset in the given block into the given slice of the given byte array.
      private static int get​(byte[] block, int offset, java.nio.ByteBuffer buf, int len)
      Reads len bytes starting at the given offset in the given block into the given byte buffer.
      (package private) byte[] getBlock​(int index)
      Gets the block at the given index in this file.
      private int length​(int off, long max)  
      private int length​(long max)  
      private int offsetInBlock​(long position)  
      void opened()
      Called when a stream or channel to this file is opened.
      private void prepareForWrite​(long pos, long len)
      Prepares for a write of len bytes starting at position pos.
      private static int put​(byte[] block, int offset, byte[] b, int off, int len)
      Puts the given slice of the given array at the given offset in the given block.
      private static void put​(byte[] block, int offset, java.nio.ByteBuffer buf)
      Puts the contents of the given byte buffer at the given offset in the given block.
      int read​(long pos)
      Reads the byte at position pos in this file as an unsigned integer in the range 0-255.
      int read​(long pos, byte[] b, int off, int len)
      Reads up to len bytes starting at position pos in this file to the given byte array starting at offset off.
      long read​(long pos, java.lang.Iterable<java.nio.ByteBuffer> bufs)
      Reads up to the total remaining() number of bytes in each of bufs starting at position pos in this file to the given buffers, in order.
      int read​(long pos, java.nio.ByteBuffer buf)
      Reads up to buf.remaining() bytes starting at position pos in this file to the given buffer.
      java.util.concurrent.locks.Lock readLock()
      Returns the read lock for this file.
      long size()
      Returns the size, in bytes, of this file's content.
      long sizeWithoutLocking()
      Gets the current size of this file in bytes.
      (package private) void transferBlocksTo​(RegularFile target, int count)
      Transfers the last count blocks from this file to the end of the given target file.
      long transferFrom​(java.nio.channels.ReadableByteChannel src, long startPos, long count)
      Transfers up to count bytes from the given channel to this file starting at position pos.
      long transferTo​(long pos, long count, java.nio.channels.WritableByteChannel dest)
      Transfers up to count bytes to the given channel starting at position pos in this file.
      boolean truncate​(long size)
      Truncates this file to the given size.
      (package private) void truncateBlocks​(int count)
      Truncates the blocks of this file to the given block count.
      int write​(long pos, byte b)
      Writes the given byte to this file at position pos.
      int write​(long pos, byte[] b, int off, int len)
      Writes len bytes starting at offset off in the given byte array to this file starting at position pos.
      long write​(long pos, java.lang.Iterable<java.nio.ByteBuffer> bufs)
      Writes all available bytes from each buffer in bufs, in order, to this file starting at position pos.
      int write​(long pos, java.nio.ByteBuffer buf)
      Writes all available bytes from buffer buf to this file starting at position pos.
      java.util.concurrent.locks.Lock writeLock()
      Returns the write lock for this file.
      private static int zero​(byte[] block, int offset, int len)
      Zeroes len bytes in the given block starting at the given offset.
      • Methods inherited from class java.lang.Object

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

      • lock

        private final java.util.concurrent.locks.ReadWriteLock lock
      • blocks

        private byte[][] blocks
        Block list for the file.
      • blockCount

        private int blockCount
        Block count for the the file, which also acts as the head of the block list.
      • size

        private long size
      • openCount

        private int openCount
      • deleted

        private boolean deleted
    • Constructor Detail

      • RegularFile

        RegularFile​(int id,
                    java.nio.file.attribute.FileTime creationTime,
                    HeapDisk disk,
                    byte[][] blocks,
                    int blockCount,
                    long size)
    • Method Detail

      • create

        public static RegularFile create​(int id,
                                         java.nio.file.attribute.FileTime creationTime,
                                         HeapDisk disk)
        Creates a new regular file with the given ID and using the given disk.
      • readLock

        public java.util.concurrent.locks.Lock readLock()
        Returns the read lock for this file.
      • writeLock

        public java.util.concurrent.locks.Lock writeLock()
        Returns the write lock for this file.
      • expandIfNecessary

        private void expandIfNecessary​(int minBlockCount)
      • blockCount

        int blockCount()
        Returns the number of blocks this file contains.
      • copyBlocksTo

        void copyBlocksTo​(RegularFile target,
                          int count)
        Copies the last count blocks from this file to the end of the given target file.
      • transferBlocksTo

        void transferBlocksTo​(RegularFile target,
                              int count)
        Transfers the last count blocks from this file to the end of the given target file.
      • truncateBlocks

        void truncateBlocks​(int count)
        Truncates the blocks of this file to the given block count.
      • addBlock

        void addBlock​(byte[] block)
        Adds the given block to the end of this file.
      • getBlock

        byte[] getBlock​(int index)
        Gets the block at the given index in this file.
      • sizeWithoutLocking

        public long sizeWithoutLocking()
        Gets the current size of this file in bytes. Does not do locking, so should only be called when holding a lock.
      • size

        public long size()
        Description copied from class: File
        Returns the size, in bytes, of this file's content. Directories and symbolic links have a size of 0.
        Overrides:
        size in class File
      • copyWithoutContent

        RegularFile copyWithoutContent​(int id,
                                       java.nio.file.attribute.FileTime creationTime)
        Description copied from class: File
        Creates a new file of the same type as this file with the given ID and creation time. Does not copy the content of this file unless the cost of copying the content is minimal. This is because this method is called with a hold on the file system's lock.
        Specified by:
        copyWithoutContent in class File
      • copyContentTo

        void copyContentTo​(File file)
                    throws java.io.IOException
        Description copied from class: File
        Copies the content of this file to the given file. The given file must be the same type of file as this file and should have no content.

        This method is used for copying the content of a file after copying the file itself. Does nothing by default.

        Overrides:
        copyContentTo in class File
        Throws:
        java.io.IOException
      • contentLock

        java.util.concurrent.locks.ReadWriteLock contentLock()
        Description copied from class: File
        Returns the read-write lock for this file's content, or null if there is no content lock.
        Overrides:
        contentLock in class File
      • opened

        public void opened()
        Description copied from class: File
        Called when a stream or channel to this file is opened.
        Overrides:
        opened in class File
      • closed

        public void closed()
        Description copied from class: File
        Called when a stream or channel to this file is closed. If there are no more streams or channels open to the file and it has been deleted, its contents may be deleted.
        Overrides:
        closed in class File
      • deleted

        public void deleted()
        Marks this file as deleted. If there are no streams or channels open to the file, its contents are deleted if necessary.
        Overrides:
        deleted in class File
      • deleteContents

        private void deleteContents()
        Deletes the contents of this file. Called when this file has been deleted and all open streams and channels to it have been closed.
      • truncate

        @CanIgnoreReturnValue
        public boolean truncate​(long size)
        Truncates this file to the given size. If the given size is less than the current size of this file, the size of the file is reduced to the given size and any bytes beyond that size are lost. If the given size is greater than the current size of the file, this method does nothing. Returns true if this file was modified by the call (its size changed) and false otherwise.
      • prepareForWrite

        private void prepareForWrite​(long pos,
                                     long len)
                              throws java.io.IOException
        Prepares for a write of len bytes starting at position pos.
        Throws:
        java.io.IOException
      • write

        @CanIgnoreReturnValue
        public int write​(long pos,
                         byte b)
                  throws java.io.IOException
        Writes the given byte to this file at position pos. pos may be greater than the current size of this file, in which case this file is resized and all bytes between the current size and pos are set to 0. Returns the number of bytes written.
        Throws:
        java.io.IOException - if the file needs more blocks but the disk is full
      • write

        @CanIgnoreReturnValue
        public int write​(long pos,
                         byte[] b,
                         int off,
                         int len)
                  throws java.io.IOException
        Writes len bytes starting at offset off in the given byte array to this file starting at position pos. pos may be greater than the current size of this file, in which case this file is resized and all bytes between the current size and pos are set to 0. Returns the number of bytes written.
        Throws:
        java.io.IOException - if the file needs more blocks but the disk is full
      • write

        @CanIgnoreReturnValue
        public int write​(long pos,
                         java.nio.ByteBuffer buf)
                  throws java.io.IOException
        Writes all available bytes from buffer buf to this file starting at position pos. pos may be greater than the current size of this file, in which case this file is resized and all bytes between the current size and pos are set to 0. Returns the number of bytes written.
        Throws:
        java.io.IOException - if the file needs more blocks but the disk is full
      • write

        @CanIgnoreReturnValue
        public long write​(long pos,
                          java.lang.Iterable<java.nio.ByteBuffer> bufs)
                   throws java.io.IOException
        Writes all available bytes from each buffer in bufs, in order, to this file starting at position pos. pos may be greater than the current size of this file, in which case this file is resized and all bytes between the current size and pos are set to 0. Returns the number of bytes written.
        Throws:
        java.io.IOException - if the file needs more blocks but the disk is full
      • transferFrom

        public long transferFrom​(java.nio.channels.ReadableByteChannel src,
                                 long startPos,
                                 long count)
                          throws java.io.IOException
        Transfers up to count bytes from the given channel to this file starting at position pos. Returns the number of bytes transferred. If pos is greater than the current size of this file, then no bytes are transferred.
        Throws:
        java.io.IOException - if the file needs more blocks but the disk is full or if reading from src throws an exception
      • read

        public int read​(long pos)
        Reads the byte at position pos in this file as an unsigned integer in the range 0-255. If pos is greater than or equal to the size of this file, returns -1 instead.
      • read

        public int read​(long pos,
                        byte[] b,
                        int off,
                        int len)
        Reads up to len bytes starting at position pos in this file to the given byte array starting at offset off. Returns the number of bytes actually read or -1 if pos is greater than or equal to the size of this file.
      • read

        public int read​(long pos,
                        java.nio.ByteBuffer buf)
        Reads up to buf.remaining() bytes starting at position pos in this file to the given buffer. Returns the number of bytes read or -1 if pos is greater than or equal to the size of this file.
      • read

        public long read​(long pos,
                         java.lang.Iterable<java.nio.ByteBuffer> bufs)
        Reads up to the total remaining() number of bytes in each of bufs starting at position pos in this file to the given buffers, in order. Returns the number of bytes read or -1 if pos is greater than or equal to the size of this file.
      • transferTo

        public long transferTo​(long pos,
                               long count,
                               java.nio.channels.WritableByteChannel dest)
                        throws java.io.IOException
        Transfers up to count bytes to the given channel starting at position pos in this file. Returns the number of bytes transferred, possibly 0. Note that unlike all other read methods in this class, this method does not return -1 if pos is greater than or equal to the current size. This for consistency with FileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel), which this method is primarily intended as an implementation of.
        Throws:
        java.io.IOException
      • blockForWrite

        private byte[] blockForWrite​(int index)
                              throws java.io.IOException
        Gets the block at the given index, expanding to create the block if necessary.
        Throws:
        java.io.IOException
      • blockIndex

        private int blockIndex​(long position)
      • offsetInBlock

        private int offsetInBlock​(long position)
      • length

        private int length​(long max)
      • length

        private int length​(int off,
                           long max)
      • bytesToRead

        private long bytesToRead​(long pos,
                                 long max)
        Returns the number of bytes that can be read starting at position pos (up to a maximum of max) or -1 if pos is greater than or equal to the current size.
      • zero

        private static int zero​(byte[] block,
                                int offset,
                                int len)
        Zeroes len bytes in the given block starting at the given offset. Returns len.
      • put

        private static int put​(byte[] block,
                               int offset,
                               byte[] b,
                               int off,
                               int len)
        Puts the given slice of the given array at the given offset in the given block.
      • put

        private static void put​(byte[] block,
                                int offset,
                                java.nio.ByteBuffer buf)
        Puts the contents of the given byte buffer at the given offset in the given block.
      • get

        private static int get​(byte[] block,
                               int offset,
                               byte[] b,
                               int off,
                               int len)
        Reads len bytes starting at the given offset in the given block into the given slice of the given byte array.
      • get

        private static int get​(byte[] block,
                               int offset,
                               java.nio.ByteBuffer buf,
                               int len)
        Reads len bytes starting at the given offset in the given block into the given byte buffer.