Class RegularFile

java.lang.Object
com.google.common.jimfs.File
com.google.common.jimfs.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
    Block count for the the file, which also acts as the head of the block list.
    private byte[][]
    Block list for the file.
    private boolean
     
    private final HeapDisk
     
    private final ReadWriteLock
     
    private int
     
    private long
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    RegularFile(int id, FileTime creationTime, HeapDisk disk, byte[][] blocks, int blockCount, long size)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) void
    addBlock(byte[] block)
    Adds the given block to the end of this file.
    (package private) int
    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
    Called when a stream or channel to this file is closed.
    (package private) ReadWriteLock
    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
    Copies the content of this file to the given file.
    (package private) RegularFile
    copyWithoutContent(int id, FileTime creationTime)
    Creates a new file of the same type as this file with the given ID and creation time.
    create(int id, FileTime creationTime, HeapDisk disk)
    Creates a new regular file with the given ID and using the given disk.
    private void
    Deletes the contents of this file.
    void
    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, 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
    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, 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, Iterable<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, ByteBuffer buf)
    Reads up to buf.remaining() bytes starting at position pos in this file to the given buffer.
    Returns the read lock for this file.
    long
    Returns the size, in bytes, of this file's content.
    long
    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(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, 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, Iterable<ByteBuffer> bufs)
    Writes all available bytes from each buffer in bufs, in order, to this file starting at position pos.
    int
    write(long pos, ByteBuffer buf)
    Writes all available bytes from buffer buf to this file starting at position pos.
    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 Details

    • lock

      private final ReadWriteLock lock
    • disk

      private final HeapDisk disk
    • 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 Details

    • RegularFile

      RegularFile(int id, FileTime creationTime, HeapDisk disk, byte[][] blocks, int blockCount, long size)
  • Method Details

    • create

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

      public Lock readLock()
      Returns the read lock for this file.
    • writeLock

      public 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, 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 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:
      IOException
    • contentLock

      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 IOException
      Prepares for a write of len bytes starting at position pos.
      Throws:
      IOException
    • write

      @CanIgnoreReturnValue public int write(long pos, byte b) throws 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:
      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 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:
      IOException - if the file needs more blocks but the disk is full
    • write

      @CanIgnoreReturnValue public int write(long pos, ByteBuffer buf) throws 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:
      IOException - if the file needs more blocks but the disk is full
    • write

      @CanIgnoreReturnValue public long write(long pos, Iterable<ByteBuffer> bufs) throws 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:
      IOException - if the file needs more blocks but the disk is full
    • transferFrom

      public long transferFrom(ReadableByteChannel src, long startPos, long count) throws 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:
      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, 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, Iterable<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, WritableByteChannel dest) throws 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:
      IOException
    • blockForWrite

      private byte[] blockForWrite(int index) throws IOException
      Gets the block at the given index, expanding to create the block if necessary.
      Throws:
      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, 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, ByteBuffer buf, int len)
      Reads len bytes starting at the given offset in the given block into the given byte buffer.