Class RegularFile
java.lang.Object
com.google.common.jimfs.File
com.google.common.jimfs.RegularFile
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate intBlock count for the file, which also acts as the head of the block list.private byte[][]Block list for the file.private booleanprivate final HeapDiskprivate final ReadWriteLockprivate intprivate long -
Constructor Summary
ConstructorsConstructorDescriptionRegularFile(int id, FileTime creationTime, HeapDisk disk, byte[][] blocks, int blockCount, long size) -
Method Summary
Modifier and TypeMethodDescription(package private) voidaddBlock(byte[] block) Adds the given block to the end of this file.(package private) intReturns 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) ReadWriteLockReturns 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, FileTime creationTime) Creates a new file of the same type as this file with the given ID and creation time.static RegularFileCreates a new regular file with the given ID and using the given disk.private voidDeletes 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, 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, 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, Iterable<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, ByteBuffer buf) Reads up tobuf.remaining()bytes starting at positionposin this file to the given buffer.readLock()Returns the read lock for this file.longsize()Returns the size, in bytes, of this file's content.longGets 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(ReadableByteChannel src, long startPos, long count) Transfers up tocountbytes from the given channel to this file starting at positionpos.longtransferTo(long pos, long count, 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, Iterable<ByteBuffer> bufs) Writes all available bytes from each buffer inbufs, in order, to this file starting at positionpos.intwrite(long pos, ByteBuffer buf) Writes all available bytes from bufferbufto this file starting at positionpos.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 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 Details
-
lock
-
disk
-
blocks
private byte[][] blocksBlock list for the file. -
blockCount
private int blockCountBlock 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 Details
-
RegularFile
-
-
Method Details
-
create
Creates a new regular file with the given ID and using the given disk. -
readLock
Returns the read lock for this file. -
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
Copies the lastcountblocks from this file to the end of the given target file. -
transferBlocksTo
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
-
copyWithoutContent
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
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:
IOException
-
contentLock
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
-
closed
-
deleted
-
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
Prepares for a write of len bytes starting at position pos.- Throws:
IOException
-
write
Writes 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:
IOException- if the file needs more blocks but the disk is full
-
write
Writeslenbytes 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:
IOException- if the file needs more blocks but the disk is full
-
write
Writes 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:
IOException- if the file needs more blocks but the disk is full
-
write
Writes 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:
IOException- if the file needs more blocks but the disk is full
-
transferFrom
Transfers 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:
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
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
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
Transfers 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, WritableByteChannel), which this method is primarily intended as an implementation of.- Throws:
IOException
-
blockForWrite
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 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
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
Reads len bytes starting at the given offset in the given block into the given byte buffer.
-