Return-Path:
+ * The methods for the byte channel are precisely those defined by readable and
+ * writable byte channels.
+ *
+ * Channels are open upon creation, and can be explicitly closed. Once a channel
+ * is closed it cannot be re-opened, and attempts to perform IO operations on
+ * the closed channel result in a
+ * Particular implementations or sub-interfaces of Channel dictate whther they
+ * are thread-safe or not.
+ *
+ * If an attempt is made to perform an operation on a closed channel then a
+ *
+ * If multiple threads attempts to simultaneously close a channel, then only
+ * one thread will runthe closure code, and others will be blocked until the
+ * first returns.
+ *
+ * When the thread is interrupted by a call to
+ * A FileChannel defines the methods for reading, writing, memory mapping, and
+ * manipulating the logical state of a platform file. This type does not have a
+ * method for opening files, since this behaviour has been delegated to the
+ *
+ * FileChannels created from a FileInputStream, or a RandomAccessFile created in
+ * mode "r", are read-only. FileChannels created from a FileOutputStream are
+ * write-only. FileChannels created from a RandomAccessFile created in mode "rw"
+ * are read/write. FileChannels created from a RandomAccessFile that was opened
+ * in append-mode will also be in append-mode -- meaning that each write will be
+ * preceeded by a seek to the end of file. Some platforms will seek and write
+ * atomically, others will not.
+ *
+ * FileChannels has a virtual pointer into the file which is refered to as a
+ * file position. The position can be manipulated by repositioning
+ * it within the file, and its current position can be queried.
+ *
+ * FileChannels also have an associated size. The size of the file
+ * is the number of bytes that it currently contains. The size can be
+ * manipulated by adding more bytes to the end of the file (which increases the
+ * size) or truncating the file (which decreases the size). The current size can
+ * also be queried.
+ *
+ * FileChannels have operations beyond the simple read, write, and close. They
+ * can also:
+ * ClosedChannelException
+ *
.
+ * ClosedChannelException
will be thrown on that attempt.
+ * interrupt()
+ * it will close the channel, set the interrupt status of the thread to true,
+ * and throw a ClosedByInterruptException
.
+ *
+ */
+public class ClosedByInterruptException extends AsynchronousCloseException {
+
+ static final long serialVersionUID = -4488191543534286750L;
+
+ /**
+ * Default constructor.
+ *
+ */
+ public ClosedByInterruptException() {
+ super();
+ }
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/ClosedChannelException.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/ClosedChannelException.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/ClosedChannelException.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/ClosedChannelException.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,37 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.nio.channels;
+
+
+import java.io.IOException;
+
+/**
+ * This exception is thrown when a channel is closed for the type of operation
+ * attempted.
+ *
+ */
+public class ClosedChannelException extends IOException {
+
+ static final long serialVersionUID = 882777185433553857L;
+
+ /**
+ * Default constructor.
+ */
+ public ClosedChannelException() {
+ super();
+ }
+
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/ClosedSelectorException.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/ClosedSelectorException.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/ClosedSelectorException.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/ClosedSelectorException.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,36 @@
+/* Copyright 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.nio.channels;
+
+
+/**
+ * Thrown when an a selector is closed and an IO operation is attempted.
+ *
+ */
+public class ClosedSelectorException extends IllegalStateException {
+
+ static final long serialVersionUID = 6466297122317847835L;
+
+ /**
+ * Default constructor.
+ *
+ */
+ public ClosedSelectorException() {
+ super();
+ }
+}
+
+
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/ConnectionPendingException.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/ConnectionPendingException.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/ConnectionPendingException.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/ConnectionPendingException.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,36 @@
+/* Copyright 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.nio.channels;
+
+
+/**
+ * Thrown when an attempt is made to connect a SocketChannel that has a
+ * non-blocking connection already underway.
+ *
+ */
+public class ConnectionPendingException extends IllegalStateException {
+
+ static final long serialVersionUID = 2008393366501760879L;
+
+ /**
+ * Default constructor.
+ *
+ */
+ public ConnectionPendingException() {
+ super();
+ }
+
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/FileChannel.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/FileChannel.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/FileChannel.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/FileChannel.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,587 @@
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.nio.channels;
+
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.MappedByteBuffer;
+import java.nio.channels.spi.AbstractInterruptibleChannel;
+
+/**
+ * An abstract channel type for interaction with a platform file.
+ * FileInputStream
, FileOutputStream
, and
+ * RandomAccessFile
types.
+ *
+ *
+ *
+ * FileChannels are thread-safe. Only one operation involving manipulation of + * the file position may be in-flight at once. Subsequent calls to such + * operations will block, and one of those blocked will be freed to continue + * when the first operation has completed. There is no ordered queue or fairness + * applied to the blocked threads. + *
+ *+ * It is undefined whether operations that do not manipulate the file position + * will also block when there are any other operations in-flight. + *
+ *+ * The logical view of the underlying file is consistent across all FileChannels + * and IO streams opened on the same file by the same JVM process. Therefore + * modifications performed via a channel will be visible to the stream, and vice + * versa; including modifications to the file position, content, size, etc. + *
+ * + */ +public abstract class FileChannel extends AbstractInterruptibleChannel + implements GatheringByteChannel, ScatteringByteChannel, ByteChannel { + + /** + * A type of file mapping modes. + * + */ + public static class MapMode { + /** + * Private mapping mode (equivalent to copy on write). + */ + public static final MapMode PRIVATE = new MapMode("PRIVATE"); //$NON-NLS-1$ + + /** + * Read-only mapping mode. + */ + public static final MapMode READ_ONLY = new MapMode("READ_ONLY"); //$NON-NLS-1$ + + /** + * Read-write mapping mode. + */ + public static final MapMode READ_WRITE = new MapMode("READ_WRITE"); //$NON-NLS-1$ + + // The string used to display the mapping mode. + private final String displayName; + + /* + * Private constructor prevents others creating new modes. + */ + private MapMode(String displayName) { + super(); + this.displayName = displayName; + } + + /** + * Answers a string version of the mapping mode useful for debugging + * etc. + * + * @return the mode string. + */ + public String toString() { + return displayName; + } + } + + /** + * Protected default constructor. + */ + protected FileChannel() { + super(); + } + + /** + * Request that all updates to the channel are committed to the storage + * device. + *+ * When this method returns all modifications made to the platform file + * underlying this channel will be committed to a local storage device. If + * the file is not hosted locally, such as a networked file system, then + * applications cannot be certain that the modifications have been + * committed. + *
+ *+ * There are no assurances given that changes made to the file using methods + * defined elsewhere will be committed. For example, changes made via a + * mapped byte buffer may not be committed. + *
+ *
+ * The metadata
parameter indicated whether the update should
+ * include the file's metadata such as last modification time, last access
+ * time, etc. Note that passing true
may invoke an underlying
+ * write to the operating system (if the platform is maintaining metadata
+ * such as last access time), even if the channel is opened read-only.
+ *
+ * @param metadata
+ * true if the file metadata should be flushed in addition to the
+ * file content, and false otherwise.
+ * @throws ClosedChannelException
+ * if the channel is already closed.
+ * @throws IOException
+ * some other problem occured.
+ */
+ public abstract void force(boolean metadata) throws IOException;
+
+ /**
+ * Obtain an exclusive lock on this file.
+ *
+ * This is a convenience method for acquiring a maximum length lock on a + * file. It is equivalent to: + * + *
+ * fileChannel.lock(0L, Long.MAX_VALUE, false) + *+ * + * @return the lock object representing the locked file area. + * @throws ClosedChannelException + * the file channel is closed. + * @throws NonWritableChannelException + * this channel was not opened for writing. + * @throws OverlappingFileLockException + * Either a lock is already held that overlaps this lock + * request, or another thread is waiting to acquire a lock that + * will overlap with this request. + * @throws FileLockInterruptionException + * The calling thread was interrupted while waiting to acquire + * the lock. + * @throws AsynchronousCloseException + * The channel was closed while the calling thread was waiting + * to acquire the lock. + * @throws IOException + * some other problem occured obtaining the requested lock. + */ + public final FileLock lock() throws IOException { + return lock(0L, Long.MAX_VALUE, false); + } + + /** + * Obtain a lock on a specified region of the file. + *
+ * This is the blocking version of lock acquisition, see also the
+ * tryLock()
methods.
+ *
+ * Attempts to acquire an overlapping lock region will fail. The attempt + * will fail if the overlapping lock has already been obtained, or if + * another thread is currently waiting to acquire the overlapping lock. + *
+ *+ * If the request is not for an overlapping lock, the thread calling this + * method will block until the lock is obtained (likely by no contention or + * another process releasing a lock), or this thread being interrupted or + * the channel closed. + *
+ *+ * If the lock is obtained successfully then the FileLock object returned + * represents the lock for subsequent operations on the locked region. + *
+ *
+ * If the thread is interrupted while waiting for the lock, the thread is
+ * set to the interrupted state, and throws a
+ * FileLockInterruptionException
. If the channel is closed
+ * while the thread is waiting to obtain the lock then the thread throws a
+ * AsynchronousCloseException
.
+ *
+ * There is no requirement for the position and size to be within the + * current start and length of the file. + *
+ *+ * Some platforms do not support shared locks, and if a request is made for + * a shared lock on such a platform this method will attempt to acquire an + * exclusive lock instead. It is undefined whether the lock obtained is + * advisory or mandatory. + *
+ * + * @param position + * the starting position for the lock region + * @param size + * the length of the lock, in bytes + * @param shared + * a flag indicating whether an attempt should be made to acquire + * a shared lock. + * @return the file lock object + * @throws IllegalArgumentException + * if the parameters are invalid. + * @throws ClosedChannelException + * if the channel is already closed. + * @throws OverlappingFileLockException + * if the requested region overlaps an existing lock or pending + * lock request. + * @throws NonReadableChannelException + * if the channel is not open in read-mode and shared is true. + * @throws NonWritableChannelException + * if the channel is not open in write mode and shared is false. + * @throws AsynchronousCloseException + * if the channel is closed by another thread while this method + * is in operation. + * @throws FileLockInterruptionException + * if the thread is interrupted while in the state of waiting + * on the desired file lock. + * @throws IOException + * if some other IO problem occurs. + */ + public abstract FileLock lock(long position, long size, boolean shared) + throws IOException; + + /** + * TODO: + * + * @param mode + * @param position + * @param size + * @return the mapped byte buffer. + * @throws IOException + */ + public abstract MappedByteBuffer map(FileChannel.MapMode mode, + long position, long size) throws IOException; + + /** + * Answers the current value of the file position pointer. + * + * @return the current position as a positive integer number of bytes from + * the start of the file. + * @throws ClosedChannelException + * if the channel is already closed. + * @throws IOException + * if some other IO problem occurs. + */ + public abstract long position() throws IOException; + + /** + * Sets the file position pointer to a new value. + *+ * The argument is the number of bytes counted from the start of the file. + * The position cannot be set to a value that is negative. The new position + * can be set beyond the current file size. If set beyond the current file + * size, attempts to read will return end of file, and writes will succeed, + * but fill-in the bytes between the current end of file and the position + * with the required number of (unspecified) byte values. + * + * @param offset + * the new file position, in bytes. + * @return the receiver. + * @throws IllegalArgumentException + * if the new position is negative. + * @throws ClosedChannelException + * if the channel is already closed. + * @throws IOException + * if some other IO problem occurs. + */ + public abstract FileChannel position(long offset) throws IOException; + + /** + * Reads bytes from the channel into the given byte buffer. + *
+ * The bytes are read starting at the current file position, and after some + * number of bytes are read (up to the remaining number of bytes in the + * buffer) the file position is increased by the number of bytes actually + * read. + * + * @see java.nio.channels.ReadableByteChannel#read(java.nio.ByteBuffer) + */ + public abstract int read(ByteBuffer buffer) throws IOException; + + /** + * Reads bytes from the file channel into the given buffer starting from the + * given file position. + *
+ * The bytes are read starting at the given file position (up to the + * remaining number of bytes in the buffer). The number of bytes actually + * read is returned. + *
+ *+ * If the position is beyond the current end of file, then no bytes are + * read. + *
+ *+ * Note that file position is unmodified by this method. + *
+ * + * @param buffer + * the buffer to receive the bytes + * @param position + * the (non-negative) position at which to read the bytes. + * @return the number of bytes actually read. + * @throws IllegalArgumentException + * ifposition
is less than -1
.
+ * @throws ClosedChannelException
+ * if the channel is already closed.
+ * @throws NonReadableChannelException
+ * if the channel was not opened in read-mode.
+ * @throws AsynchronousCloseException
+ * if the channel is closed by another thread while this method
+ * is in operation.
+ * @throws ClosedByInterruptException
+ * if another thread interrupts the calling thread while the
+ * operation is in progress. The calling thread will have the
+ * interrupt state set, and the channel will be closed.
+ * @throws IOException
+ * some other IO error occured.
+ */
+ public abstract int read(ByteBuffer buffer, long position)
+ throws IOException;
+
+ /**
+ * Reads bytes from the channel into all the given byte buffers.
+ * + * The bytes are read starting at the current file position, and after some + * number of bytes are read (up to the remaining number of bytes in all the + * buffers) the file position is increased by the number of bytes actually + * read. + *
+ *+ * This method behaves exactly like: + * + *
+ * read(buffers, 0, buffers.length); + *+ * + * + * + * @see java.nio.channels.ScatteringByteChannel#read(java.nio.ByteBuffer[]) + */ + public final long read(ByteBuffer[] buffers) throws IOException { + return read(buffers, 0, buffers.length); + } + + /** + * Reads bytes from the file channel into a subset of the given byte + * buffers. + * + * @see java.nio.channels.ScatteringByteChannel#read(java.nio.ByteBuffer[], + * int, int) + */ + public abstract long read(ByteBuffer[] buffers, int start, int number) + throws IOException; + + /** + * Answers the size of the file underlying this channel, in bytes. + * + * @return the size of the file in bytes. + * @throws ClosedChannelException + * if the channel is closed. + * @throws IOException + * if a problem occurs getting the size of the file. + */ + public abstract long size() throws IOException; + + /** + * TODO: + * + * @param src + * @param position + * @param count + * @return the number of bytes that were transferred. + * @throws IOException + */ + public abstract long transferFrom(ReadableByteChannel src, long position, + long count) throws IOException; + + /** + * TODO + * + * @param position + * @param count + * @param target + * @return the number of bytes transferred. + * @throws IOException + */ + public abstract long transferTo(long position, long count, + WritableByteChannel target) throws IOException; + + /** + * Truncates the file underlying this channel to a given size. + *
+ * Any bytes beyond the given size are removed from the file. If there are + * no bytes beyond the given size then the file contents are unmodified. + *
+ *+ * If the file position is curently greater than the given size, then it is + * set to be the given size. + *
+ * + * @param size + * the maximum size of the underlying file + * @throws IllegalArgumentException + * the requested size is negative. + * @throws ClosedChannelException + * the channel is closed. + * @throws NonWritableChannelException + * the channel cannot be written. + * @throws IOException + * some other IO problem occured. + * @return this channel + */ + public abstract FileChannel truncate(long size) throws IOException; + + /** + * Attempts to acquire an exclusive lock on this file without blocking. + *+ * This is a convenience method for attempting to acquire a maximum length + * lock on the file. It is equivalent to: + * + *
+ * fileChannel.tryLock(0L, Long.MAX_VALUE, false) + *+ * + * + *
+ * The method returns
+ * null
+ *
+ * @return the file lock object, or null
if the lock would overlap an existing exclusive lock
+ * in another OS process.
+ * @throws ClosedChannelException the file channel is closed.
+ * @throws NonWritableChannelException this channel was not opened for writing.
+ * @throws OverlappingFileLockException Either a lock is already held that overlaps this lock request, or another thread is waiting to acquire a lock that will overlap with this request.
+ * @throws IOException
+ */
+ public final FileLock tryLock() throws IOException {
+ return tryLock(0L, Long.MAX_VALUE, false);
+ }
+
+ /**
+ * TODO
+ *
+ * @param position
+ * @param size
+ * @param shared
+ * @return the file lock.
+ * @throws IOException
+ */
+ public abstract FileLock tryLock(long position, long size, boolean shared)
+ throws IOException;
+
+ /**
+ * Writes bytes from the given byte buffer into the file channel.
+ *
+ * The bytes are written starting at the current file position, and after + * some number of bytes are written (up to the remaining number of bytes in + * the buffer) the file position is increased by the number of bytes + * actually written. + * + * @see java.nio.channels.WritableByteChannel#write(java.nio.ByteBuffer) + */ + public abstract int write(ByteBuffer src) throws IOException; + + /** + * Writes bytes from the given buffer to the file channel starting at the + * given file position. + *
+ * The bytes are written starting at the given file position (up to the + * remaining number of bytes in the buffer). The number of bytes actually + * written is returned. + *
+ *+ * If the position is beyond the current end of file, then the file is first + * extended up to the given position by the required number of unspecified + * byte values. + *
+ *+ * Note that file position is unmodified by this method. + *
+ * + * @param buffer + * the buffer containing the bytes to be written. + * @param position + * the (non-negative) position at which to write the bytes. + * @return the number of bytes actually written. + * @throws IllegalArgumentException + * ifposition
is less than -1
.
+ * @throws ClosedChannelException
+ * if the channel is aleady closed.
+ * @throws NonWritableChannelException
+ * if the channel was not opened in write-mode.
+ * @throws AsynchronousCloseException
+ * if the channel is closed by another thread while this method
+ * is in operation.
+ * @throws ClosedByInterruptException
+ * if another thread interrupts the calling thread while the
+ * operation is in progress. The calling thread will have the
+ * interrupt state set, and the channel will be closed.
+ * @throws IOException
+ * some other IO error occured.
+ */
+ public abstract int write(ByteBuffer buffer, long position)
+ throws IOException;
+
+ /**
+ * Writes bytes from all the given byte buffers into the file channel.
+ * + * The bytes are written starting at the current file position, and after + * some number of bytes are written (up to the remaining number of bytes in + * all the buffers) the file position is increased by the number of bytes + * actually written. + *
+ * This method behaves exactly like: + * + *
+ * write(buffers, 0, buffers.length); + *+ * + * + * + * @see java.nio.channels.GatheringByteChannel#write(java.nio.ByteBuffer[]) + */ + public final long write(ByteBuffer[] buffers) throws IOException { + return write(buffers, 0, buffers.length); + } + + /** + * + * + * @see java.nio.channels.GatheringByteChannel#write(java.nio.ByteBuffer[], + * int, int) + */ + public abstract long write(ByteBuffer[] buffers, int offset, int length) + throws IOException; + +} Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/FileLock.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/FileLock.java?rev=350181&view=auto ============================================================================== --- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/FileLock.java (added) +++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/FileLock.java Wed Nov 30 21:29:27 2005 @@ -0,0 +1,200 @@ +/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package java.nio.channels; + + +import java.io.IOException; + +/** + * A
FileLock
represents a locked region of a file.
+ * + * Locks have certain properties that enable collaborating processes to avoid + * the lost update problem, or reading inconsistent data. + *
+ *
+ * logically, a file lock can be 'exclusive' or 'shared'. Multiple processes can
+ * hold shared locks on the same region of a file, but only a single process can
+ * hold an exclusive lock on a given region of a file and no other process can
+ * simultaneously hold a shared lock overlapping the exclusive lock. An
+ * application can determine whether a FileLock is shared or exclusive via the
+ * isShared()
API.
+ *
+ * Locks held by a particular process cannot overlap one another. Applications
+ * can determine whether a proposed lock will overlap by using the
+ * overlaps(long, long)
+ *
+ * Locks are shared amongst all threads in the acquiring process, and are therefore unsuitable for + * intra-process synchronization.
+ *
+ * Once a lock is acquired it is immutable in all its state except isValid()
. The lock
+ * will initially be valid, but may be rendered invalid by explicit removal of the lock, using
+ * release()
, or implictly by closing the channel or exiting the process (terminating the JVM).
+ * Platform dependencies
+ *+ * Locks are intended to be true platform operating system file locks, and therefore locks held by the + * JVM process will be visible to other OS processes.
+ *+ * The characteristics of the underlying OS locks will show through in the Java implementation. For + * example, some platforms' locks are 'mandatory' -- meaning the operating system enforces the locks + * on processes that attempt to access locked regions of file; whereas other platforms' locks are + * only 'advisory' -- meaning that processes are required to collaborate on ensuring locks are acquired + * and there is a potential for processes not to play well. The only safe answer is to assume that + * the platform is adopting advisory locks an always acquire shared locks when reading a region of file.
+ *+ * On some platforms the presence of a lock will prevent the file being memory mapped. On some platforms + * closing a channel on a given file handle will release all the locks held on that file -- even if there + * are other channels open on the same file (their locks will be released). The safe option here is to + * ensure that you only acquire locks on a single channel for a particular file, and that becomes the + * synchronization point.
+ *+ * Further care should be exercised when locking files maintained on network file systems since they often + * have further limitations.
+ * + */ +public abstract class FileLock { + + // The underlying file channel. + private final FileChannel channel; + + // The lock starting position. + private final long position; + + // The lock length in bytes + private final long size; + + // If true then shared, if false then exclusive + private final boolean shared; + + /** + * Constructor for a new file lock instance for a given channel. The + * constructor enforces the starting position, stretch, and shared status of + * the lock. + * + * @param channel + * underlying file channel that holds the lock. + * @param position + * starting point for the lock. + * @param size + * length of lock in number of bytes. + * @param shared + * shared status of lock (true is shared, false is exclusive). + */ + protected FileLock(FileChannel channel, long position, long size, + boolean shared) { + super(); + + this.channel = channel; + this.position = position; + this.size = size; + this.shared = shared; + } + + /** + * Answers the lock's FileChannel. + * + * @return the channel. + */ + public final FileChannel channel() { + return channel; + } + + /** + * Answers the lock's starting position in the file. + * + * @return the lock position. + */ + public final long position() { + return position; + } + + /** + * Answers the length of the file lock in bytes. + * + * @return the size of file lock in bytes. + */ + public final long size() { + return size; + } + + /** + * Answers true if the file lock is shared with other proceses and false if + * it is not. + * + * @return true if the lock is a shared lock, and false if it is exclusive. + */ + public final boolean isShared() { + return shared; + } + + /** + * Answers true if the receiver's lock region overlapps the region described + * in the parameter list,and answers false otherwise. + * + * @param start + * the starting position for the comparative lock. + * @param length + * the length of the comparative lock. + * @return true if there is an overlap, and false otherwise. + */ + public final boolean overlaps(long start, long length) { + long myEnd = position + size; + long otherEnd = start + length; + return ((start >= position) && (start <= myEnd)) + || ((otherEnd >= position) && (otherEnd <= myEnd)); + } + + /** + * Answers whether the receiver is a valid file lock or not. The lock is + * valid unless the underlying channel has been closed or it has been + * explicitly released. + * + * @return true if the lock is valid, and false otherwise. + */ + public abstract boolean isValid(); + + /** + * Releases this particular lock on the file. If the lock is invalid then + * this method has no effect. Once released the lock becomes invalid. + * + * @throws ClosedChannelException + * if the channel is already closed when an attempt to release + * the lock is made. + * @throws IOException + * some other IO exception occurred. + */ + public abstract void release() throws IOException; + + /** + * Answers a string that shows the details of the lock suitable for display + * to an end user. + * + * @return the display string. + */ + public final String toString() { + StringBuffer buffer = new StringBuffer(64); // Guess length of string + buffer.append("FileLock: [position="); //$NON-NLS-1$ + buffer.append(position); + buffer.append(", size="); //$NON-NLS-1$ + buffer.append(size); + buffer.append(", shared="); //$NON-NLS-1$ + buffer.append(Boolean.toString(shared)); + buffer.append("]"); //$NON-NLS-1$ + return buffer.toString(); + } +} Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/FileLockInterruptionException.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/FileLockInterruptionException.java?rev=350181&view=auto ============================================================================== --- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/FileLockInterruptionException.java (added) +++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/FileLockInterruptionException.java Wed Nov 30 21:29:27 2005 @@ -0,0 +1,38 @@ +/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package java.nio.channels; + + +import java.io.IOException; + +/** + * Thrown when thread was interrupted while waiting to acquire a file lock. + *+ * Note that the thread will also be in the 'interrupted' state. + *
+ * + */ +public class FileLockInterruptionException extends IOException { + + static final long serialVersionUID = 7104080643653532383L; + + /** + * Default constructor. + */ + public FileLockInterruptionException() { + super(); + } +} Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/GatheringByteChannel.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/GatheringByteChannel.java?rev=350181&view=auto ============================================================================== --- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/GatheringByteChannel.java (added) +++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/GatheringByteChannel.java Wed Nov 30 21:29:27 2005 @@ -0,0 +1,103 @@ +/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package java.nio.channels; + + +import java.io.IOException; +import java.nio.ByteBuffer; + +/** + * The interface to channels that can write a set of buffers in a single + * operation. + *
+ * The corresponding interface for reads is called
+ * ScatteringByteChannel
.
+ *
+ */
+public interface GatheringByteChannel extends WritableByteChannel {
+
+ /**
+ * Writes bytes from all the given buffers to the channel.
+ *
+ * This method is equivalent to: + * + *
+ * write(buffers, 0, buffers.length); + *+ * + * + * + * @param buffers + * the buffers containing bytes to be written. + * @return the number of bytes actually written. + * @throws ClosedChannelException + * if the channel is closed. + * @throws NonWritableChannelException + * if the channel is open, but not in a mode that permits + * writing. + * @throws ClosedByInterruptException + * if the thread is interrupted in its IO operation by another + * thread closing the channel. + * @throws AsynchronousCloseException + * if the write is interrupted by another thread sending an + * explicit interrupt. + * @throws IOException + * if some other type of exception occurs. Details are in the + * message. + */ + public long write(ByteBuffer[] buffers) throws IOException; + + /** + * Writes a subset of the given bytes from the buffers to the channel. + *
+ * This method attempts to write all of the remaining()
bytes
+ * from length
byte buffers, in order, starting at
+ * buffers[offset]
. The number of bytes actually written is
+ * returned.
+ *
+ * If a write operation is in progress, subsequent threads will block until + * the write is completed, and will then contend for the ability to write. + *
+ * + * @param buffers + * the array of byte buffers containing the source of remaining + * bytes that will be attempted to be written. + * @param offset + * the index of the first buffer to write. + * @param length + * the number of buffers to write. + * @return the number of bytes actually written. + * @throws IndexOutOfBoundsException + * if offset < 0 or > buffers.length; or length < 0 or > + * buffers.length - offset. + * @throws NonWritableChannelException + * if the channel was not opened for writing. + * @throws ClosedChannelException + * the channel is currently closed. + * @throws AsynchronousCloseException + * the channel was closed by another thread while the write was + * underway. + * @throws ClosedByInterruptException + * the thread was interrupted by another thread while the write + * was underway. + * @throws IOException + * if some other type of exception occurs. Details are in the + * message. + */ + public long write(ByteBuffer[] buffers, int offset, int length) + throws IOException; +} Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/IllegalBlockingModeException.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/IllegalBlockingModeException.java?rev=350181&view=auto ============================================================================== --- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/IllegalBlockingModeException.java (added) +++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/IllegalBlockingModeException.java Wed Nov 30 21:29:27 2005 @@ -0,0 +1,34 @@ +/* Copyright 2005 The Apache Software Foundation or its licensors, as applicable + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package java.nio.channels; + + +/** + * Thrown when when operation that requires a specific blocking mode is invoked + * on a channel that is in a different blocking mode. + * + */ +public class IllegalBlockingModeException extends IllegalStateException { + + static final long serialVersionUID = -3335774961855590474L; + + /** + * Default constructor. + */ + public IllegalBlockingModeException() { + super(); + } +} Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/IllegalSelectorException.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/IllegalSelectorException.java?rev=350181&view=auto ============================================================================== --- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/IllegalSelectorException.java (added) +++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/IllegalSelectorException.java Wed Nov 30 21:29:27 2005 @@ -0,0 +1,36 @@ +/* Copyright 2005 The Apache Software Foundation or its licensors, as applicable + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package java.nio.channels; + + +/** + * Thrown when a call is made to register a channel on a selector that has been + * created by a different provider. + * + */ +public class IllegalSelectorException extends IllegalArgumentException { + + static final long serialVersionUID = -8406323347253320987L; + + /** + * Default constructor. + * + */ + public IllegalSelectorException() { + super(); + } + +} Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/InterruptibleChannel.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/InterruptibleChannel.java?rev=350181&view=auto ============================================================================== --- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/InterruptibleChannel.java (added) +++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/InterruptibleChannel.java Wed Nov 30 21:29:27 2005 @@ -0,0 +1,57 @@ +/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package java.nio.channels; + + +import java.io.IOException; + +/** + * Channels that implement this interface are both asynchronously closeable and + * interruptable. + *
+ * A channel that is asynchronously closeable permits a thread blocked on an IO
+ * operation (the IO thread) to be released by another thread calling the
+ * channel's close()
method. The IO thread will throw an
+ * AsynchronousCloseException
and the channel will be closed.
+ *
+ * A channel that is interruptable permits a thread blocked on an IO operation
+ * (the IO thread) to be interrupted by another thread (by invoking
+ * interrupt()
on the IO thread). When the IO thread is
+ * interrupted it will throw a ClosedByInterruptException
+ * exception, it will have its interrupted status set, and the channel will be
+ * closed. If the IO thread attempts to make an IO call with the interrupt
+ * status set the call will immediately fail with a
+ * ClosedByInterruptException
.
+ *
+ */
+public interface InterruptibleChannel extends Channel {
+
+ /**
+ * Closes an InterruptibleChannel. This method is precisely the same as the
+ * super-interface close()
.
+ *
+ * Any threads that are blocked on IO operations on this channel will be
+ * interrupted with an AsynchronousCloseException
+ *
.
+ *
+ * Reads are synchronous on a ReadableByteChannel, that is, if a read is already + * in progress on the channel then subsequent reads will block until the first + * read completes. It is undefined whether non-read operations will block. + * + */ +public interface ReadableByteChannel extends Channel { + + /** + * Reads bytes from the channel into the given buffer. + *
+ * The maximum number of bytes that will be read is the
+ * remaining()
number of bytes in the buffer when the method
+ * invoked. The bytes will be read into the buffer starting at the buffer's
+ * position
.
+ *
+ * The call may block if other threads are also attempting to read on the + * same channel. + *
+ *
+ * Upon completion, the buffer's position()
is updated to the
+ * end of the bytes that were read. The buffer's limit()
is
+ * unmodified.
+ *
+ * The corresponding interface for writes is called
+ * GatheringByteChannel
.
+ *
+ */
+public interface ScatteringByteChannel extends ReadableByteChannel {
+
+ /**
+ * Reads bytes from the channel into all the given buffers.
+ *
+ * This method is equivalent to: + * + *
+ * read(buffers, 0, buffers.length); + *+ * + * + * + * @param buffers + * the array of byte buffers to receive the bytes being read. + * @return the number of bytes actually read. + * @throws ClosedChannelException + * if the channel is closed. + * @throws NonReadableChannelException + * if the channel is open, but not in a mode that permits + * reading. + * @throws ClosedByInterruptException + * if the thread is interrupted in its IO operation by another + * thread closing the channel. + * @throws AsynchronousCloseException + * if the read is interrupted by another thread sending an + * explicit interrupt. + * @throws IOException + * if some other type of exception occurs. Details are in the + * message. + */ + public long read(ByteBuffer[] buffers) throws IOException; + + /** + * Reads bytes from the channel into a subset of the given buffers. + *
+ * This method attempts to read all of the remaining()
bytes
+ * from length
byte buffers, in order, starting at
+ * buffers[offset]
. The number of bytes actually read is
+ * returned.
+ *
+ * If a read operation is in progress, subsequent threads will block until + * the read is completed, and will then contend for the ability to read. + *
+ * + * @param buffers + * the array of byte buffers into which the bytes will be read. + * @param offset + * the index of the first buffer to read. + * @param length + * the maximum number of buffers to read. + * @return the number of bytes actually read. + * @throws IndexOutOfBoundsException + * if offset < 0 or > buffers.length; or length < 0 or > + * buffers.length - offset. + * @throws NonReadableChannelException + * if the channel was not opened for reading. + * @throws ClosedChannelException + * the channel is currently closed. + * @throws AsynchronousCloseException + * the channel was closed by another thread while the write was + * underway. + * @throws ClosedByInterruptException + * the thread was interrupted by another thread while the write + * was underway. + * @throws IOException + * if some other type of exception occurs. Details are in the + * message. + */ + public long read(ByteBuffer[] buffers, int offset, int length) + throws IOException; + +} Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/SelectableChannel.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/SelectableChannel.java?rev=350181&view=auto ============================================================================== --- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/SelectableChannel.java (added) +++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/SelectableChannel.java Wed Nov 30 21:29:27 2005 @@ -0,0 +1,57 @@ +/* Copyright 2005 The Apache Software Foundation or its licensors, as applicable + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package java.nio.channels; + + +import java.io.IOException; +import java.nio.channels.spi.AbstractInterruptibleChannel; +import java.nio.channels.spi.SelectorProvider; + +/** + * TODO Type description + * + */ +public abstract class SelectableChannel extends AbstractInterruptibleChannel + implements Channel { + + protected SelectableChannel() { + super(); + } + + public abstract Object blockingLock(); + + public abstract SelectableChannel configureBlocking(boolean block) + throws IOException; + + public abstract boolean isBlocking(); + + public abstract boolean isRegistered(); + + public abstract SelectionKey keyFor(Selector sel); + + public abstract SelectorProvider provider(); + + public final SelectionKey register(Selector selector, int operations) + throws ClosedChannelException { + return register(selector, operations, null); + } + + public abstract SelectionKey register(Selector sel, int ops, Object att) + throws ClosedChannelException; + + public abstract int validOps(); + +} Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/SelectionKey.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/SelectionKey.java?rev=350181&view=auto ============================================================================== --- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/SelectionKey.java (added) +++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/nio/src/java/nio/channels/SelectionKey.java Wed Nov 30 21:29:27 2005 @@ -0,0 +1,80 @@ +/* Copyright 2005 The Apache Software Foundation or its licensors, as applicable + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package java.nio.channels; + + +import java.nio.channels.Selector; + +/** + * TODO Type description + * + */ +public abstract class SelectionKey { + + public static final int OP_ACCEPT = 16; + + public static final int OP_CONNECT = 8; + + public static final int OP_READ = 1; + + public static final int OP_WRITE = 4; + + private volatile Object attachment = null; + + protected SelectionKey() { + super(); + } + + public final Object attach(Object anObject) { + Object oldAttachment = attachment; + attachment = anObject; + return oldAttachment; + } + + public final Object attachment() { + return attachment; + } + + public abstract void cancel(); + + public abstract SelectableChannel channel(); + + public abstract int interestOps(); + + public abstract SelectionKey interestOps(int operations); + + public final boolean isAcceptable() { + return (readyOps() & OP_ACCEPT) == OP_ACCEPT; + } + + public final boolean isConnectable() { + return (readyOps() & OP_CONNECT) == OP_CONNECT; + } + + public final boolean isReadable() { + return (readyOps() & OP_READ) == OP_READ; + } + + public abstract boolean isValid(); + + public final boolean isWritable() { + return (readyOps() & OP_WRITE) == OP_WRITE; + } + + public abstract int readyOps(); + + public abstract Selector selector(); +}