Top Description Fields Constructors Methods
sun.nio.ch

pack-priv abstract Class AsynchronousFileChannelImpl

extends AsynchronousFileChannel
Class Inheritance
Known Direct Subclasses
sun.nio.ch.SimpleAsynchronousFileChannelImpl
Imports
java.nio.ByteBuffer, java.nio.channels.*, java.util.concurrent.ExecutorService, .Future, java.util.concurrent.locks.*, java.io.FileDescriptor, .IOException

Base implementation of AsynchronousFileChannel.

Field Summary

Modifier and TypeField and Description
protected volatile boolean
protected final ReadWriteLock
protected final ExecutorService
protected final FileDescriptor
private volatile FileLockTable
protected final boolean
protected final boolean

Constructor Summary

AccessConstructor and Description
protected
AsynchronousFileChannelImpl(FileDescriptor fdObj, boolean reading, boolean writing, ExecutorService executor)

Method Summary

Modifier and TypeMethod and Description
protected final FileLockImpl
addToFileLockTable(long position, long size, boolean shared)

Adds region to lock table

protected final void
begin()

Marks the beginning of an I/O operation.

protected final void
end()

Marks the end of an I/O operation.

protected final void
end(boolean completed)

Marks end of I/O operation

pack-priv final void
pack-priv final ExecutorService
pack-priv abstract <A> Future<FileLock>
implLock(long position, long size, boolean shared, A attachment, CompletionHandler<FileLock, ? super A> handler)

pack-priv abstract <A> Future<Integer>
implRead(ByteBuffer dst, long position, A attachment, CompletionHandler<Integer, ? super A> handler)

protected abstract void
implRelease(FileLockImpl fli)

Releases the given file lock.

pack-priv abstract <A> Future<Integer>
implWrite(ByteBuffer src, long position, A attachment, CompletionHandler<Integer, ? super A> handler)

pack-priv final void
public final boolean
isOpen()

Implements java.nio.channels.Channel.isOpen.

Tells whether or not this channel is open.

public final Future<FileLock>
lock(long
The position at which the locked region is to start; must be non-negative
position
,
long
The size of the locked region; must be non-negative, and the sum position + size must be non-negative. A value of zero means to lock all bytes from the specified starting position to the end of the file, regardless of whether the file is subsequently extended or truncated
size
,
boolean
true to request a shared lock, in which case this channel must be open for reading (and possibly writing); false to request an exclusive lock, in which case this channel must be open for writing (and possibly reading)
shared
)

Implements abstract java.nio.channels.AsynchronousFileChannel.lock.

Acquires a lock on the given region of this channel's file.

public final <A> void
lock(long
The position at which the locked region is to start; must be non-negative
position
,
long
The size of the locked region; must be non-negative, and the sum position + size must be non-negative. A value of zero means to lock all bytes from the specified starting position to the end of the file, regardless of whether the file is subsequently extended or truncated
size
,
boolean
true to request a shared lock, in which case this channel must be open for reading (and possibly writing); false to request an exclusive lock, in which case this channel must be open for writing (and possibly reading)
shared
,
A
The object to attach to the I/O operation; can be null
attachment
,
CompletionHandler<FileLock, ? super A>
The handler for consuming the result
handler
)

Implements abstract java.nio.channels.AsynchronousFileChannel.lock.

Acquires a lock on the given region of this channel's file.

public final Future<Integer>
read(ByteBuffer
The buffer into which bytes are to be transferred
dst
,
long
The file position at which the transfer is to begin; must be non-negative
position
)

Implements abstract java.nio.channels.AsynchronousFileChannel.read.

Reads a sequence of bytes from this channel into the given buffer, starting at the given file position.

public final <A> void
read(ByteBuffer
The buffer into which bytes are to be transferred
dst
,
long
The file position at which the transfer is to begin; must be non-negative
position
,
A
The object to attach to the I/O operation; can be null
attachment
,
CompletionHandler<Integer, ? super A>
The handler for consuming the result
handler
)

Implements abstract java.nio.channels.AsynchronousFileChannel.read.

Reads a sequence of bytes from this channel into the given buffer, starting at the given file position.

pack-priv final void
release(FileLockImpl fli)

Invoked by FileLockImpl to release the given file lock and remove it from the lock table.

protected final void
public final Future<Integer>
write(ByteBuffer
The buffer from which bytes are to be transferred
src
,
long
The file position at which the transfer is to begin; must be non-negative
position
)

Implements abstract java.nio.channels.AsynchronousFileChannel.write.

Writes a sequence of bytes to this channel from the given buffer, starting at the given file position.

public final <A> void
write(ByteBuffer
The buffer from which bytes are to be transferred
src
,
long
The file position at which the transfer is to begin; must be non-negative
position
,
A
The object to attach to the I/O operation; can be null
attachment
,
CompletionHandler<Integer, ? super A>
The handler for consuming the result
handler
)

Implements abstract java.nio.channels.AsynchronousFileChannel.write.

Writes a sequence of bytes to this channel from the given buffer, starting at the given file position.

Inherited from java.nio.channels.AsynchronousFileChannel:
forcelocklockopenopensizetruncatetryLocktryLock

Field Detail

closedback to summary
protected volatile boolean closed
closeLockback to summary
protected final ReadWriteLock closeLock
executorback to summary
protected final ExecutorService executor
fdObjback to summary
protected final FileDescriptor fdObj
fileLockTableback to summary
private volatile FileLockTable fileLockTable
readingback to summary
protected final boolean reading
writingback to summary
protected final boolean writing

Constructor Detail

AsynchronousFileChannelImplback to summary
protected AsynchronousFileChannelImpl(FileDescriptor fdObj, boolean reading, boolean writing, ExecutorService executor)

Method Detail

addToFileLockTableback to summary
protected final FileLockImpl addToFileLockTable(long position, long size, boolean shared)

Adds region to lock table

beginback to summary
protected final void begin() throws IOException

Marks the beginning of an I/O operation.

Exceptions
ClosedChannelException:
If channel is closed
endback to summary
protected final void end()

Marks the end of an I/O operation.

endback to summary
protected final void end(boolean completed) throws IOException

Marks end of I/O operation

ensureFileLockTableInitializedback to summary
pack-priv final void ensureFileLockTableInitialized() throws IOException
executorback to summary
pack-priv final ExecutorService executor()
implLockback to summary
pack-priv abstract <A> Future<FileLock> implLock(long position, long size, boolean shared, A attachment, CompletionHandler<FileLock, ? super A> handler)
implReadback to summary
pack-priv abstract <A> Future<Integer> implRead(ByteBuffer dst, long position, A attachment, CompletionHandler<Integer, ? super A> handler)
implReleaseback to summary
protected abstract void implRelease(FileLockImpl fli) throws IOException

Releases the given file lock.

implWriteback to summary
pack-priv abstract <A> Future<Integer> implWrite(ByteBuffer src, long position, A attachment, CompletionHandler<Integer, ? super A> handler)
invalidateAllLocksback to summary
pack-priv final void invalidateAllLocks() throws IOException
isOpenback to summary
public final boolean isOpen()

Implements java.nio.channels.Channel.isOpen.

Doc from java.nio.channels.Channel.isOpen.

Tells whether or not this channel is open.

Returns:boolean

true if, and only if, this channel is open

Annotations
@Override
lockback to summary
public final Future<FileLock> lock(long position, long size, boolean shared)

Implements abstract java.nio.channels.AsynchronousFileChannel.lock.

Doc from java.nio.channels.AsynchronousFileChannel.lock.

Acquires a lock on the given region of this channel's file.

This method initiates an operation to acquire a lock on the given region of this channel's file. The method behaves in exactly the same manner as the lock(long, long, boolean, Object, CompletionHandler) method except that instead of specifying a completion handler, this method returns a Future representing the pending result. The Future's get method returns the FileLock on successful completion.

Parameters
position:long

The position at which the locked region is to start; must be non-negative

size:long

The size of the locked region; must be non-negative, and the sum position + size must be non-negative. A value of zero means to lock all bytes from the specified starting position to the end of the file, regardless of whether the file is subsequently extended or truncated

shared:boolean

true to request a shared lock, in which case this channel must be open for reading (and possibly writing); false to request an exclusive lock, in which case this channel must be open for writing (and possibly reading)

Returns:Future<FileLock>

a Future object representing the pending result

Annotations
@Override
lockback to summary
public final <A> void lock(long position, long size, boolean shared, A attachment, CompletionHandler<FileLock, ? super A> handler)

Implements abstract java.nio.channels.AsynchronousFileChannel.lock.

Doc from java.nio.channels.AsynchronousFileChannel.lock.

Acquires a lock on the given region of this channel's file.

This method initiates an operation to acquire a lock on the given region of this channel's file. The handler parameter is a completion handler that is invoked when the lock is acquired (or the operation fails). The result passed to the completion handler is the resulting FileLock.

The region specified by the position and size parameters need not be contained within, or even overlap, the actual underlying file. Lock regions are fixed in size; if a locked region initially contains the end of the file and the file grows beyond the region then the new portion of the file will not be covered by the lock. If a file is expected to grow in size and a lock on the entire file is required then a region starting at zero, and no smaller than the expected maximum size of the file, should be locked. The two-argument lock(Object, CompletionHandler) method simply locks a region of size Long#MAX_VALUE. If the position is non-negative and the size is zero, then a lock of size Long.MAX_VALUE - position is returned. If a lock that overlaps the requested region is already held by this Java virtual machine, or this method has been invoked to lock an overlapping region and that operation has not completed, then this method throws OverlappingFileLockException.

Some operating systems do not support a mechanism to acquire a file lock in an asynchronous manner. Consequently an implementation may acquire the file lock in a background thread or from a task executed by a thread in the associated thread pool. If there are many lock operations outstanding then it may consume threads in the Java virtual machine for indefinite periods.

Some operating systems do not support shared locks, in which case a request for a shared lock is automatically converted into a request for an exclusive lock. Whether the newly-acquired lock is shared or exclusive may be tested by invoking the resulting lock object's isShared method.

File locks are held on behalf of the entire Java virtual machine. They are not suitable for controlling access to a file by multiple threads within the same virtual machine.

Parameters
<A>
The type of the attachment
position:long

The position at which the locked region is to start; must be non-negative

size:long

The size of the locked region; must be non-negative, and the sum position + size must be non-negative. A value of zero means to lock all bytes from the specified starting position to the end of the file, regardless of whether the file is subsequently extended or truncated

shared:boolean

true to request a shared lock, in which case this channel must be open for reading (and possibly writing); false to request an exclusive lock, in which case this channel must be open for writing (and possibly reading)

attachment:A

The object to attach to the I/O operation; can be null

handler:CompletionHandler<FileLock, ? super A>

The handler for consuming the result

Annotations
@Override
readback to summary
public final Future<Integer> read(ByteBuffer dst, long position)

Implements abstract java.nio.channels.AsynchronousFileChannel.read.

Doc from java.nio.channels.AsynchronousFileChannel.read.

Reads a sequence of bytes from this channel into the given buffer, starting at the given file position.

This method initiates the reading of a sequence of bytes from this channel into the given buffer, starting at the given file position. This method returns a Future representing the pending result of the operation. The Future's get method returns the number of bytes read or -1 if the given position is greater than or equal to the file's size at the time that the read is attempted.

This method works in the same manner as the AsynchronousByteChannel#read(ByteBuffer) method, except that bytes are read starting at the given file position. If the given file position is greater than the file's size at the time that the read is attempted then no bytes are read.

Parameters
dst:ByteBuffer

The buffer into which bytes are to be transferred

position:long

The file position at which the transfer is to begin; must be non-negative

Returns:Future<Integer>

A Future object representing the pending result

Annotations
@Override
readback to summary
public final <A> void read(ByteBuffer dst, long position, A attachment, CompletionHandler<Integer, ? super A> handler)

Implements abstract java.nio.channels.AsynchronousFileChannel.read.

Doc from java.nio.channels.AsynchronousFileChannel.read.

Reads a sequence of bytes from this channel into the given buffer, starting at the given file position.

This method initiates the reading of a sequence of bytes from this channel into the given buffer, starting at the given file position. The result of the read is the number of bytes read or -1 if the given position is greater than or equal to the file's size at the time that the read is attempted.

This method works in the same manner as the AsynchronousByteChannel#read(ByteBuffer, Object, CompletionHandler) method, except that bytes are read starting at the given file position. If the given file position is greater than the file's size at the time that the read is attempted then no bytes are read.

Parameters
<A>
The type of the attachment
dst:ByteBuffer

The buffer into which bytes are to be transferred

position:long

The file position at which the transfer is to begin; must be non-negative

attachment:A

The object to attach to the I/O operation; can be null

handler:CompletionHandler<Integer, ? super A>

The handler for consuming the result

Annotations
@Override
releaseback to summary
pack-priv final void release(FileLockImpl fli) throws IOException

Invoked by FileLockImpl to release the given file lock and remove it from the lock table.

removeFromFileLockTableback to summary
protected final void removeFromFileLockTable(FileLockImpl fli)
writeback to summary
public final Future<Integer> write(ByteBuffer src, long position)

Implements abstract java.nio.channels.AsynchronousFileChannel.write.

Doc from java.nio.channels.AsynchronousFileChannel.write.

Writes a sequence of bytes to this channel from the given buffer, starting at the given file position.

This method initiates the writing of a sequence of bytes to this channel from the given buffer, starting at the given file position. The method returns a Future representing the pending result of the write operation. The Future's get method returns the number of bytes written.

This method works in the same manner as the AsynchronousByteChannel#write(ByteBuffer) method, except that bytes are written starting at the given file position. If the given position is greater than the file's size, at the time that the write is attempted, then the file will be grown to accommodate the new bytes; the values of any bytes between the previous end-of-file and the newly-written bytes are unspecified.

Parameters
src:ByteBuffer

The buffer from which bytes are to be transferred

position:long

The file position at which the transfer is to begin; must be non-negative

Returns:Future<Integer>

A Future object representing the pending result

Annotations
@Override
writeback to summary
public final <A> void write(ByteBuffer src, long position, A attachment, CompletionHandler<Integer, ? super A> handler)

Implements abstract java.nio.channels.AsynchronousFileChannel.write.

Doc from java.nio.channels.AsynchronousFileChannel.write.

Writes a sequence of bytes to this channel from the given buffer, starting at the given file position.

This method works in the same manner as the AsynchronousByteChannel#write(ByteBuffer, Object, CompletionHandler) method, except that bytes are written starting at the given file position. If the given position is greater than the file's size, at the time that the write is attempted, then the file will be grown to accommodate the new bytes; the values of any bytes between the previous end-of-file and the newly-written bytes are unspecified.

Parameters
<A>
The type of the attachment
src:ByteBuffer

The buffer from which bytes are to be transferred

position:long

The file position at which the transfer is to begin; must be non-negative

attachment:A

The object to attach to the I/O operation; can be null

handler:CompletionHandler<Integer, ? super A>

The handler for consuming the result

Annotations
@Override