NIO2 API

org.classpath.icedtea.java.nio.channels
Class FileChannel

java.lang.Object
  extended by java.nio.channels.spi.AbstractInterruptibleChannel
      extended by java.nio.channels.FileChannel
          extended by org.classpath.icedtea.java.nio.channels.FileChannel
All Implemented Interfaces:
Closeable, ByteChannel, Channel, GatheringByteChannel, InterruptibleChannel, ReadableByteChannel, ScatteringByteChannel, WritableByteChannel, SeekableByteChannel

public abstract class FileChannel
extends FileChannel
implements SeekableByteChannel

A channel for reading, writing, mapping, and manipulating a file.

A file channel is a SeekableByteChannel that is connected to a file. It has a current position within its file which can be both queried and modified. The file itself contains a variable-length sequence of bytes that can be read and written and whose current size can be queried. The size of the file increases when bytes are written beyond its current size; the size of the file decreases when it is truncated. The file may also have some associated metadata such as access permissions, content type, and last-modification time; this class does not define methods for metadata access.

In addition to the familiar read, write, and close operations of byte channels, this class defines the following file-specific operations:

File channels are safe for use by multiple concurrent threads. The close method may be invoked at any time, as specified by the Channel interface. Only one operation that involves the channel's position or can change its file's size may be in progress at any given time; attempts to initiate a second such operation while the first is still in progress will block until the first operation completes. Other operations, in particular those that take an explicit position, may proceed concurrently; whether they in fact do so is dependent upon the underlying implementation and is therefore unspecified.

The view of a file provided by an instance of this class is guaranteed to be consistent with other views of the same file provided by other instances in the same program. The view provided by an instance of this class may or may not, however, be consistent with the views seen by other concurrently-running programs due to caching performed by the underlying operating system and delays induced by network-filesystem protocols. This is true regardless of the language in which these other programs are written, and whether they are running on the same machine or on some other machine. The exact nature of any such inconsistencies are system-dependent and are therefore unspecified.

A file channel is created by invoking one of the open methods defined by this class. A file channel can also be obtained from an existing FileInputStream, FileOutputStream, or RandomAccessFile object by invoking that object's getChannel method, which returns a file channel that is connected to the same underlying file. Where the file channel is obtained from an existing stream or random access file then the state of the file channel is intimately connected to that of the object whose getChannel method returned the channel. Changing the channel's position, whether explicitly or by reading or writing bytes, will change the file position of the originating object, and vice versa. Changing the file's length via the file channel will change the length seen via the originating object, and vice versa. Changing the file's content by writing bytes will change the content seen by the originating object, and vice versa.

At various points this class specifies that an instance that is "open for reading," "open for writing," or "open for reading and writing" is required. A channel obtained via the getChannel method of a FileInputStream instance will be open for reading. A channel obtained via the getChannel method of a FileOutputStream instance will be open for writing. Finally, a channel obtained via the getChannel method of a RandomAccessFile instance will be open for reading if the instance was created with mode "r" and will be open for reading and writing if the instance was created with mode "rw".

A file channel that is open for writing may be in append mode, for example if it was obtained from a file-output stream that was created by invoking the FileOutputStream(File,boolean) constructor and passing true for the second parameter. In this mode each invocation of a relative write operation first advances the position to the end of the file and then writes the requested data. Whether the advancement of the position and the writing of the data are done in a single atomic operation is system-dependent and therefore unspecified.

Since:
1.4
See Also:
FileInputStream.getChannel(), FileOutputStream.getChannel(), RandomAccessFile.getChannel()

Nested Class Summary
 
Nested classes/interfaces inherited from class java.nio.channels.FileChannel
FileChannel.MapMode
 
Constructor Summary
protected FileChannel()
          Initializes a new instance of this class.
 
Method Summary
static FileChannel open(Path file, OpenOption... options)
           Opens or creates a file, returning a file channel to access the file.
static FileChannel open(Path file, Set<? extends OpenOption> options, FileAttribute<?>... attrs)
           Opens or creates a file, returning a file channel to access the file.
abstract  FileChannel positionSBC(long newPosition)
          Sets this channel's file position.
abstract  FileChannel truncateSBC(long size)
          Truncates this channel's file to the given size.
 
Methods inherited from class java.nio.channels.FileChannel
force, lock, lock, map, position, position, read, read, read, read, size, transferFrom, transferTo, truncate, tryLock, tryLock, write, write, write, write
 
Methods inherited from class java.nio.channels.spi.AbstractInterruptibleChannel
begin, close, end, implCloseChannel, isOpen
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.classpath.icedtea.java.nio.channels.SeekableByteChannel
position, read, size, write
 
Methods inherited from interface java.nio.channels.Channel
close, isOpen
 

Constructor Detail

FileChannel

protected FileChannel()
Initializes a new instance of this class.

Method Detail

open

public static FileChannel open(Path file,
                               Set<? extends OpenOption> options,
                               FileAttribute<?>... attrs)
                        throws IOException
Opens or creates a file, returning a file channel to access the file.

The options parameter determines how the file is opened. The READ and WRITE options determine if the file should be opened for reading and/or writing. If neither option (or the APPEND option) is contained in the array then the file is opened for reading. By default reading or writing commences at the beginning of the file.

In the addition to READ and WRITE, the following options may be present:

Option Description
APPEND If this option is present then the file is opened for writing and each invocation of the channel's write method first advances the position to the end of the file and then writes the requested data. Whether the advancement of the position and the writing of the data are done in a single atomic operation is system-dependent and therefore unspecified. This option may not be used in conjunction with the READ or TRUNCATE_EXISTING options.
TRUNCATE_EXISTING If this option is present then the existing file is truncated to a size of 0 bytes. This option is ignored when the file is opened only for reading.
CREATE_NEW If this option is present then a new file is created, failing if the file already exists. When creating a file the check for the existence of the file and the creation of the file if it does not exist is atomic with respect to other file system operations. This option is ignored when the file is opened only for reading.
CREATE If this option is present then an existing file is opened if it exists, otherwise a new file is created. When creating a file the check for the existence of the file and the creation of the file if it does not exist is atomic with respect to other file system operations. This option is ignored if the CREATE_NEW option is also present or the file is opened only for reading.
DELETE_ON_CLOSE When this option is present then the implementation makes a best effort attempt to delete the file when closed by the the close method. If the close method is not invoked then a best effort attempt is made to delete the file when the Java virtual machine terminates.
SPARSE When creating a new file this option is a hint that the new file will be sparse. This option is ignored when not creating a new file.
SYNC Requires that every update to the file's content or metadata be written synchronously to the underlying storage device. (see Synchronized I/O file integrity).
DSYNC Requires that every update to the file's content be written synchronously to the underlying storage device. (see Synchronized I/O file integrity).

An implementation may also support additional options.

The attrs parameter is an optional array of file file-attributes to set atomically when creating the file.

The new channel is created by invoking the newFileChannel method on the provider that created the Path.

Parameters:
file - The path of the file to open or create
options - Options specifying how the file is opened
attrs - An optional list of file attributes to set atomically when creating the file
Returns:
A new file channel
Throws:
IllegalArgumentException - If the set contains an invalid combination of options
UnsupportedOperationException - If the file is associated with a provider that does not support creating file channels, or an unsupported open option is specified, or the array contains an attribute that cannot be set atomically when creating the file
IOException - If an I/O error occurs
SecurityException - If a security manager is installed and it denies an unspecified permission required by the implementation. In the case of the default provider, the SecurityManager.checkRead(String) method is invoked to check read access if the file is opened for reading. The SecurityManager.checkWrite(String) method is invoked to check write access if the file is opened for writing
Since:
1.7

open

public static FileChannel open(Path file,
                               OpenOption... options)
                        throws IOException
Opens or creates a file, returning a file channel to access the file.

An invocation of this method behaves in exactly the same way as the invocation

     fc.open(file, options, new FileAttribute<?>[0]);
 

Parameters:
file - The path of the file to open or create
options - Options specifying how the file is opened
Returns:
A new file channel
Throws:
IllegalArgumentException - If the set contains an invalid combination of options
UnsupportedOperationException - If the file is associated with a provider that does not support creating file channels, or an unsupported open option is specified
IOException - If an I/O error occurs
SecurityException - If a security manager is installed and it denies an unspecified permission required by the implementation. In the case of the default provider, the SecurityManager.checkRead(String) method is invoked to check read access if the file is opened for reading. The SecurityManager.checkWrite(String) method is invoked to check write access if the file is opened for writing
Since:
1.7

positionSBC

public abstract FileChannel positionSBC(long newPosition)
                                 throws IOException
Sets this channel's file position.

Setting the position to a value that is greater than the file's current size is legal but does not change the size of the file. A later attempt to read bytes at such a position will immediately return an end-of-file indication. A later attempt to write bytes at such a position will cause the file to 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.

Specified by:
positionSBC in interface SeekableByteChannel
Parameters:
newPosition - The new position, a non-negative integer counting the number of bytes from the beginning of the file
Returns:
This file channel
Throws:
ClosedChannelException - If this channel is closed
IllegalArgumentException - If the new position is negative
IOException - If some other I/O error occurs

truncateSBC

public abstract FileChannel truncateSBC(long size)
                                 throws IOException
Truncates this channel's file to the given size.

If the given size is less than the file's current size then the file is truncated, discarding any bytes beyond the new end of the file. If the given size is greater than or equal to the file's current size then the file is not modified. In either case, if this channel's file position is greater than the given size then it is set to that size.

Specified by:
truncateSBC in interface SeekableByteChannel
Parameters:
size - The new size, a non-negative byte count
Returns:
This file channel
Throws:
NonWritableChannelException - If this channel was not opened for writing
ClosedChannelException - If this channel is closed
IllegalArgumentException - If the new size is negative
IOException - If some other I/O error occurs

NIO2 API

Copyright © 2007, 2011, Oracle and/or its affiliates. All rights reserved.