Java Doc for FileChannel.java in  » 6.0-JDK-Core » io-nio » java » nio » channels » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » io nio » java.nio.channels 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   java.nio.channels.spi.AbstractInterruptibleChannel
      java.nio.channels.FileChannel

FileChannel
abstract public class FileChannel extends AbstractInterruptibleChannel implements ByteChannel,GatheringByteChannel,ScatteringByteChannel(Code)
A channel for reading, writing, mapping, and manipulating a file.

A file channel has a current position within its file which can be both FileChannel.position() queried and FileChannel.position(long)modified . The file itself contains a variable-length sequence of bytes that can be read and written and whose current FileChannel.sizesize 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 FileChannel.truncate 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:

  • Bytes may be FileChannel.read(ByteBuffer,long) read or FileChannel.write(ByteBuffer,long) written at an absolute position in a file in a way that does not affect the channel's current position.

  • A region of a file may be FileChannel.map mapped directly into memory; for large files this is often much more efficient than invoking the usual read or write methods.

  • Updates made to a file may be FileChannel.force forcedout to the underlying storage device, ensuring that data are not lost in the event of a system crash.

  • Bytes can be transferred from a file FileChannel.transferTo tosome other channel , and FileChannel.transferFrom viceversa , in a way that can be optimized by many operating systems into a very fast transfer directly to or from the filesystem cache.

  • A region of a file may be FileLock locked against access by other programs.

File channels are safe for use by multiple concurrent threads. The Channel.close 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.

This class does not define methods for opening existing files or for creating new ones; such methods may be added in a future release. In this release a file channel can be obtained from an existing java.io.FileInputStream.getChannel FileInputStream , java.io.FileOutputStream.getChannel FileOutputStream , or java.io.RandomAccessFile.getChannel RandomAccessFile object by invoking that object's getChannel method, which returns a file channel that is connected to the same underlying file.

The state of a 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 java.io.FileInputStream.getChannel getChannel method of a java.io.FileInputStream instance will be open for reading. A channel obtained via the java.io.FileOutputStream.getChannel getChannel method of a java.io.FileOutputStream instance will be open for writing. Finally, a channel obtained via the java.io.RandomAccessFile.getChannel getChannel method of a java.io.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 java.io.FileOutputStream.FileOutputStream(java.io.Fileboolean)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.
See Also:   java.io.FileInputStream.getChannel
See Also:   java.io.FileOutputStream.getChannel
See Also:   java.io.RandomAccessFile.getChannel
author:
   Mark Reinhold
author:
   Mike McCloskey
author:
   JSR-51 Expert Group
version:
   1.49, 07/05/05
since:
   1.4


Inner Class :public static class MapMode


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

Method Summary
abstract public  voidforce(boolean metaData)
     Forces any updates to this channel's file to be written to the storage device that contains it.

If this channel's file resides on a local storage device then when this method returns it is guaranteed that all changes made to the file since this channel was created, or since this method was last invoked, will have been written to that device.

abstract public  FileLocklock(long position, long size, boolean shared)
     Acquires a lock on the given region of this channel's file.

An invocation of this method will block until the region can be locked, this channel is closed, or the invoking thread is interrupted, whichever comes first.

If this channel is closed by another thread during an invocation of this method then an AsynchronousCloseException will be thrown.

If the invoking thread is interrupted while waiting to acquire the lock then its interrupt status will be set and a FileLockInterruptionException will be thrown.

final public  FileLocklock()
     Acquires an exclusive lock on this channel's file.
abstract public  MappedByteBuffermap(MapMode mode, long position, long size)
     Maps a region of this channel's file directly into memory.

A region of a file may be mapped into memory in one of three modes:

  • Read-only: Any attempt to modify the resulting buffer will cause a java.nio.ReadOnlyBufferException to be thrown. ( MapMode.READ_ONLY MapMode.READ_ONLY )

  • Read/write: Changes made to the resulting buffer will eventually be propagated to the file; they may or may not be made visible to other programs that have mapped the same file.

abstract public  longposition()
     Returns this channel's file position.
abstract public  FileChannelposition(long newPosition)
     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.

abstract public  intread(ByteBuffer dst)
     Reads a sequence of bytes from this channel into the given buffer.

Bytes are read starting at this channel's current file position, and then the file position is updated with the number of bytes actually read.

abstract public  longread(ByteBuffer[] dsts, int offset, int length)
     Reads a sequence of bytes from this channel into a subsequence of the given buffers.

Bytes are read starting at this channel's current file position, and then the file position is updated with the number of bytes actually read.

final public  longread(ByteBuffer[] dsts)
     Reads a sequence of bytes from this channel into the given buffers.

Bytes are read starting at this channel's current file position, and then the file position is updated with the number of bytes actually read.

abstract public  intread(ByteBuffer dst, long position)
     Reads a sequence of bytes from this channel into the given buffer, starting at the given file position.

This method works in the same manner as the FileChannel.read(ByteBuffer) method, except that bytes are read starting at the given file position rather than at the channel's current position.

abstract public  longsize()
     Returns the current size of this channel's file.
abstract public  longtransferFrom(ReadableByteChannel src, long position, long count)
     Transfers bytes into this channel's file from the given readable byte channel.

An attempt is made to read up to count bytes from the source channel and write them to this channel's file starting at the given position.

abstract public  longtransferTo(long position, long count, WritableByteChannel target)
     Transfers bytes from this channel's file to the given writable byte channel.

An attempt is made to read up to count bytes starting at the given position in this channel's file and write them to the target channel.

abstract public  FileChanneltruncate(long size)
     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.

abstract public  FileLocktryLock(long position, long size, boolean shared)
     Attempts to acquire a lock on the given region of this channel's file.

This method does not block.

final public  FileLocktryLock()
     Attempts to acquire an exclusive lock on this channel's file.
abstract public  intwrite(ByteBuffer src)
     Writes a sequence of bytes to this channel from the given buffer.

Bytes are written starting at this channel's current file position unless the channel is in append mode, in which case the position is first advanced to the end of the file.

abstract public  longwrite(ByteBuffer[] srcs, int offset, int length)
     Writes a sequence of bytes to this channel from a subsequence of the given buffers.

Bytes are written starting at this channel's current file position unless the channel is in append mode, in which case the position is first advanced to the end of the file.

final public  longwrite(ByteBuffer[] srcs)
     Writes a sequence of bytes to this channel from the given buffers.

Bytes are written starting at this channel's current file position unless the channel is in append mode, in which case the position is first advanced to the end of the file.

abstract public  intwrite(ByteBuffer src, long position)
     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 FileChannel.write(ByteBuffer) method, except that bytes are written starting at the given file position rather than at the channel's current position. This method does not modify this channel's position.



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




Method Detail
force
abstract public void force(boolean metaData) throws IOException(Code)
Forces any updates to this channel's file to be written to the storage device that contains it.

If this channel's file resides on a local storage device then when this method returns it is guaranteed that all changes made to the file since this channel was created, or since this method was last invoked, will have been written to that device. This is useful for ensuring that critical information is not lost in the event of a system crash.

If the file does not reside on a local device then no such guarantee is made.

The metaData parameter can be used to limit the number of I/O operations that this method is required to perform. Passing false for this parameter indicates that only updates to the file's content need be written to storage; passing true indicates that updates to both the file's content and metadata must be written, which generally requires at least one more I/O operation. Whether this parameter actually has any effect is dependent upon the underlying operating system and is therefore unspecified.

Invoking this method may cause an I/O operation to occur even if the channel was only opened for reading. Some operating systems, for example, maintain a last-access time as part of a file's metadata, and this time is updated whenever the file is read. Whether or not this is actually done is system-dependent and is therefore unspecified.

This method is only guaranteed to force changes that were made to this channel's file via the methods defined in this class. It may or may not force changes that were made by modifying the content of a MappedByteBuffer mapped byte buffer obtained by invoking the FileChannel.map map method. Invoking the MappedByteBuffer.force force method of the mapped byte buffer will force changes made to the buffer's content to be written.


Parameters:
  metaData - If true then this method is required to force changesto both the file's content and metadata to be written tostorage; otherwise, it need only force content changes to bewritten
throws:
  ClosedChannelException - If this channel is closed
throws:
  IOException - If some other I/O error occurs



lock
abstract public FileLock lock(long position, long size, boolean shared) throws IOException(Code)
Acquires a lock on the given region of this channel's file.

An invocation of this method will block until the region can be locked, this channel is closed, or the invoking thread is interrupted, whichever comes first.

If this channel is closed by another thread during an invocation of this method then an AsynchronousCloseException will be thrown.

If the invoking thread is interrupted while waiting to acquire the lock then its interrupt status will be set and a FileLockInterruptionException will be thrown. If the invoker's interrupt status is set when this method is invoked then that exception will be thrown immediately; the thread's interrupt status will not be changed.

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 zero-argument FileChannel.lock() method simply locks a region of size Long.MAX_VALUE .

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 FileLock.isShared 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:
  position - The position at which the locked region is to start; must benon-negative
Parameters:
  size - The size of the locked region; must be non-negative, and the sumposition + size must be non-negative
Parameters:
  shared - true to request a shared lock, in which case thischannel must be open for reading (and possibly writing);false to request an exclusive lock, in which case thischannel must be open for writing (and possibly reading) A lock object representing the newly-acquired lock
throws:
  IllegalArgumentException - If the preconditions on the parameters do not hold
throws:
  ClosedChannelException - If this channel is closed
throws:
  AsynchronousCloseException - If another thread closes this channel while the invokingthread is blocked in this method
throws:
  FileLockInterruptionException - If the invoking thread is interrupted while blocked in thismethod
throws:
  OverlappingFileLockException - If a lock that overlaps the requested region is already held bythis Java virtual machine, or if another thread is alreadyblocked in this method and is attempting to lock an overlappingregion
throws:
  NonReadableChannelException - If shared is true this channel was notopened for reading
throws:
  NonWritableChannelException - If shared is false but this channel was notopened for writing
throws:
  IOException - If some other I/O error occurs
See Also:   FileChannel.lock()
See Also:   FileChannel.tryLock()
See Also:   FileChannel.tryLock(long,long,boolean)



lock
final public FileLock lock() throws IOException(Code)
Acquires an exclusive lock on this channel's file.

An invocation of this method of the form fc.lock() behaves in exactly the same way as the invocation

 fc.
FileChannel.lock(long,long,boolean) lock (0L, Long.MAX_VALUE, false) 
A lock object representing the newly-acquired lock
throws:
  ClosedChannelException - If this channel is closed
throws:
  AsynchronousCloseException - If another thread closes this channel while the invokingthread is blocked in this method
throws:
  FileLockInterruptionException - If the invoking thread is interrupted while blocked in thismethod
throws:
  OverlappingFileLockException - If a lock that overlaps the requested region is already held bythis Java virtual machine, or if another thread is alreadyblocked in this method and is attempting to lock an overlappingregion of the same file
throws:
  NonWritableChannelException - If this channel was not opened for writing
throws:
  IOException - If some other I/O error occurs
See Also:   FileChannel.lock(long,long,boolean)
See Also:   FileChannel.tryLock()
See Also:   FileChannel.tryLock(long,long,boolean)



map
abstract public MappedByteBuffer map(MapMode mode, long position, long size) throws IOException(Code)
Maps a region of this channel's file directly into memory.

A region of a file may be mapped into memory in one of three modes:

  • Read-only: Any attempt to modify the resulting buffer will cause a java.nio.ReadOnlyBufferException to be thrown. ( MapMode.READ_ONLY MapMode.READ_ONLY )

  • Read/write: Changes made to the resulting buffer will eventually be propagated to the file; they may or may not be made visible to other programs that have mapped the same file. ( MapMode.READ_WRITE MapMode.READ_WRITE )

  • Private: Changes made to the resulting buffer will not be propagated to the file and will not be visible to other programs that have mapped the same file; instead, they will cause private copies of the modified portions of the buffer to be created. ( MapMode.PRIVATE MapMode.PRIVATE )

For a read-only mapping, this channel must have been opened for reading; for a read/write or private mapping, this channel must have been opened for both reading and writing.

The MappedByteBuffer mapped byte buffer returned by this method will have a position of zero and a limit and capacity of size; its mark will be undefined. The buffer and the mapping that it represents will remain valid until the buffer itself is garbage-collected.

A mapping, once established, is not dependent upon the file channel that was used to create it. Closing the channel, in particular, has no effect upon the validity of the mapping.

Many of the details of memory-mapped files are inherently dependent upon the underlying operating system and are therefore unspecified. The behavior of this method when the requested region is not completely contained within this channel's file is unspecified. Whether changes made to the content or size of the underlying file, by this program or another, are propagated to the buffer is unspecified. The rate at which changes to the buffer are propagated to the file is unspecified.

For most operating systems, mapping a file into memory is more expensive than reading or writing a few tens of kilobytes of data via the usual FileChannel.read read and FileChannel.write write methods. From the standpoint of performance it is generally only worth mapping relatively large files into memory.


Parameters:
  mode - One of the constants MapMode.READ_ONLY READ_ONLY, MapMode.READ_WRITE READ_WRITE, or MapMode.PRIVATEPRIVATE defined in the MapMode class, according towhether the file is to be mapped read-only, read/write, orprivately (copy-on-write), respectively
Parameters:
  position - The position within the file at which the mapped regionis to start; must be non-negative
Parameters:
  size - The size of the region to be mapped; must be non-negative andno greater than java.lang.Integer.MAX_VALUE
throws:
  NonReadableChannelException - If the mode is MapMode.READ_ONLY READ_ONLY butthis channel was not opened for reading
throws:
  NonWritableChannelException - If the mode is MapMode.READ_WRITE READ_WRITE orMapMode.PRIVATE PRIVATE but this channel was not openedfor both reading and writing
throws:
  IllegalArgumentException - If the preconditions on the parameters do not hold
throws:
  IOException - If some other I/O error occurs
See Also:   java.nio.channels.FileChannel.MapMode
See Also:   java.nio.MappedByteBuffer



position
abstract public long position() throws IOException(Code)
Returns this channel's file position.

This channel's file position,a non-negative integer counting the number of bytesfrom the beginning of the file to the current position
throws:
  ClosedChannelException - If this channel is closed
throws:
  IOException - If some other I/O error occurs



position
abstract public FileChannel position(long newPosition) throws IOException(Code)
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.


Parameters:
  newPosition - The new position, a non-negative integer countingthe number of bytes from the beginning of the file This file channel
throws:
  ClosedChannelException - If this channel is closed
throws:
  IllegalArgumentException - If the new position is negative
throws:
  IOException - If some other I/O error occurs



read
abstract public int read(ByteBuffer dst) throws IOException(Code)
Reads a sequence of bytes from this channel into the given buffer.

Bytes are read starting at this channel's current file position, and then the file position is updated with the number of bytes actually read. Otherwise this method behaves exactly as specified in the ReadableByteChannel interface.




read
abstract public long read(ByteBuffer[] dsts, int offset, int length) throws IOException(Code)
Reads a sequence of bytes from this channel into a subsequence of the given buffers.

Bytes are read starting at this channel's current file position, and then the file position is updated with the number of bytes actually read. Otherwise this method behaves exactly as specified in the ScatteringByteChannel interface.




read
final public long read(ByteBuffer[] dsts) throws IOException(Code)
Reads a sequence of bytes from this channel into the given buffers.

Bytes are read starting at this channel's current file position, and then the file position is updated with the number of bytes actually read. Otherwise this method behaves exactly as specified in the ScatteringByteChannel interface.




read
abstract public int read(ByteBuffer dst, long position) throws IOException(Code)
Reads a sequence of bytes from this channel into the given buffer, starting at the given file position.

This method works in the same manner as the FileChannel.read(ByteBuffer) method, except that bytes are read starting at the given file position rather than at the channel's current position. This method does not modify this channel's position. If the given position is greater than the file's current size then no bytes are read.


Parameters:
  dst - The buffer into which bytes are to be transferred
Parameters:
  position - The file position at which the transfer is to begin;must be non-negative The number of bytes read, possibly zero, or -1 if thegiven position is greater than or equal to the file's currentsize
throws:
  IllegalArgumentException - If the position is negative
throws:
  NonReadableChannelException - If this channel was not opened for reading
throws:
  ClosedChannelException - If this channel is closed
throws:
  AsynchronousCloseException - If another thread closes this channelwhile the read operation is in progress
throws:
  ClosedByInterruptException - If another thread interrupts the current threadwhile the read operation is in progress, therebyclosing the channel and setting the current thread'sinterrupt status
throws:
  IOException - If some other I/O error occurs



size
abstract public long size() throws IOException(Code)
Returns the current size of this channel's file.

The current size of this channel's file,measured in bytes
throws:
  ClosedChannelException - If this channel is closed
throws:
  IOException - If some other I/O error occurs



transferFrom
abstract public long transferFrom(ReadableByteChannel src, long position, long count) throws IOException(Code)
Transfers bytes into this channel's file from the given readable byte channel.

An attempt is made to read up to count bytes from the source channel and write them to this channel's file starting at the given position. An invocation of this method may or may not transfer all of the requested bytes; whether or not it does so depends upon the natures and states of the channels. Fewer than the requested number of bytes will be transferred if the source channel has fewer than count bytes remaining, or if the source channel is non-blocking and has fewer than count bytes immediately available in its input buffer.

This method does not modify this channel's position. If the given position is greater than the file's current size then no bytes are transferred. If the source channel has a position then bytes are read starting at that position and then the position is incremented by the number of bytes read.

This method is potentially much more efficient than a simple loop that reads from the source channel and writes to this channel. Many operating systems can transfer bytes directly from the source channel into the filesystem cache without actually copying them.


Parameters:
  src - The source channel
Parameters:
  position - The position within the file at which the transfer is to begin;must be non-negative
Parameters:
  count - The maximum number of bytes to be transferred; must benon-negative The number of bytes, possibly zero,that were actually transferred
throws:
  IllegalArgumentException - If the preconditions on the parameters do not hold
throws:
  NonReadableChannelException - If the source channel was not opened for reading
throws:
  NonWritableChannelException - If this channel was not opened for writing
throws:
  ClosedChannelException - If either this channel or the source channel is closed
throws:
  AsynchronousCloseException - If another thread closes either channelwhile the transfer is in progress
throws:
  ClosedByInterruptException - If another thread interrupts the current thread while thetransfer is in progress, thereby closing both channels andsetting the current thread's interrupt status
throws:
  IOException - If some other I/O error occurs



transferTo
abstract public long transferTo(long position, long count, WritableByteChannel target) throws IOException(Code)
Transfers bytes from this channel's file to the given writable byte channel.

An attempt is made to read up to count bytes starting at the given position in this channel's file and write them to the target channel. An invocation of this method may or may not transfer all of the requested bytes; whether or not it does so depends upon the natures and states of the channels. Fewer than the requested number of bytes are transferred if this channel's file contains fewer than count bytes starting at the given position, or if the target channel is non-blocking and it has fewer than count bytes free in its output buffer.

This method does not modify this channel's position. If the given position is greater than the file's current size then no bytes are transferred. If the target channel has a position then bytes are written starting at that position and then the position is incremented by the number of bytes written.

This method is potentially much more efficient than a simple loop that reads from this channel and writes to the target channel. Many operating systems can transfer bytes directly from the filesystem cache to the target channel without actually copying them.


Parameters:
  position - The position within the file at which the transfer is to begin;must be non-negative
Parameters:
  count - The maximum number of bytes to be transferred; must benon-negative
Parameters:
  target - The target channel The number of bytes, possibly zero,that were actually transferred
throws:
  IllegalArgumentException - If the preconditions on the parameters do not hold
throws:
  NonReadableChannelException - If this channel was not opened for reading
throws:
  NonWritableChannelException - If the target channel was not opened for writing
throws:
  ClosedChannelException - If either this channel or the target channel is closed
throws:
  AsynchronousCloseException - If another thread closes either channelwhile the transfer is in progress
throws:
  ClosedByInterruptException - If another thread interrupts the current thread while thetransfer is in progress, thereby closing both channels andsetting the current thread's interrupt status
throws:
  IOException - If some other I/O error occurs



truncate
abstract public FileChannel truncate(long size) throws IOException(Code)
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.


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



tryLock
abstract public FileLock tryLock(long position, long size, boolean shared) throws IOException(Code)
Attempts to acquire a lock on the given region of this channel's file.

This method does not block. An invocation always returns immediately, either having acquired a lock on the requested region or having failed to do so. If it fails to acquire a lock because an overlapping lock is held by another program then it returns null. If it fails to acquire a lock for any other reason then an appropriate exception is thrown.

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 zero-argument FileChannel.tryLock() method simply locks a region of size Long.MAX_VALUE .

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 FileLock.isShared 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:
  position - The position at which the locked region is to start; must benon-negative
Parameters:
  size - The size of the locked region; must be non-negative, and the sumposition + size must be non-negative
Parameters:
  shared - true to request a shared lock,false to request an exclusive lock A lock object representing the newly-acquired lock,or null if the lock could not be acquiredbecause another program holds an overlapping lock
throws:
  IllegalArgumentException - If the preconditions on the parameters do not hold
throws:
  ClosedChannelException - If this channel is closed
throws:
  OverlappingFileLockException - If a lock that overlaps the requested region is already held bythis Java virtual machine, or if another thread is alreadyblocked in this method and is attempting to lock an overlappingregion of the same file
throws:
  IOException - If some other I/O error occurs
See Also:   FileChannel.lock()
See Also:   FileChannel.lock(long,long,boolean)
See Also:   FileChannel.tryLock()



tryLock
final public FileLock tryLock() throws IOException(Code)
Attempts to acquire an exclusive lock on this channel's file.

An invocation of this method of the form fc.tryLock() behaves in exactly the same way as the invocation

 fc.
FileChannel.tryLock(long,long,boolean) tryLock (0L, Long.MAX_VALUE, false) 
A lock object representing the newly-acquired lock,or null if the lock could not be acquiredbecause another program holds an overlapping lock
throws:
  ClosedChannelException - If this channel is closed
throws:
  OverlappingFileLockException - If a lock that overlaps the requested region is already held bythis Java virtual machine, or if another thread is alreadyblocked in this method and is attempting to lock an overlappingregion
throws:
  IOException - If some other I/O error occurs
See Also:   FileChannel.lock()
See Also:   FileChannel.lock(long,long,boolean)
See Also:   FileChannel.tryLock(long,long,boolean)



write
abstract public int write(ByteBuffer src) throws IOException(Code)
Writes a sequence of bytes to this channel from the given buffer.

Bytes are written starting at this channel's current file position unless the channel is in append mode, in which case the position is first advanced to the end of the file. The file is grown, if necessary, to accommodate the written bytes, and then the file position is updated with the number of bytes actually written. Otherwise this method behaves exactly as specified by the WritableByteChannel interface.




write
abstract public long write(ByteBuffer[] srcs, int offset, int length) throws IOException(Code)
Writes a sequence of bytes to this channel from a subsequence of the given buffers.

Bytes are written starting at this channel's current file position unless the channel is in append mode, in which case the position is first advanced to the end of the file. The file is grown, if necessary, to accommodate the written bytes, and then the file position is updated with the number of bytes actually written. Otherwise this method behaves exactly as specified in the GatheringByteChannel interface.




write
final public long write(ByteBuffer[] srcs) throws IOException(Code)
Writes a sequence of bytes to this channel from the given buffers.

Bytes are written starting at this channel's current file position unless the channel is in append mode, in which case the position is first advanced to the end of the file. The file is grown, if necessary, to accommodate the written bytes, and then the file position is updated with the number of bytes actually written. Otherwise this method behaves exactly as specified in the GatheringByteChannel interface.




write
abstract public int write(ByteBuffer src, long position) throws IOException(Code)
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 FileChannel.write(ByteBuffer) method, except that bytes are written starting at the given file position rather than at the channel's current position. This method does not modify this channel's position. If the given position is greater than the file's current size 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 - The buffer from which bytes are to be transferred
Parameters:
  position - The file position at which the transfer is to begin;must be non-negative The number of bytes written, possibly zero
throws:
  IllegalArgumentException - If the position is negative
throws:
  NonWritableChannelException - If this channel was not opened for writing
throws:
  ClosedChannelException - If this channel is closed
throws:
  AsynchronousCloseException - If another thread closes this channelwhile the write operation is in progress
throws:
  ClosedByInterruptException - If another thread interrupts the current threadwhile the write operation is in progress, therebyclosing the channel and setting the current thread'sinterrupt status
throws:
  IOException - If some other I/O error occurs



Methods inherited from java.nio.channels.spi.AbstractInterruptibleChannel
final protected void begin()(Code)(Java Doc)
final public void close() throws IOException(Code)(Java Doc)
final protected void end(boolean completed) throws AsynchronousCloseException(Code)(Java Doc)
abstract protected void implCloseChannel() throws IOException(Code)(Java Doc)
final public boolean isOpen()(Code)(Java Doc)

Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.