Package com.google.common.jimfs
Class RegularFile
java.lang.Object
com.google.common.jimfs.File
com.google.common.jimfs.RegularFile
A mutable, resizable store for bytes. Bytes are stored in fixed-sized byte arrays (blocks)
allocated by a
HeapDisk
.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate 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
ConstructorsConstructorDescriptionRegularFile
(int id, FileTime creationTime, HeapDisk disk, byte[][] blocks, int blockCount, long size) -
Method Summary
Modifier and TypeMethodDescription(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 positionpos
(up to a maximum ofmax
) or -1 ifpos
is greater than or equal to the current size.void
closed()
Called when a stream or channel to this file is closed.(package private) ReadWriteLock
Returns the read-write lock for this file's content, ornull
if there is no content lock.(package private) void
copyBlocksTo
(RegularFile target, int count) Copies the lastcount
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, FileTime creationTime) Creates a new file of the same type as this file with the given ID and creation time.static RegularFile
Creates a new regular file with the given ID and using the given disk.private void
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, 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, 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 positionpos
in this file as an unsigned integer in the range 0-255.int
read
(long pos, byte[] b, int off, int len) Reads up tolen
bytes starting at positionpos
in this file to the given byte array starting at offsetoff
.long
read
(long pos, Iterable<ByteBuffer> bufs) Reads up to the totalremaining()
number of bytes in each ofbufs
starting at positionpos
in this file to the given buffers, in order.int
read
(long pos, ByteBuffer buf) Reads up tobuf.remaining()
bytes starting at positionpos
in this file to the given buffer.readLock()
Returns the read lock for this file.long
size()
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 lastcount
blocks from this file to the end of the given target file.long
transferFrom
(ReadableByteChannel src, long startPos, long count) Transfers up tocount
bytes from the given channel to this file starting at positionpos
.long
transferTo
(long pos, long count, WritableByteChannel dest) Transfers up tocount
bytes to the given channel starting at positionpos
in this file.boolean
truncate
(long size) Truncates this file to the givensize
.(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 positionpos
.int
write
(long pos, byte[] b, int off, int len) Writeslen
bytes starting at offsetoff
in the given byte array to this file starting at positionpos
.long
write
(long pos, Iterable<ByteBuffer> bufs) Writes all available bytes from each buffer inbufs
, in order, to this file starting at positionpos
.int
write
(long pos, ByteBuffer buf) Writes all available bytes from bufferbuf
to this file starting at positionpos
.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 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 Details
-
lock
-
disk
-
blocks
private byte[][] blocksBlock list for the file. -
blockCount
private int blockCountBlock 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
-
-
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 lastcount
blocks from this file to the end of the given target file. -
transferBlocksTo
Transfers the lastcount
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. -
copyWithoutContent
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 classFile
-
copyContentTo
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 classFile
- Throws:
IOException
-
contentLock
ReadWriteLock contentLock()Description copied from class:File
Returns the read-write lock for this file's content, ornull
if there is no content lock.- Overrides:
contentLock
in classFile
-
opened
public void opened()Description copied from class:File
Called when a stream or channel to this file is opened. -
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. -
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. Returnstrue
if this file was modified by the call (its size changed) andfalse
otherwise. -
prepareForWrite
Prepares for a write of len bytes starting at position pos.- Throws:
IOException
-
write
Writes the given byte to this file at positionpos
.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 andpos
are set to 0. Returns the number of bytes written.- Throws:
IOException
- if the file needs more blocks but the disk is full
-
write
Writeslen
bytes starting at offsetoff
in the given byte array to this file starting at positionpos
.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 andpos
are 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 bufferbuf
to this file starting at positionpos
.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 andpos
are 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
.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 andpos
are 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 tocount
bytes from the given channel to this file starting at positionpos
. Returns the number of bytes transferred. Ifpos
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 positionpos
in this file as an unsigned integer in the range 0-255. Ifpos
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 tolen
bytes starting at positionpos
in this file to the given byte array starting at offsetoff
. Returns the number of bytes actually read or -1 ifpos
is greater than or equal to the size of this file. -
read
Reads up tobuf.remaining()
bytes starting at positionpos
in this file to the given buffer. Returns the number of bytes read or -1 ifpos
is greater than or equal to the size of this file. -
read
Reads up to the totalremaining()
number of bytes in each ofbufs
starting at positionpos
in this file to the given buffers, in order. Returns the number of bytes read or -1 ifpos
is greater than or equal to the size of this file. -
transferTo
Transfers up tocount
bytes to the given channel starting at positionpos
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 ifpos
is 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:
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 ifpos
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
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.
-