Package com.google.common.jimfs
Class RegularFile
- java.lang.Object
-
- com.google.common.jimfs.File
-
- com.google.common.jimfs.RegularFile
-
-
Field Summary
Fields Modifier and Type Field Description private intblockCountBlock count for the file, which also acts as the head of the block list.private byte[][]blocksBlock list for the file.private booleandeletedprivate HeapDiskdiskprivate java.util.concurrent.locks.ReadWriteLocklockprivate intopenCountprivate longsize
-
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) voidaddBlock(byte[] block)Adds the given block to the end of this file.(package private) intblockCount()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 intblockIndex(long position)private longbytesToRead(long pos, long max)Returns the number of bytes that can be read starting at positionpos(up to a maximum ofmax) or -1 ifposis greater than or equal to the current size.voidclosed()Called when a stream or channel to this file is closed.(package private) java.util.concurrent.locks.ReadWriteLockcontentLock()Returns the read-write lock for this file's content, ornullif there is no content lock.(package private) voidcopyBlocksTo(RegularFile target, int count)Copies the lastcountblocks from this file to the end of the given target file.(package private) voidcopyContentTo(File file)Copies the content of this file to the given file.(package private) RegularFilecopyWithoutContent(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 RegularFilecreate(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 voiddeleteContents()Deletes the contents of this file.voiddeleted()Marks this file as deleted.private voidexpandIfNecessary(int minBlockCount)private static intget(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 intget(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 intlength(int off, long max)private intlength(long max)private intoffsetInBlock(long position)voidopened()Called when a stream or channel to this file is opened.private voidprepareForWrite(long pos, long len)Prepares for a write of len bytes starting at position pos.private static intput(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 voidput(byte[] block, int offset, java.nio.ByteBuffer buf)Puts the contents of the given byte buffer at the given offset in the given block.intread(long pos)Reads the byte at positionposin this file as an unsigned integer in the range 0-255.intread(long pos, byte[] b, int off, int len)Reads up tolenbytes starting at positionposin this file to the given byte array starting at offsetoff.longread(long pos, java.lang.Iterable<java.nio.ByteBuffer> bufs)Reads up to the totalremaining()number of bytes in each ofbufsstarting at positionposin this file to the given buffers, in order.intread(long pos, java.nio.ByteBuffer buf)Reads up tobuf.remaining()bytes starting at positionposin this file to the given buffer.java.util.concurrent.locks.LockreadLock()Returns the read lock for this file.longsize()Returns the size, in bytes, of this file's content.longsizeWithoutLocking()Gets the current size of this file in bytes.(package private) voidtransferBlocksTo(RegularFile target, int count)Transfers the lastcountblocks from this file to the end of the given target file.longtransferFrom(java.nio.channels.ReadableByteChannel src, long startPos, long count)Transfers up tocountbytes from the given channel to this file starting at positionpos.longtransferTo(long pos, long count, java.nio.channels.WritableByteChannel dest)Transfers up tocountbytes to the given channel starting at positionposin this file.booleantruncate(long size)Truncates this file to the givensize.(package private) voidtruncateBlocks(int count)Truncates the blocks of this file to the given block count.intwrite(long pos, byte b)Writes the given byte to this file at positionpos.intwrite(long pos, byte[] b, int off, int len)Writeslenbytes starting at offsetoffin the given byte array to this file starting at positionpos.longwrite(long pos, java.lang.Iterable<java.nio.ByteBuffer> bufs)Writes all available bytes from each buffer inbufs, in order, to this file starting at positionpos.intwrite(long pos, java.nio.ByteBuffer buf)Writes all available bytes from bufferbufto this file starting at positionpos.java.util.concurrent.locks.LockwriteLock()Returns the write lock for this file.private static intzero(byte[] block, int offset, int len)Zeroes len bytes in the given block starting at the given offset.-
Methods inherited from class com.google.common.jimfs.File
copyAttributes, copyBasicAttributes, decrementLinkCount, deleteAttribute, getAttribute, getAttributeKeys, getAttributeNames, getCreationTime, getLastAccessTime, getLastModifiedTime, id, incrementLinkCount, isDirectory, isRegularFile, isRootDirectory, isSymbolicLink, linked, links, setAttribute, setCreationTime, setLastAccessTime, setLastModifiedTime, toString, unlinked
-
-
-
-
Field Detail
-
lock
private final java.util.concurrent.locks.ReadWriteLock lock
-
disk
private final HeapDisk disk
-
blocks
private byte[][] blocks
Block list for the file.
-
blockCount
private int blockCount
Block count for 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 lastcountblocks from this file to the end of the given target file.
-
transferBlocksTo
void transferBlocksTo(RegularFile target, int count)
Transfers the lastcountblocks 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:FileReturns the size, in bytes, of this file's content. Directories and symbolic links have a size of 0.
-
copyWithoutContent
RegularFile copyWithoutContent(int id, java.nio.file.attribute.FileTime creationTime)
Description copied from class:FileCreates 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:
copyWithoutContentin classFile
-
copyContentTo
void copyContentTo(File file) throws java.io.IOException
Description copied from class:FileCopies 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:
copyContentToin classFile- Throws:
java.io.IOException
-
contentLock
java.util.concurrent.locks.ReadWriteLock contentLock()
Description copied from class:FileReturns the read-write lock for this file's content, ornullif there is no content lock.- Overrides:
contentLockin classFile
-
opened
public void opened()
Description copied from class:FileCalled when a stream or channel to this file is opened.
-
closed
public void closed()
Description copied from class:FileCalled 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.
-
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.
-
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 givensize. 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. Returnstrueif this file was modified by the call (its size changed) andfalseotherwise.
-
prepareForWrite
private void prepareForWrite(long pos, long len) throws java.io.IOExceptionPrepares 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.IOExceptionWrites the given byte to this file at positionpos.posmay be greater than the current size of this file, in which case this file is resized and all bytes between the current size andposare 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.IOExceptionWriteslenbytes starting at offsetoffin the given byte array to this file starting at positionpos.posmay be greater than the current size of this file, in which case this file is resized and all bytes between the current size andposare 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.IOExceptionWrites all available bytes from bufferbufto this file starting at positionpos.posmay be greater than the current size of this file, in which case this file is resized and all bytes between the current size andposare 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.IOExceptionWrites all available bytes from each buffer inbufs, in order, to this file starting at positionpos.posmay be greater than the current size of this file, in which case this file is resized and all bytes between the current size andposare 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.IOExceptionTransfers up tocountbytes from the given channel to this file starting at positionpos. Returns the number of bytes transferred. Ifposis 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 positionposin this file as an unsigned integer in the range 0-255. Ifposis 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 tolenbytes starting at positionposin this file to the given byte array starting at offsetoff. Returns the number of bytes actually read or -1 ifposis greater than or equal to the size of this file.
-
read
public int read(long pos, java.nio.ByteBuffer buf)Reads up tobuf.remaining()bytes starting at positionposin this file to the given buffer. Returns the number of bytes read or -1 ifposis 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 totalremaining()number of bytes in each ofbufsstarting at positionposin this file to the given buffers, in order. Returns the number of bytes read or -1 ifposis 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.IOExceptionTransfers up tocountbytes to the given channel starting at positionposin 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 ifposis greater than or equal to the current size. This for consistency withFileChannel.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.IOExceptionGets 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 positionpos(up to a maximum ofmax) or -1 ifposis 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.
-
-