1. /*
  2. * @(#)FileChannel.java 1.40 04/01/12
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.nio.channels;
  8. import java.io.*;
  9. import java.nio.ByteBuffer;
  10. import java.nio.MappedByteBuffer;
  11. import java.nio.channels.spi.AbstractInterruptibleChannel;
  12. /**
  13. * A channel for reading, writing, mapping, and manipulating a file.
  14. *
  15. * <p> A file channel has a current <i>position</i> within its file which can
  16. * be both {@link #position() </code>queried<code>} and {@link #position(long)
  17. * </code>modified<code>}. The file itself contains a variable-length sequence
  18. * of bytes that can be read and written and whose current {@link #size
  19. * </code><i>size</i><code>} can be queried. The size of the file increases
  20. * when bytes are written beyond its current size; the size of the file
  21. * decreases when it is {@link #truncate </code><i>truncated</i><code>}. The
  22. * file may also have some associated <i>metadata</i> such as access
  23. * permissions, content type, and last-modification time; this class does not
  24. * define methods for metadata access.
  25. *
  26. * <p> In addition to the familiar read, write, and close operations of byte
  27. * channels, this class defines the following file-specific operations: </p>
  28. *
  29. * <ul>
  30. *
  31. * <li><p> Bytes may be {@link #read(ByteBuffer, long) </code>read<code>} or
  32. * {@link #write(ByteBuffer, long) </code>written<code>} at an absolute
  33. * position in a file in a way that does not affect the channel's current
  34. * position. </p></li>
  35. *
  36. * <li><p> A region of a file may be {@link #map </code>mapped<code>}
  37. * directly into memory; for large files this is often much more efficient
  38. * than invoking the usual <tt>read</tt> or <tt>write</tt> methods.
  39. * </p></li>
  40. *
  41. * <li><p> Updates made to a file may be {@link #force </code>forced
  42. * out<code>} to the underlying storage device, ensuring that data are not
  43. * lost in the event of a system crash. </p></li>
  44. *
  45. * <li><p> Bytes can be transferred from a file {@link #transferTo </code>to
  46. * some other channel<code>}, and {@link #transferFrom </code>vice
  47. * versa<code>}, in a way that can be optimized by many operating systems
  48. * into a very fast transfer directly to or from the filesystem cache.
  49. * </p></li>
  50. *
  51. * <li><p> A region of a file may be {@link FileLock </code>locked<code>}
  52. * against access by other programs. </p></li>
  53. *
  54. * </ul>
  55. *
  56. * <p> File channels are safe for use by multiple concurrent threads. The
  57. * {@link Channel#close close} method may be invoked at any time, as specified
  58. * by the {@link Channel} interface. Only one operation that involves the
  59. * channel's position or can change its file's size may be in progress at any
  60. * given time; attempts to initiate a second such operation while the first is
  61. * still in progress will block until the first operation completes. Other
  62. * operations, in particular those that take an explicit position, may proceed
  63. * concurrently; whether they in fact do so is dependent upon the underlying
  64. * implementation and is therefore unspecified.
  65. *
  66. * <p> The view of a file provided by an instance of this class is guaranteed
  67. * to be consistent with other views of the same file provided by other
  68. * instances in the same program. The view provided by an instance of this
  69. * class may or may not, however, be consistent with the views seen by other
  70. * concurrently-running programs due to caching performed by the underlying
  71. * operating system and delays induced by network-filesystem protocols. This
  72. * is true regardless of the language in which these other programs are
  73. * written, and whether they are running on the same machine or on some other
  74. * machine. The exact nature of any such inconsistencies are system-dependent
  75. * and are therefore unspecified.
  76. *
  77. * <p> This class does not define methods for opening existing files or for
  78. * creating new ones; such methods may be added in a future release. In this
  79. * release a file channel can be obtained from an existing {@link
  80. * java.io.FileInputStream#getChannel FileInputStream}, {@link
  81. * java.io.FileOutputStream#getChannel FileOutputStream}, or {@link
  82. * java.io.RandomAccessFile#getChannel RandomAccessFile} object by invoking
  83. * that object's <tt>getChannel</tt> method, which returns a file channel that
  84. * is connected to the same underlying file.
  85. *
  86. * <p> The state of a file channel is intimately connected to that of the
  87. * object whose <tt>getChannel</tt> method returned the channel. Changing the
  88. * channel's position, whether explicitly or by reading or writing bytes, will
  89. * change the file position of the originating object, and vice versa.
  90. * Changing the file's length via the file channel will change the length seen
  91. * via the originating object, and vice versa. Changing the file's content by
  92. * writing bytes will change the content seen by the originating object, and
  93. * vice versa.
  94. *
  95. * <a name="open-mode"><p> At various points this class specifies that an
  96. * instance that is "open for reading," "open for writing," or "open for
  97. * reading and writing" is required. A channel obtained via the {@link
  98. * java.io.FileInputStream#getChannel getChannel} method of a {@link
  99. * java.io.FileInputStream} instance will be open for reading. A channel
  100. * obtained via the {@link java.io.FileOutputStream#getChannel getChannel}
  101. * method of a {@link java.io.FileOutputStream} instance will be open for
  102. * writing. Finally, a channel obtained via the {@link
  103. * java.io.RandomAccessFile#getChannel getChannel} method of a {@link
  104. * java.io.RandomAccessFile} instance will be open for reading if the instance
  105. * was created with mode <tt>"r"</tt> and will be open for reading and writing
  106. * if the instance was created with mode <tt>"rw"</tt>.
  107. *
  108. * <a name="append-mode"><p> A file channel that is open for writing may be in
  109. * <i>append mode</i>, for example if it was obtained from a file-output stream
  110. * that was created by invoking the {@link
  111. * java.io.FileOutputStream#FileOutputStream(java.io.File,boolean)
  112. * FileOutputStream(File,boolean)} constructor and passing <tt>true</tt> for
  113. * the second parameter. In this mode each invocation of a relative write
  114. * operation first advances the position to the end of the file and then writes
  115. * the requested data. Whether the advancement of the position and the writing
  116. * of the data are done in a single atomic operation is system-dependent and
  117. * therefore unspecified.
  118. *
  119. *
  120. * @see java.io.FileInputStream#getChannel()
  121. * @see java.io.FileOutputStream#getChannel()
  122. * @see java.io.RandomAccessFile#getChannel()
  123. *
  124. * @author Mark Reinhold
  125. * @author Mike McCloskey
  126. * @author JSR-51 Expert Group
  127. * @version 1.40, 04/01/12
  128. * @since 1.4
  129. */
  130. public abstract class FileChannel
  131. extends AbstractInterruptibleChannel
  132. implements ByteChannel, GatheringByteChannel, ScatteringByteChannel
  133. {
  134. /**
  135. * Initializes a new instance of this class.
  136. */
  137. protected FileChannel() { }
  138. // -- Channel operations --
  139. /**
  140. * Reads a sequence of bytes from this channel into the given buffer.
  141. *
  142. * <p> Bytes are read starting at this channel's current file position, and
  143. * then the file position is updated with the number of bytes actually
  144. * read. Otherwise this method behaves exactly as specified in the {@link
  145. * ReadableByteChannel} interface. </p>
  146. */
  147. public abstract int read(ByteBuffer dst) throws IOException;
  148. /**
  149. * Reads a sequence of bytes from this channel into a subsequence of the
  150. * given buffers.
  151. *
  152. * <p> Bytes are read starting at this channel's current file position, and
  153. * then the file position is updated with the number of bytes actually
  154. * read. Otherwise this method behaves exactly as specified in the {@link
  155. * ScatteringByteChannel} interface. </p>
  156. */
  157. public abstract long read(ByteBuffer[] dsts, int offset, int length)
  158. throws IOException;
  159. /**
  160. * Reads a sequence of bytes from this channel into the given buffers.
  161. *
  162. * <p> Bytes are read starting at this channel's current file position, and
  163. * then the file position is updated with the number of bytes actually
  164. * read. Otherwise this method behaves exactly as specified in the {@link
  165. * ScatteringByteChannel} interface. </p>
  166. */
  167. public final long read(ByteBuffer[] dsts) throws IOException {
  168. return read(dsts, 0, dsts.length);
  169. }
  170. /**
  171. * Writes a sequence of bytes to this channel from the given buffer.
  172. *
  173. * <p> Bytes are written starting at this channel's current file position
  174. * unless the channel is in append mode, in which case the position is
  175. * first advanced to the end of the file. The file is grown, if necessary,
  176. * to accommodate the written bytes, and then the file position is updated
  177. * with the number of bytes actually written. Otherwise this method
  178. * behaves exactly as specified by the {@link WritableByteChannel}
  179. * interface. </p>
  180. */
  181. public abstract int write(ByteBuffer src) throws IOException;
  182. /**
  183. * Writes a sequence of bytes to this channel from a subsequence of the
  184. * given buffers.
  185. *
  186. * <p> Bytes are written starting at this channel's current file position
  187. * unless the channel is in append mode, in which case the position is
  188. * first advanced to the end of the file. The file is grown, if necessary,
  189. * to accommodate the written bytes, and then the file position is updated
  190. * with the number of bytes actually written. Otherwise this method
  191. * behaves exactly as specified in the {@link GatheringByteChannel}
  192. * interface. </p>
  193. */
  194. public abstract long write(ByteBuffer[] srcs, int offset, int length)
  195. throws IOException;
  196. /**
  197. * Writes a sequence of bytes to this channel from the given buffers.
  198. *
  199. * <p> Bytes are written starting at this channel's current file position
  200. * unless the channel is in append mode, in which case the position is
  201. * first advanced to the end of the file. The file is grown, if necessary,
  202. * to accommodate the written bytes, and then the file position is updated
  203. * with the number of bytes actually written. Otherwise this method
  204. * behaves exactly as specified in the {@link GatheringByteChannel}
  205. * interface. </p>
  206. */
  207. public final long write(ByteBuffer[] srcs) throws IOException {
  208. return write(srcs, 0, srcs.length);
  209. }
  210. // -- Other operations --
  211. /**
  212. * Returns this channel's file position. </p>
  213. *
  214. * @return This channel's file position,
  215. * a non-negative integer counting the number of bytes
  216. * from the beginning of the file to the current position
  217. *
  218. * @throws ClosedChannelException
  219. * If this channel is closed
  220. *
  221. * @throws IOException
  222. * If some other I/O error occurs
  223. */
  224. public abstract long position() throws IOException;
  225. /**
  226. * Sets this channel's file position.
  227. *
  228. * <p> Setting the position to a value that is greater than the file's
  229. * current size is legal but does not change the size of the file. A later
  230. * attempt to read bytes at such a position will immediately return an
  231. * end-of-file indication. A later attempt to write bytes at such a
  232. * position will cause the file to be grown to accommodate the new bytes;
  233. * the values of any bytes between the previous end-of-file and the
  234. * newly-written bytes are unspecified. </p>
  235. *
  236. * @param newPosition
  237. * The new position, a non-negative integer counting
  238. * the number of bytes from the beginning of the file
  239. *
  240. * @return This file channel
  241. *
  242. * @throws ClosedChannelException
  243. * If this channel is closed
  244. *
  245. * @throws IllegalArgumentException
  246. * If the new position is negative
  247. *
  248. * @throws IOException
  249. * If some other I/O error occurs
  250. */
  251. public abstract FileChannel position(long newPosition) throws IOException;
  252. /**
  253. * Returns the current size of this channel's file. </p>
  254. *
  255. * @return The current size of this channel's file,
  256. * measured in bytes
  257. *
  258. * @throws ClosedChannelException
  259. * If this channel is closed
  260. *
  261. * @throws IOException
  262. * If some other I/O error occurs
  263. */
  264. public abstract long size() throws IOException;
  265. /**
  266. * Truncates this channel's file to the given size.
  267. *
  268. * <p> If the given size is less than the file's current size then the file
  269. * is truncated, discarding any bytes beyond the new end of the file. If
  270. * the given size is greater than or equal to the file's current size then
  271. * the file is not modified. In either case, if this channel's file
  272. * position is greater than the given size then it is set to that size.
  273. * </p>
  274. *
  275. * @param size
  276. * The new size, a non-negative byte count
  277. *
  278. * @return This file channel
  279. *
  280. * @throws NonWritableChannelException
  281. * If this channel was not opened for writing
  282. *
  283. * @throws ClosedChannelException
  284. * If this channel is closed
  285. *
  286. * @throws IllegalArgumentException
  287. * If the new size is negative
  288. *
  289. * @throws IOException
  290. * If some other I/O error occurs
  291. */
  292. public abstract FileChannel truncate(long size) throws IOException;
  293. /**
  294. * Forces any updates to this channel's file to be written to the storage
  295. * device that contains it.
  296. *
  297. * <p> If this channel's file resides on a local storage device then when
  298. * this method returns it is guaranteed that all changes made to the file
  299. * since this channel was created, or since this method was last invoked,
  300. * will have been written to that device. This is useful for ensuring that
  301. * critical information is not lost in the event of a system crash.
  302. *
  303. * <p> If the file does not reside on a local device then no such guarantee
  304. * is made.
  305. *
  306. * <p> The <tt>metaData</tt> parameter can be used to limit the number of
  307. * I/O operations that this method is required to perform. Passing
  308. * <tt>false</tt> for this parameter indicates that only updates to the
  309. * file's content need be written to storage; passing <tt>true</tt>
  310. * indicates that updates to both the file's content and metadata must be
  311. * written, which generally requires at least one more I/O operation.
  312. * Whether this parameter actually has any effect is dependent upon the
  313. * underlying operating system and is therefore unspecified.
  314. *
  315. * <p> Invoking this method may cause an I/O operation to occur even if the
  316. * channel was only opened for reading. Some operating systems, for
  317. * example, maintain a last-access time as part of a file's metadata, and
  318. * this time is updated whenever the file is read. Whether or not this is
  319. * actually done is system-dependent and is therefore unspecified.
  320. *
  321. * <p> This method is only guaranteed to force changes that were made to
  322. * this channel's file via the methods defined in this class. It may or
  323. * may not force changes that were made by modifying the content of a
  324. * {@link MappedByteBuffer </code>mapped byte buffer<code>} obtained by
  325. * invoking the {@link #map map} method. Invoking the {@link
  326. * MappedByteBuffer#force force} method of the mapped byte buffer will
  327. * force changes made to the buffer's content to be written. </p>
  328. *
  329. * @param metaData
  330. * If <tt>true</tt> then this method is required to force changes
  331. * to both the file's content and metadata to be written to
  332. * storage; otherwise, it need only force content changes to be
  333. * written
  334. *
  335. * @throws ClosedChannelException
  336. * If this channel is closed
  337. *
  338. * @throws IOException
  339. * If some other I/O error occurs
  340. */
  341. public abstract void force(boolean metaData) throws IOException;
  342. /**
  343. * Transfers bytes from this channel's file to the given writable byte
  344. * channel.
  345. *
  346. * <p> An attempt is made to read up to <tt>count</tt> bytes starting at
  347. * the given <tt>position</tt> in this channel's file and write them to the
  348. * target channel. An invocation of this method may or may not transfer
  349. * all of the requested bytes; whether or not it does so depends upon the
  350. * natures and states of the channels. Fewer than the requested number of
  351. * bytes are transferred if this channel's file contains fewer than
  352. * <tt>count</tt> bytes starting at the given <tt>position</tt>, or if the
  353. * target channel is non-blocking and it has fewer than <tt>count</tt>
  354. * bytes free in its output buffer.
  355. *
  356. * <p> This method does not modify this channel's position. If the given
  357. * position is greater than the file's current size then no bytes are
  358. * transferred. If the target channel has a position then bytes are
  359. * written starting at that position and then the position is incremented
  360. * by the number of bytes written.
  361. *
  362. * <p> This method is potentially much more efficient than a simple loop
  363. * that reads from this channel and writes to the target channel. Many
  364. * operating systems can transfer bytes directly from the filesystem cache
  365. * to the target channel without actually copying them. </p>
  366. *
  367. * @param position
  368. * The position within the file at which the transfer is to begin;
  369. * must be non-negative
  370. *
  371. * @param count
  372. * The maximum number of bytes to be transferred; must be
  373. * non-negative
  374. *
  375. * @param target
  376. * The target channel
  377. *
  378. * @return The number of bytes, possibly zero,
  379. * that were actually transferred
  380. *
  381. * @throws IllegalArgumentException
  382. * If the preconditions on the parameters do not hold
  383. *
  384. * @throws NonReadableChannelException
  385. * If this channel was not opened for reading
  386. *
  387. * @throws NonWritableChannelException
  388. * If the target channel was not opened for writing
  389. *
  390. * @throws ClosedChannelException
  391. * If either this channel or the target channel is closed
  392. *
  393. * @throws AsynchronousCloseException
  394. * If another thread closes either channel
  395. * while the transfer is in progress
  396. *
  397. * @throws ClosedByInterruptException
  398. * If another thread interrupts the current thread while the
  399. * transfer is in progress, thereby closing both channels and
  400. * setting the current thread's interrupt status
  401. *
  402. * @throws IOException
  403. * If some other I/O error occurs
  404. */
  405. public abstract long transferTo(long position, long count,
  406. WritableByteChannel target)
  407. throws IOException;
  408. /**
  409. * Transfers bytes into this channel's file from the given readable byte
  410. * channel.
  411. *
  412. * <p> An attempt is made to read up to <tt>count</tt> bytes from the
  413. * source channel and write them to this channel's file starting at the
  414. * given <tt>position</tt>. An invocation of this method may or may not
  415. * transfer all of the requested bytes; whether or not it does so depends
  416. * upon the natures and states of the channels. Fewer than the requested
  417. * number of bytes will be transferred if the source channel has fewer than
  418. * <tt>count</tt> bytes remaining, or if the source channel is non-blocking
  419. * and has fewer than <tt>count</tt> bytes immediately available in its
  420. * input buffer.
  421. *
  422. * <p> This method does not modify this channel's position. If the given
  423. * position is greater than the file's current size then no bytes are
  424. * transferred. If the source channel has a position then bytes are read
  425. * starting at that position and then the position is incremented by the
  426. * number of bytes read.
  427. *
  428. * <p> This method is potentially much more efficient than a simple loop
  429. * that reads from the source channel and writes to this channel. Many
  430. * operating systems can transfer bytes directly from the source channel
  431. * into the filesystem cache without actually copying them. </p>
  432. *
  433. * @param src
  434. * The source channel
  435. *
  436. * @param position
  437. * The position within the file at which the transfer is to begin;
  438. * must be non-negative
  439. *
  440. * @param count
  441. * The maximum number of bytes to be transferred; must be
  442. * non-negative
  443. *
  444. * @return The number of bytes, possibly zero,
  445. * that were actually transferred
  446. *
  447. * @throws IllegalArgumentException
  448. * If the preconditions on the parameters do not hold
  449. *
  450. * @throws NonReadableChannelException
  451. * If the source channel was not opened for reading
  452. *
  453. * @throws NonWritableChannelException
  454. * If this channel was not opened for writing
  455. *
  456. * @throws ClosedChannelException
  457. * If either this channel or the source channel is closed
  458. *
  459. * @throws AsynchronousCloseException
  460. * If another thread closes either channel
  461. * while the transfer is in progress
  462. *
  463. * @throws ClosedByInterruptException
  464. * If another thread interrupts the current thread while the
  465. * transfer is in progress, thereby closing both channels and
  466. * setting the current thread's interrupt status
  467. *
  468. * @throws IOException
  469. * If some other I/O error occurs
  470. */
  471. public abstract long transferFrom(ReadableByteChannel src,
  472. long position, long count)
  473. throws IOException;
  474. /**
  475. * Reads a sequence of bytes from this channel into the given buffer,
  476. * starting at the given file position.
  477. *
  478. * <p> This method works in the same manner as the {@link
  479. * #read(ByteBuffer)} method, except that bytes are read starting at the
  480. * given file position rather than at the channel's current position. This
  481. * method does not modify this channel's position. If the given position
  482. * is greater than the file's current size then no bytes are read. </p>
  483. *
  484. * @param dst
  485. * The buffer into which bytes are to be transferred
  486. *
  487. * @param position
  488. * The file position at which the transfer is to begin;
  489. * must be non-negative
  490. *
  491. * @return The number of bytes read, possibly zero, or <tt>-1</tt> if the
  492. * given position is greater than or equal to the file's current
  493. * size
  494. *
  495. * @throws IllegalArgumentException
  496. * If the position is negative
  497. *
  498. * @throws NonReadableChannelException
  499. * If this channel was not opened for reading
  500. *
  501. * @throws ClosedChannelException
  502. * If this channel is closed
  503. *
  504. * @throws AsynchronousCloseException
  505. * If another thread closes this channel
  506. * while the read operation is in progress
  507. *
  508. * @throws ClosedByInterruptException
  509. * If another thread interrupts the current thread
  510. * while the read operation is in progress, thereby
  511. * closing the channel and setting the current thread's
  512. * interrupt status
  513. *
  514. * @throws IOException
  515. * If some other I/O error occurs
  516. */
  517. public abstract int read(ByteBuffer dst, long position) throws IOException;
  518. /**
  519. * Writes a sequence of bytes to this channel from the given buffer,
  520. * starting at the given file position.
  521. *
  522. * <p> This method works in the same manner as the {@link
  523. * #write(ByteBuffer)} method, except that bytes are written starting at
  524. * the given file position rather than at the channel's current position.
  525. * This method does not modify this channel's position. If the given
  526. * position is greater than the file's current size then the file will be
  527. * grown to accommodate the new bytes; the values of any bytes between the
  528. * previous end-of-file and the newly-written bytes are unspecified. </p>
  529. *
  530. * @param src
  531. * The buffer from which bytes are to be transferred
  532. *
  533. * @param position
  534. * The file position at which the transfer is to begin;
  535. * must be non-negative
  536. *
  537. * @return The number of bytes written, possibly zero
  538. *
  539. * @throws IllegalArgumentException
  540. * If the position is negative
  541. *
  542. * @throws NonWritableChannelException
  543. * If this channel was not opened for writing
  544. *
  545. * @throws ClosedChannelException
  546. * If this channel is closed
  547. *
  548. * @throws AsynchronousCloseException
  549. * If another thread closes this channel
  550. * while the write operation is in progress
  551. *
  552. * @throws ClosedByInterruptException
  553. * If another thread interrupts the current thread
  554. * while the write operation is in progress, thereby
  555. * closing the channel and setting the current thread's
  556. * interrupt status
  557. *
  558. * @throws IOException
  559. * If some other I/O error occurs
  560. */
  561. public abstract int write(ByteBuffer src, long position) throws IOException;
  562. // -- Memory-mapped buffers --
  563. /**
  564. * A typesafe enumeration for file-mapping modes.
  565. *
  566. * @version 1.40, 04/01/12
  567. * @since 1.4
  568. *
  569. * @see java.nio.channels.FileChannel#map
  570. */
  571. public static class MapMode {
  572. /**
  573. * Mode for a read-only mapping.
  574. */
  575. public static final MapMode READ_ONLY
  576. = new MapMode("READ_ONLY");
  577. /**
  578. * Mode for a read/write mapping.
  579. */
  580. public static final MapMode READ_WRITE
  581. = new MapMode("READ_WRITE");
  582. /**
  583. * Mode for a private (copy-on-write) mapping.
  584. */
  585. public static final MapMode PRIVATE
  586. = new MapMode("PRIVATE");
  587. private final String name;
  588. private MapMode(String name) {
  589. this.name = name;
  590. }
  591. /**
  592. * Returns a string describing this file-mapping mode.
  593. *
  594. * @return A descriptive string
  595. */
  596. public String toString() {
  597. return name;
  598. }
  599. }
  600. /**
  601. * Maps a region of this channel's file directly into memory.
  602. *
  603. * <p> A region of a file may be mapped into memory in one of three modes:
  604. * </p>
  605. *
  606. * <ul type=disc>
  607. *
  608. * <li><p> <i>Read-only:</i> Any attempt to modify the resulting buffer
  609. * will cause a {@link java.nio.ReadOnlyBufferException} to be thrown.
  610. * ({@link MapMode#READ_ONLY MapMode.READ_ONLY}) </p></li>
  611. *
  612. * <li><p> <i>Read/write:</i> Changes made to the resulting buffer will
  613. * eventually be propagated to the file; they may or may not be made
  614. * visible to other programs that have mapped the same file. ({@link
  615. * MapMode#READ_WRITE MapMode.READ_WRITE}) </p></li>
  616. *
  617. * <li><p> <i>Private:</i> Changes made to the resulting buffer will not
  618. * be propagated to the file and will not be visible to other programs
  619. * that have mapped the same file; instead, they will cause private
  620. * copies of the modified portions of the buffer to be created. ({@link
  621. * MapMode#PRIVATE MapMode.PRIVATE}) </p></li>
  622. *
  623. * </ul>
  624. *
  625. * <p> For a read-only mapping, this channel must have been opened for
  626. * reading; for a read/write or private mapping, this channel must have
  627. * been opened for both reading and writing.
  628. *
  629. * <p> The {@link MappedByteBuffer </code>mapped byte buffer<code>}
  630. * returned by this method will have a position of zero and a limit and
  631. * capacity of <tt>size</tt> its mark will be undefined. The buffer and
  632. * the mapping that it represents will remain valid until the buffer itself
  633. * is garbage-collected.
  634. *
  635. * <p> A mapping, once established, is not dependent upon the file channel
  636. * that was used to create it. Closing the channel, in particular, has no
  637. * effect upon the validity of the mapping.
  638. *
  639. * <p> Many of the details of memory-mapped files are inherently dependent
  640. * upon the underlying operating system and are therefore unspecified. The
  641. * behavior of this method when the requested region is not completely
  642. * contained within this channel's file is unspecified. Whether changes
  643. * made to the content or size of the underlying file, by this program or
  644. * another, are propagated to the buffer is unspecified. The rate at which
  645. * changes to the buffer are propagated to the file is unspecified.
  646. *
  647. * <p> For most operating systems, mapping a file into memory is more
  648. * expensive than reading or writing a few tens of kilobytes of data via
  649. * the usual {@link #read read} and {@link #write write} methods. From the
  650. * standpoint of performance it is generally only worth mapping relatively
  651. * large files into memory. </p>
  652. *
  653. * @param mode
  654. * One of the constants {@link MapMode#READ_ONLY READ_ONLY}, {@link
  655. * MapMode#READ_WRITE READ_WRITE}, or {@link MapMode#PRIVATE
  656. * PRIVATE} defined in the {@link MapMode} class, according to
  657. * whether the file is to be mapped read-only, read/write, or
  658. * privately (copy-on-write), respectively
  659. *
  660. * @param position
  661. * The position within the file at which the mapped region
  662. * is to start; must be non-negative
  663. *
  664. * @param size
  665. * The size of the region to be mapped; must be non-negative and
  666. * no greater than {@link java.lang.Integer#MAX_VALUE}
  667. *
  668. * @throws NonReadableChannelException
  669. * If the <tt>mode</tt> is {@link MapMode#READ_ONLY READ_ONLY} but
  670. * this channel was not opened for reading
  671. *
  672. * @throws NonWritableChannelException
  673. * If the <tt>mode</tt> is {@link MapMode#READ_WRITE READ_WRITE} or
  674. * {@link MapMode#PRIVATE PRIVATE} but this channel was not opened
  675. * for both reading and writing
  676. *
  677. * @throws IllegalArgumentException
  678. * If the preconditions on the parameters do not hold
  679. *
  680. * @throws IOException
  681. * If some other I/O error occurs
  682. *
  683. * @see java.nio.channels.FileChannel.MapMode
  684. * @see java.nio.MappedByteBuffer
  685. */
  686. public abstract MappedByteBuffer map(MapMode mode,
  687. long position, long size)
  688. throws IOException;
  689. // -- Locks --
  690. /**
  691. * Acquires a lock on the given region of this channel's file.
  692. *
  693. * <p> An invocation of this method will block until the region can be
  694. * locked, this channel is closed, or the invoking thread is interrupted,
  695. * whichever comes first.
  696. *
  697. * <p> If this channel is closed by another thread during an invocation of
  698. * this method then an {@link AsynchronousCloseException} will be thrown.
  699. *
  700. * <p> If the invoking thread is interrupted while waiting to acquire the
  701. * lock then its interrupt status will be set and a {@link
  702. * FileLockInterruptionException} will be thrown. If the invoker's
  703. * interrupt status is set when this method is invoked then that exception
  704. * will be thrown immediately; the thread's interrupt status will not be
  705. * changed.
  706. *
  707. * <p> The region specified by the <tt>position</tt> and <tt>size</tt>
  708. * parameters need not be contained within, or even overlap, the actual
  709. * underlying file. Lock regions are fixed in size; if a locked region
  710. * initially contains the end of the file and the file grows beyond the
  711. * region then the new portion of the file will not be covered by the lock.
  712. * If a file is expected to grow in size and a lock on the entire file is
  713. * required then a region starting at zero, and no smaller than the
  714. * expected maximum size of the file, should be locked. The zero-argument
  715. * {@link #lock()} method simply locks a region of size {@link
  716. * Long#MAX_VALUE}.
  717. *
  718. * <p> Some operating systems do not support shared locks, in which case a
  719. * request for a shared lock is automatically converted into a request for
  720. * an exclusive lock. Whether the newly-acquired lock is shared or
  721. * exclusive may be tested by invoking the resulting lock object's {@link
  722. * FileLock#isShared() isShared} method.
  723. *
  724. * <p> File locks are held on behalf of the entire Java virtual machine.
  725. * They are not suitable for controlling access to a file by multiple
  726. * threads within the same virtual machine. </p>
  727. *
  728. * @param position
  729. * The position at which the locked region is to start; must be
  730. * non-negative
  731. *
  732. * @param size
  733. * The size of the locked region; must be non-negative, and the sum
  734. * <tt>position</tt> + <tt>size</tt> must be non-negative
  735. *
  736. * @param shared
  737. * <tt>true</tt> to request a shared lock, in which case this
  738. * channel must be open for reading (and possibly writing);
  739. * <tt>false</tt> to request an exclusive lock, in which case this
  740. * channel must be open for writing (and possibly reading)
  741. *
  742. * @return A lock object representing the newly-acquired lock
  743. *
  744. * @throws IllegalArgumentException
  745. * If the preconditions on the parameters do not hold
  746. *
  747. * @throws ClosedChannelException
  748. * If this channel is closed
  749. *
  750. * @throws AsynchronousCloseException
  751. * If another thread closes this channel while the invoking
  752. * thread is blocked in this method
  753. *
  754. * @throws FileLockInterruptionException
  755. * If the invoking thread is interrupted while blocked in this
  756. * method
  757. *
  758. * @throws OverlappingFileLockException
  759. * If a lock that overlaps the requested region is already held by
  760. * this Java virtual machine, or if another thread is already
  761. * blocked in this method and is attempting to lock an overlapping
  762. * region
  763. *
  764. * @throws NonReadableChannelException
  765. * If <tt>shared</tt> is <tt>true</tt> this channel was not
  766. * opened for reading
  767. *
  768. * @throws NonWritableChannelException
  769. * If <tt>shared</tt> is <tt>false</tt> but this channel was not
  770. * opened for writing
  771. *
  772. * @throws IOException
  773. * If some other I/O error occurs
  774. *
  775. * @see #lock()
  776. * @see #tryLock()
  777. * @see #tryLock(long,long,boolean)
  778. */
  779. public abstract FileLock lock(long position, long size, boolean shared)
  780. throws IOException;
  781. /**
  782. * Acquires an exclusive lock on this channel's file.
  783. *
  784. * <p> An invocation of this method of the form <tt>fc.lock()</tt> behaves
  785. * in exactly the same way as the invocation
  786. *
  787. * <pre>
  788. * fc.{@link #lock(long,long,boolean) lock}(0L, Long.MAX_VALUE, false) </pre>
  789. *
  790. * @return A lock object representing the newly-acquired lock
  791. *
  792. * @throws ClosedChannelException
  793. * If this channel is closed
  794. *
  795. * @throws AsynchronousCloseException
  796. * If another thread closes this channel while the invoking
  797. * thread is blocked in this method
  798. *
  799. * @throws FileLockInterruptionException
  800. * If the invoking thread is interrupted while blocked in this
  801. * method
  802. *
  803. * @throws OverlappingFileLockException
  804. * If a lock that overlaps the requested region is already held by
  805. * this Java virtual machine, or if another thread is already
  806. * blocked in this method and is attempting to lock an overlapping
  807. * region of the same file
  808. *
  809. * @throws NonReadableChannelException
  810. * If <tt>shared</tt> is <tt>true</tt> this channel was not
  811. * opened for reading
  812. *
  813. * @throws NonWritableChannelException
  814. * If <tt>shared</tt> is <tt>false</tt> but this channel was not
  815. * opened for writing
  816. *
  817. * @throws IOException
  818. * If some other I/O error occurs
  819. *
  820. * @see #lock(long,long,boolean)
  821. * @see #tryLock()
  822. * @see #tryLock(long,long,boolean)
  823. */
  824. public final FileLock lock() throws IOException {
  825. return lock(0L, Long.MAX_VALUE, false);
  826. }
  827. /**
  828. * Attempts to acquire a lock on the given region of this channel's file.
  829. *
  830. * <p> This method does not block. An invocation of this always returns
  831. * immediately, either having acquired a lock on the requested region or
  832. * having failed to do so. If it fails to acquire a lock because an
  833. * overlapping lock is held by another program then it returns
  834. * <tt>null</tt>. If it fails to acquire a lock for any other reason then
  835. * an appropriate exception is thrown.
  836. *
  837. * <p> The region specified by the <tt>position</tt> and <tt>size</tt>
  838. * parameters need not be contained within, or even overlap, the actual
  839. * underlying file. Lock regions are fixed in size; if a locked region
  840. * initially contains the end of the file and the file grows beyond the
  841. * region then the new portion of the file will not be covered by the lock.
  842. * If a file is expected to grow in size and a lock on the entire file is
  843. * required then a region starting at zero, and no smaller than the
  844. * expected maximum size of the file, should be locked. The zero-argument
  845. * {@link #tryLock()} method simply locks a region of size {@link
  846. * Long#MAX_VALUE}.
  847. *
  848. * <p> Some operating systems do not support shared locks, in which case a
  849. * request for a shared lock is automatically converted into a request for
  850. * an exclusive lock. Whether the newly-acquired lock is shared or
  851. * exclusive may be tested by invoking the resulting lock object's {@link
  852. * FileLock#isShared() isShared} method.
  853. *
  854. * <p> File locks are held on behalf of the entire Java virtual machine.
  855. * They are not suitable for controlling access to a file by multiple
  856. * threads within the same virtual machine. </p>
  857. *
  858. * @param position
  859. * The position at which the locked region is to start; must be
  860. * non-negative
  861. *
  862. * @param size
  863. * The size of the locked region; must be non-negative, and the sum
  864. * <tt>position</tt> + <tt>size</tt> must be non-negative
  865. *
  866. * @param shared
  867. * <tt>true</tt> to request a shared lock,
  868. * <tt>false</tt> to request an exclusive lock
  869. *
  870. * @return A lock object representing the newly-acquired lock,
  871. * or <tt>null</tt> if the lock could not be acquired
  872. * because another program holds an overlapping lock
  873. *
  874. * @throws IllegalArgumentException
  875. * If the preconditions on the parameters do not hold
  876. *
  877. * @throws ClosedChannelException
  878. * If this channel is closed
  879. *
  880. * @throws OverlappingFileLockException
  881. * If a lock that overlaps the requested region is already held by
  882. * this Java virtual machine, or if another thread is already
  883. * blocked in this method and is attempting to lock an overlapping
  884. * region of the same file
  885. *
  886. * @throws IOException
  887. * If some other I/O error occurs
  888. *
  889. * @see #lock()
  890. * @see #lock(long,long,boolean)
  891. * @see #tryLock()
  892. */
  893. public abstract FileLock tryLock(long position, long size, boolean shared)
  894. throws IOException;
  895. /**
  896. * Attempts to acquire an exclusive lock on this channel's file.
  897. *
  898. * <p> An invocation of this method of the form <tt>fc.tryLock()</tt>
  899. * behaves in exactly the same way as the invocation
  900. *
  901. * <pre>
  902. * fc.{@link #tryLock(long,long,boolean) tryLock}(0L, Long.MAX_VALUE, false) </pre>
  903. *
  904. * @return A lock object representing the newly-acquired lock,
  905. * or <tt>null</tt> if the lock could not be acquired
  906. * because another program holds an overlapping lock
  907. *
  908. * @throws ClosedChannelException
  909. * If this channel is closed
  910. *
  911. * @throws OverlappingFileLockException
  912. * If a lock that overlaps the requested region is already held by
  913. * this Java virtual machine, or if another thread is already
  914. * blocked in this method and is attempting to lock an overlapping
  915. * region
  916. *
  917. * @throws IOException
  918. * If some other I/O error occurs
  919. *
  920. * @see #lock()
  921. * @see #lock(long,long,boolean)
  922. * @see #tryLock(long,long,boolean)
  923. */
  924. public final FileLock tryLock() throws IOException {
  925. return tryLock(0L, Long.MAX_VALUE, false);
  926. }
  927. }