Java Doc for FileChannel.java in  » Apache-Harmony-Java-SE » java-package » java » nio » channels » Java Source Code / Java DocumentationJava Source Code and Java Documentation

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 geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » java package » 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 GatheringByteChannel,ScatteringByteChannel,ByteChannel(Code)
An abstract channel type for interaction with a platform file.

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 FileInputStream, FileOutputStream, and RandomAccessFile types.

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 proceeded 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 referred 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:

  • request that cached data be forced onto the disk
  • lock ranges of bytes associated with the file
  • transfer data directly to another channel in a manner that has the potential to be optimized by the platform
  • memory-mapping files into NIO buffers to provide efficient manipulation of file data
  • read and write to the file at absolute byte offsets in a fashion that does not modify the current position

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.


Inner Class :public static class MapMode


Constructor Summary
protected  FileChannel()
     Protected default constructor.

Method Summary
abstract public  voidforce(boolean metadata)
     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.

final public  FileLocklock()
     Obtain an exclusive lock on this file.

This is a convenience method for acquiring a maximum length lock on a file.

abstract public  FileLocklock(long position, long size, boolean shared)
     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.

abstract public  MappedByteBuffermap(FileChannel.MapMode mode, long position, long size)
     Maps the file into memory.There can be three modes:Read-only,Read/write and Private.
abstract public  longposition()
     Answers the current value of the file position pointer.
abstract public  FileChannelposition(long offset)
     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.

abstract public  intread(ByteBuffer buffer)
     Reads bytes from the channel into the given byte buffer.
abstract public  intread(ByteBuffer buffer, long position)
     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).

final public  longread(ByteBuffer[] buffers)
     Reads bytes from the channel into all the given byte buffers.
abstract public  longread(ByteBuffer[] buffers, int start, int number)
     Reads bytes from the file channel into a subset of the given byte buffers.
abstract public  longsize()
     Answers the size of the file underlying this channel, in bytes.
abstract public  longtransferFrom(ReadableByteChannel src, long position, long count)
     Transfers bytes into this channel's file from the given readable byte channel.
abstract public  longtransferTo(long position, long count, WritableByteChannel target)
     Transfers data from the file to the given channel.
abstract public  FileChanneltruncate(long size)
     Truncates the file underlying this channel to a given size.

Any bytes beyond the given size are removed from the file.

final public  FileLocktryLock()
     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.

abstract public  FileLocktryLock(long position, long size, boolean shared)
     Attempts to acquire an exclusive lock on this file without blocking.
abstract public  intwrite(ByteBuffer src)
     Writes bytes from the given byte buffer into the file channel.
abstract public  intwrite(ByteBuffer buffer, long position)
     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).

final public  longwrite(ByteBuffer[] buffers)
     Writes bytes from all the given byte buffers into the file channel.
abstract public  longwrite(ByteBuffer[] buffers, int offset, int length)
    


Constructor Detail
FileChannel
protected FileChannel()(Code)
Protected default constructor.




Method Detail
force
abstract public void force(boolean metadata) throws IOException(Code)
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.
Parameters:
  metadata - true if the file metadata should be flushed in addition to thefile content, and false otherwise.
throws:
  ClosedChannelException - if the channel is already closed.
throws:
  IOException - some other problem occurred.




lock
final public FileLock lock() throws IOException(Code)
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)
 
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 lockrequest, or another thread is waiting to acquire a lock thatwill overlap with this request.
throws:
  FileLockInterruptionException - The calling thread was interrupted while waiting to acquirethe lock.
throws:
  AsynchronousCloseException - The channel was closed while the calling thread was waitingto acquire the lock.
throws:
  IOException - some other problem occurred obtaining the requested lock.



lock
abstract public FileLock lock(long position, long size, boolean shared) throws IOException(Code)
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.


Parameters:
  position - the starting position for the lock region
Parameters:
  size - the length of the lock, in bytes
Parameters:
  shared - a flag indicating whether an attempt should be made to acquirea shared lock. 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 pendinglock 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 methodis in operation.
throws:
  FileLockInterruptionException - if the thread is interrupted while in the state of waiting onthe desired file lock.
throws:
  IOException - if some other IO problem occurs.



map
abstract public MappedByteBuffer map(FileChannel.MapMode mode, long position, long size) throws IOException(Code)
Maps the file into memory.There can be three modes:Read-only,Read/write and Private. After mapping, the memory and the file channel do not affect each other. Note : mapping a file into memory is usually expensive.
Parameters:
  mode - one of three modes to map
Parameters:
  position - the starting position of the file
Parameters:
  size - the size to map the mapped byte buffer
throws:
  NonReadableChannelException - If the file is not opened for reading but the given mode is"READ_ONLY"
throws:
  NonWritableChannelException - If the file is not opened for writing but the mode is not"READ_ONLY"
throws:
  IllegalArgumentException - If the given parameters of position and size are not correct
throws:
  IOException - If any I/O error occurs



position
abstract public long position() throws IOException(Code)
Answers the current value of the file position pointer. the current position as a positive integer number of bytes fromthe start of the file.
throws:
  ClosedChannelException - if the channel is already closed.
throws:
  IOException - if some other IO problem occurs.



position
abstract public FileChannel position(long offset) throws IOException(Code)
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.
Parameters:
  offset - the new file position, in bytes. 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.




read
abstract public int read(ByteBuffer buffer) throws IOException(Code)
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 Also:   java.nio.channels.ReadableByteChannel.read(java.nio.ByteBuffer)




read
abstract public int read(ByteBuffer buffer, long position) throws IOException(Code)
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.


Parameters:
  buffer - the buffer to receive the bytes
Parameters:
  position - the (non-negative) position at which to read the bytes. the number of bytes actually read.
throws:
  IllegalArgumentException - if position 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 methodis in operation.
throws:
  ClosedByInterruptException - if another thread interrupts the calling thread while theoperation is in progress. The calling thread will have theinterrupt state set, and the channel will be closed.
throws:
  IOException - some other IO error occurred.



read
final public long read(ByteBuffer[] buffers) throws IOException(Code)
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 Also:   java.nio.channels.ScatteringByteChannel.read(java.nio.ByteBuffer[])



read
abstract public long read(ByteBuffer[] buffers, int start, int number) throws IOException(Code)
Reads bytes from the file channel into a subset of the given byte buffers.
See Also:   java.nio.channels.ScatteringByteChannel.read(java.nio.ByteBuffer[]intint)



size
abstract public long size() throws IOException(Code)
Answers the size of the file underlying this channel, in bytes. 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.



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. It may be very efficient. By invoking this method, it will read form the source channel and write into the file channel. Note: no guarantee whether all bytes may be transferred. And it does not modify the position of the channel.
Parameters:
  src - the source channel to read
Parameters:
  position - the non-negative position to begin
Parameters:
  count - the non-negative bytes to be transferred the number of bytes that are transferred.
throws:
  IllegalArgumentException - If the parameters are not correct
throws:
  NonReadableChannelException - If the source channel is not readable
throws:
  NonWritableChannelException - If this channel is not writable
throws:
  ClosedChannelException - If either channel has already been closed
throws:
  AsynchronousCloseException - If either channel is closed by other threads during thisoperation
throws:
  ClosedByInterruptException - If the thread is interrupted during this operation
throws:
  IOException - If any I/O error occurs



transferTo
abstract public long transferTo(long position, long count, WritableByteChannel target) throws IOException(Code)
Transfers data from the file to the given channel. It may be very efficient. By invoking this method, it will read form the file and write into the writable channel. Note: no guarantee whether all bytes may be transfered.And it does not modify the position of the channel.
Parameters:
  position - the non-negative position to begin
Parameters:
  count - the non-negative bytes to be transferred
Parameters:
  target - the target channel to write into the number of bytes that were transferred.
throws:
  IllegalArgumentException - If the parameters are not correct
throws:
  NonReadableChannelException - If this channel is not readable
throws:
  NonWritableChannelException - If the target channel is not writable
throws:
  ClosedChannelException - If either channel has already been closed
throws:
  AsynchronousCloseException - If either channel is closed by other threads during thisoperation
throws:
  ClosedByInterruptException - If the thread is interrupted during this operation
throws:
  IOException - If any I/O error occurs



truncate
abstract public FileChannel truncate(long size) throws IOException(Code)
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 currently greater than the given size, then it is set to be the given size.


Parameters:
  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 occurred. this channel



tryLock
final public FileLock tryLock() throws IOException(Code)
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 if the acquisition would result in an overlapped lock with another OS process.

the file lock object, or null if the lock wouldoverlap an existing exclusive lock in another OS process.
throws:
  ClosedChannelException - the file channel is closed.
throws:
  OverlappingFileLockException - Either a lock is already held that overlaps this lockrequest, or another thread is waiting to acquire a lock thatwill overlap with this request.
throws:
  IOException - if any I/O error occurs



tryLock
abstract public FileLock tryLock(long position, long size, boolean shared) throws IOException(Code)
Attempts to acquire an exclusive lock on this file without blocking.

The method returns null if the acquisition would result in an overlapped lock with another OS process.


Parameters:
  position - the starting position
Parameters:
  size - the size of file to lock
Parameters:
  shared - true if share the file lock object, or null if the lock wouldoverlap an existing exclusive lock in another OS process.
throws:
  IllegalArgumentException - If any parameters are bad
throws:
  ClosedChannelException - the file channel is closed.
throws:
  OverlappingFileLockException - Either a lock is already held that overlaps this lockrequest, or another thread is waiting to acquire a lock thatwill overlap with this request.
throws:
  IOException - if any I/O error occurs



write
abstract public int write(ByteBuffer src) throws IOException(Code)
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 Also:   java.nio.channels.WritableByteChannel.write(java.nio.ByteBuffer)
Parameters:
  src - the source buffer to write




write
abstract public int write(ByteBuffer buffer, long position) throws IOException(Code)
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.


Parameters:
  buffer - the buffer containing the bytes to be written.
Parameters:
  position - the (non-negative) position at which to write the bytes. the number of bytes actually written.
throws:
  IllegalArgumentException - if position is less than -1.
throws:
  ClosedChannelException - if the channel is already closed.
throws:
  NonWritableChannelException - if the channel was not opened in write-mode.
throws:
  AsynchronousCloseException - if the channel is closed by another thread while this methodis in operation.
throws:
  ClosedByInterruptException - if another thread interrupts the calling thread while theoperation is in progress. The calling thread will have theinterrupt state set, and the channel will be closed.
throws:
  IOException - some other IO error occurred.



write
final public long write(ByteBuffer[] buffers) throws IOException(Code)
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 Also:   java.nio.channels.GatheringByteChannel.write(java.nio.ByteBuffer[])



write
abstract public long write(ByteBuffer[] buffers, int offset, int length) throws IOException(Code)

See Also:   java.nio.channels.GatheringByteChannel.write(java.nio.ByteBuffer[]intint)



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 success) throws AsynchronousCloseException(Code)(Java Doc)
abstract protected void implCloseChannel() throws IOException(Code)(Java Doc)
final public synchronized boolean isOpen()(Code)(Java Doc)

Methods inherited from java.lang.Object
protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object object)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final public Class<? extends Object> getClass()(Code)(Java Doc)
public int hashCode()(Code)(Java Doc)
final public void notify()(Code)(Java Doc)
final public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final public void wait(long millis, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait(long millis) 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.