Package com.google.common.jimfs
Class JimfsFileChannel
- java.lang.Object
-
- java.nio.channels.spi.AbstractInterruptibleChannel
-
- java.nio.channels.FileChannel
-
- com.google.common.jimfs.JimfsFileChannel
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,java.nio.channels.ByteChannel
,java.nio.channels.Channel
,java.nio.channels.GatheringByteChannel
,java.nio.channels.InterruptibleChannel
,java.nio.channels.ReadableByteChannel
,java.nio.channels.ScatteringByteChannel
,java.nio.channels.SeekableByteChannel
,java.nio.channels.WritableByteChannel
final class JimfsFileChannel extends java.nio.channels.FileChannel
AFileChannel
implementation that reads and writes to aRegularFile
object. The read and write methods and other methods that read or change the position of the channel are locked because theReadableByteChannel
andWritableByteChannel
interfaces specify that the read and write methods block when another thread is currently doing a read or write operation.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
JimfsFileChannel.FakeFileLock
A file lock that does nothing, since only one JVM process has access to this file system.
-
Field Summary
Fields Modifier and Type Field Description private boolean
append
private java.util.Set<java.lang.Thread>
blockingThreads
Set of threads that are currently doing an interruptible blocking operation; that is, doing something that requires acquiring the file's lock.private RegularFile
file
private FileSystemState
fileSystemState
private long
position
private boolean
read
private boolean
write
-
Constructor Summary
Constructors Constructor Description JimfsFileChannel(RegularFile file, java.util.Set<java.nio.file.OpenOption> options, FileSystemState fileSystemState)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.nio.channels.AsynchronousFileChannel
asAsynchronousFileChannel(java.util.concurrent.ExecutorService executor)
Returns anAsynchronousFileChannel
view of this channel using the given executor for asynchronous operations.private boolean
beginBlocking()
Begins a blocking operation, making the operation interruptible.private void
checkLockArguments(long position, long size, boolean shared)
(package private) void
checkOpen()
(package private) void
checkReadable()
(package private) void
checkWritable()
private void
endBlocking(boolean completed)
Ends a blocking operation, throwing an exception if the thread was interrupted while blocking or if the channel was closed from another thread.void
force(boolean metaData)
protected void
implCloseChannel()
java.nio.channels.FileLock
lock(long position, long size, boolean shared)
java.nio.MappedByteBuffer
map(java.nio.channels.FileChannel.MapMode mode, long position, long size)
long
position()
java.nio.channels.FileChannel
position(long newPosition)
int
read(java.nio.ByteBuffer dst)
long
read(java.nio.ByteBuffer[] dsts, int offset, int length)
int
read(java.nio.ByteBuffer dst, long position)
long
size()
long
transferFrom(java.nio.channels.ReadableByteChannel src, long position, long count)
long
transferTo(long position, long count, java.nio.channels.WritableByteChannel target)
java.nio.channels.FileChannel
truncate(long size)
java.nio.channels.FileLock
tryLock(long position, long size, boolean shared)
int
write(java.nio.ByteBuffer src)
long
write(java.nio.ByteBuffer[] srcs, int offset, int length)
int
write(java.nio.ByteBuffer src, long position)
-
Methods inherited from class java.nio.channels.spi.AbstractInterruptibleChannel
begin, close, end, isOpen
-
-
-
-
Field Detail
-
blockingThreads
private final java.util.Set<java.lang.Thread> blockingThreads
Set of threads that are currently doing an interruptible blocking operation; that is, doing something that requires acquiring the file's lock. These threads must be interrupted if the channel is closed by another thread.
-
file
private final RegularFile file
-
fileSystemState
private final FileSystemState fileSystemState
-
read
private final boolean read
-
write
private final boolean write
-
append
private final boolean append
-
position
private long position
-
-
Constructor Detail
-
JimfsFileChannel
public JimfsFileChannel(RegularFile file, java.util.Set<java.nio.file.OpenOption> options, FileSystemState fileSystemState)
-
-
Method Detail
-
asAsynchronousFileChannel
public java.nio.channels.AsynchronousFileChannel asAsynchronousFileChannel(java.util.concurrent.ExecutorService executor)
Returns anAsynchronousFileChannel
view of this channel using the given executor for asynchronous operations.
-
checkReadable
void checkReadable()
-
checkWritable
void checkWritable()
-
checkOpen
void checkOpen() throws java.nio.channels.ClosedChannelException
- Throws:
java.nio.channels.ClosedChannelException
-
beginBlocking
private boolean beginBlocking()
Begins a blocking operation, making the operation interruptible. Returnstrue
if the channel was open and the thread was added as a blocking thread; returnsfalse
if the channel was closed.
-
endBlocking
private void endBlocking(boolean completed) throws java.nio.channels.AsynchronousCloseException
Ends a blocking operation, throwing an exception if the thread was interrupted while blocking or if the channel was closed from another thread.- Throws:
java.nio.channels.AsynchronousCloseException
-
read
public int read(java.nio.ByteBuffer dst) throws java.io.IOException
- Specified by:
read
in interfacejava.nio.channels.ReadableByteChannel
- Specified by:
read
in interfacejava.nio.channels.SeekableByteChannel
- Specified by:
read
in classjava.nio.channels.FileChannel
- Throws:
java.io.IOException
-
read
public long read(java.nio.ByteBuffer[] dsts, int offset, int length) throws java.io.IOException
- Specified by:
read
in interfacejava.nio.channels.ScatteringByteChannel
- Specified by:
read
in classjava.nio.channels.FileChannel
- Throws:
java.io.IOException
-
read
public int read(java.nio.ByteBuffer dst, long position) throws java.io.IOException
- Specified by:
read
in classjava.nio.channels.FileChannel
- Throws:
java.io.IOException
-
write
public int write(java.nio.ByteBuffer src) throws java.io.IOException
- Specified by:
write
in interfacejava.nio.channels.SeekableByteChannel
- Specified by:
write
in interfacejava.nio.channels.WritableByteChannel
- Specified by:
write
in classjava.nio.channels.FileChannel
- Throws:
java.io.IOException
-
write
public long write(java.nio.ByteBuffer[] srcs, int offset, int length) throws java.io.IOException
- Specified by:
write
in interfacejava.nio.channels.GatheringByteChannel
- Specified by:
write
in classjava.nio.channels.FileChannel
- Throws:
java.io.IOException
-
write
public int write(java.nio.ByteBuffer src, long position) throws java.io.IOException
- Specified by:
write
in classjava.nio.channels.FileChannel
- Throws:
java.io.IOException
-
position
public long position() throws java.io.IOException
- Specified by:
position
in interfacejava.nio.channels.SeekableByteChannel
- Specified by:
position
in classjava.nio.channels.FileChannel
- Throws:
java.io.IOException
-
position
@CanIgnoreReturnValue public java.nio.channels.FileChannel position(long newPosition) throws java.io.IOException
- Specified by:
position
in interfacejava.nio.channels.SeekableByteChannel
- Specified by:
position
in classjava.nio.channels.FileChannel
- Throws:
java.io.IOException
-
size
public long size() throws java.io.IOException
- Specified by:
size
in interfacejava.nio.channels.SeekableByteChannel
- Specified by:
size
in classjava.nio.channels.FileChannel
- Throws:
java.io.IOException
-
truncate
@CanIgnoreReturnValue public java.nio.channels.FileChannel truncate(long size) throws java.io.IOException
- Specified by:
truncate
in interfacejava.nio.channels.SeekableByteChannel
- Specified by:
truncate
in classjava.nio.channels.FileChannel
- Throws:
java.io.IOException
-
force
public void force(boolean metaData) throws java.io.IOException
- Specified by:
force
in classjava.nio.channels.FileChannel
- Throws:
java.io.IOException
-
transferTo
public long transferTo(long position, long count, java.nio.channels.WritableByteChannel target) throws java.io.IOException
- Specified by:
transferTo
in classjava.nio.channels.FileChannel
- Throws:
java.io.IOException
-
transferFrom
public long transferFrom(java.nio.channels.ReadableByteChannel src, long position, long count) throws java.io.IOException
- Specified by:
transferFrom
in classjava.nio.channels.FileChannel
- Throws:
java.io.IOException
-
map
public java.nio.MappedByteBuffer map(java.nio.channels.FileChannel.MapMode mode, long position, long size) throws java.io.IOException
- Specified by:
map
in classjava.nio.channels.FileChannel
- Throws:
java.io.IOException
-
lock
public java.nio.channels.FileLock lock(long position, long size, boolean shared) throws java.io.IOException
- Specified by:
lock
in classjava.nio.channels.FileChannel
- Throws:
java.io.IOException
-
tryLock
public java.nio.channels.FileLock tryLock(long position, long size, boolean shared) throws java.io.IOException
- Specified by:
tryLock
in classjava.nio.channels.FileChannel
- Throws:
java.io.IOException
-
checkLockArguments
private void checkLockArguments(long position, long size, boolean shared) throws java.io.IOException
- Throws:
java.io.IOException
-
implCloseChannel
protected void implCloseChannel()
- Specified by:
implCloseChannel
in classjava.nio.channels.spi.AbstractInterruptibleChannel
-
-