FileChannel.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /* FileChannel.java --
  2. Copyright (C) 2002 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.nio.channels;
  32. import java.io.IOException;
  33. import java.nio.ByteBuffer;
  34. import java.nio.MappedByteBuffer;
  35. import java.nio.channels.spi.AbstractInterruptibleChannel;
  36. /**
  37. * @author Michael Koch
  38. * @since 1.4
  39. */
  40. public abstract class FileChannel extends AbstractInterruptibleChannel
  41. implements ByteChannel, GatheringByteChannel, ScatteringByteChannel
  42. {
  43. public static class MapMode
  44. {
  45. int m;
  46. public static final MapMode READ_ONLY = new MapMode(0);
  47. public static final MapMode READ_WRITE = new MapMode(1);
  48. public static final MapMode PRIVATE = new MapMode(2);
  49. /**
  50. * Initializes the MapMode.
  51. */
  52. MapMode(int a)
  53. {
  54. m = a;
  55. }
  56. /**
  57. * Returns a string representation of the <code>MapMode</code> object.
  58. */
  59. public String toString()
  60. {
  61. if (this == READ_ONLY)
  62. return "READ_ONLY";
  63. else if (this == READ_WRITE)
  64. return "READ_WRITE";
  65. return "PRIVATE";
  66. }
  67. }
  68. /**
  69. * Initializes the channel.
  70. */
  71. protected FileChannel()
  72. {
  73. }
  74. /**
  75. * Maps the file into the memory.
  76. *
  77. * @exception IllegalArgumentException If the preconditions on the parameters
  78. * do not hold.
  79. * @exception IOException If an I/O error occurs.
  80. * @exception NonReadableChannelException If mode is READ_ONLY but this channel was
  81. * not opened for reading.
  82. * @exception NonWritableChannelException If mode is READ_WRITE or PRIVATE but this
  83. * channel was not opened for writing.
  84. */
  85. public abstract MappedByteBuffer map(MapMode mode, long position, long size)
  86. throws IOException;
  87. /**
  88. * Return the size of the file thus far
  89. *
  90. * @exception ClosedChannelException If this channel is closed.
  91. */
  92. public abstract long size() throws IOException;
  93. /**
  94. * Writes data to the channel.
  95. *
  96. * @exception IOException If an I/O error occurs.
  97. */
  98. public final long write(ByteBuffer[] srcs) throws IOException
  99. {
  100. return write(srcs, 0, srcs.length);
  101. }
  102. /**
  103. * Writes data to the channel.
  104. *
  105. * @exception IOException If an I/O error occurs.
  106. */
  107. public abstract int write(ByteBuffer src) throws IOException;
  108. /**
  109. * Writes data to the channel.
  110. *
  111. * @exception AsynchronousCloseException If another thread closes this channel
  112. * while the transfer is in progress.
  113. * @exception ClosedByInterruptException If another thread interrupts the
  114. * current thread while the transfer is in progress, thereby closing both
  115. * channels and setting the current thread's interrupt status.
  116. * @exception ClosedChannelException If this channel is closed.
  117. * @exception IllegalArgumentException If position is negative.
  118. * @exception IOException If an I/O error occurs.
  119. * @exception NonWritableChannelException If this channel was not opened for
  120. * writing.
  121. */
  122. public abstract int write(ByteBuffer srcs, long position)
  123. throws IOException;
  124. /**
  125. * Writes data to the channel.
  126. *
  127. * @exception IOException If an I/O error occurs.
  128. */
  129. public abstract long write(ByteBuffer[] srcs, int offset, int length)
  130. throws IOException;
  131. /**
  132. * Reads data from the channel.
  133. *
  134. * @exception IOException If an I/O error occurs.
  135. */
  136. public abstract long read(ByteBuffer[] dsts, int offset, int length)
  137. throws IOException;
  138. /**
  139. * Reads data from the channel.
  140. *
  141. * @exception IOException If an I/O error occurs.
  142. */
  143. public final long read(ByteBuffer[] dsts) throws IOException
  144. {
  145. return read(dsts, 0, dsts.length);
  146. }
  147. /**
  148. * Reads data from the channel.
  149. *
  150. * @exception IOException If an I/O error occurs.
  151. */
  152. public abstract int read(ByteBuffer dst) throws IOException;
  153. /**
  154. * Reads data from the channel.
  155. *
  156. * @exception AsynchronousCloseException If another thread closes this channel
  157. * while the transfer is in progress.
  158. * @exception ClosedByInterruptException If another thread interrupts the
  159. * current thread while the transfer is in progress, thereby closing both
  160. * channels and setting the current thread's interrupt status.
  161. * @exception ClosedChannelException If this channel is closed.
  162. * @exception IllegalArgumentException If position is negative.
  163. * @exception IOException If an I/O error occurs.
  164. * @exception NonReadableChannelException If this channel was not opened for
  165. * reading.
  166. */
  167. public abstract int read(ByteBuffer dst, long position)
  168. throws IOException;
  169. /**
  170. * Closes the channel.
  171. *
  172. * This is called from @see close.
  173. *
  174. * @exception IOException If an I/O error occurs.
  175. */
  176. protected abstract void implCloseChannel() throws IOException;
  177. /**
  178. * msync with the disk
  179. *
  180. * @exception ClosedChannelException If this channel is closed.
  181. * @exception IOException If an I/O error occurs.
  182. */
  183. public abstract void force(boolean metaData) throws IOException;
  184. /**
  185. * Creates a file lock for the whole associated file.
  186. *
  187. * @exception AsynchronousCloseException If another thread closes this channel
  188. * while the transfer is in progress.
  189. * @exception ClosedChannelException If this channel is closed.
  190. * @exception FileLockInterruptionException If the invoking thread is
  191. * interrupted while blocked in this method.
  192. * @exception IOException If an I/O error occurs.
  193. * @exception NonReadableChannelException If shared is true and this channel
  194. * was not opened for reading.
  195. * @exception NonWritableChannelException If shared is false and this channel
  196. * was not opened for writing.
  197. * @exception OverlappingFileLockException If a lock that overlaps the
  198. * requested region is already held by this Java virtual machine, or if
  199. * another thread is already blocked in this method and is attempting to lock
  200. * an overlapping region.
  201. */
  202. public final FileLock lock() throws IOException
  203. {
  204. return lock(0, Long.MAX_VALUE, false);
  205. }
  206. /**
  207. * Creates a file lock for a region of the associated file.
  208. *
  209. * @exception AsynchronousCloseException If another thread closes this channel
  210. * while the transfer is in progress.
  211. * @exception ClosedChannelException If this channel is closed.
  212. * @exception FileLockInterruptionException If the invoking thread is
  213. * interrupted while blocked in this method.
  214. * @exception IllegalArgumentException If the preconditions on the parameters
  215. * do not hold.
  216. * @exception IOException If an I/O error occurs.
  217. * @exception OverlappingFileLockException If a lock that overlaps the
  218. * requested region is already held by this Java virtual machine, or if
  219. * another thread is already blocked in this method and is attempting to lock
  220. * an overlapping region.
  221. * @exception NonReadableChannelException If shared is true and this channel
  222. * was not opened for reading.
  223. * @exception NonWritableChannelException If shared is false and this channel
  224. * was not opened for writing.
  225. */
  226. public abstract FileLock lock(long position, long size, boolean shared)
  227. throws IOException;
  228. /**
  229. * Tries to aqquire alock on the whole associated file.
  230. *
  231. * @exception ClosedChannelException If this channel is closed.
  232. * @exception IOException If an I/O error occurs.
  233. * @exception OverlappingFileLockException If a lock that overlaps the
  234. * requested region is already held by this Java virtual machine, or if
  235. * another thread is already blocked in this method and is attempting to lock
  236. * an overlapping region.
  237. */
  238. public final FileLock tryLock() throws IOException
  239. {
  240. return tryLock(0, Long.MAX_VALUE, false);
  241. }
  242. /**
  243. * Tries to aqquire a lock on a region of the associated file.
  244. *
  245. * @exception ClosedChannelException If this channel is closed.
  246. * @exception IllegalArgumentException If the preconditions on the parameters
  247. * do not hold.
  248. * @exception IOException If an I/O error occurs.
  249. * @exception OverlappingFileLockException If a lock that overlaps the
  250. * requested region is already held by this Java virtual machine, or if
  251. * another thread is already blocked in this method and is attempting to lock
  252. * an overlapping region.
  253. */
  254. public abstract FileLock tryLock(long position, long size, boolean shared)
  255. throws IOException;
  256. /**
  257. * Returns the current position on the file.
  258. *
  259. * @exception ClosedChannelException If this channel is closed.
  260. * @exception IOException If an I/O error occurs.
  261. */
  262. public abstract long position() throws IOException;
  263. /**
  264. * Sets the position of the channel on the assoziated file.
  265. *
  266. * @exception ClosedChannelException If this channel is closed.
  267. * @exception IllegalArgumentException If newPosition is negative.
  268. * @exception IOException If an I/O error occurs.
  269. */
  270. public abstract FileChannel position(long newPosition)
  271. throws IOException;
  272. /**
  273. * Transfers bytes from this channel's file to the given writable byte
  274. * channel.
  275. *
  276. * @exception AsynchronousCloseException If another thread closes this channel
  277. * while the transfer is in progress.
  278. * @exception ClosedByInterruptException If another thread interrupts the
  279. * current thread while the transfer is in progress, thereby closing both
  280. * channels and setting the current thread's interrupt status.
  281. * @exception ClosedChannelException If this channel is closed.
  282. * @exception IllegalArgumentException If the preconditions on the parameters
  283. * do not hold.
  284. * @exception IOException If an I/O error occurs.
  285. * @exception NonReadableChannelException If this channel was not opened for
  286. * reading.
  287. * @exception NonWritableChannelException If the target channel was not
  288. * opened for writing.
  289. */
  290. public abstract long transferTo(long position, long count,
  291. WritableByteChannel target)
  292. throws IOException;
  293. /**
  294. * Transfers bytes from the given readable channel into this channel.
  295. *
  296. * @exception AsynchronousCloseException If another thread closes this channel
  297. * while the transfer is in progress.
  298. * @exception ClosedByInterruptException If another thread interrupts the
  299. * current thread while the transfer is in progress, thereby closing both
  300. * channels and setting the current thread's interrupt status.
  301. * @exception ClosedChannelException If this channel is closed.
  302. * @exception IllegalArgumentException If the preconditions on the parameters
  303. * do not hold.
  304. * @exception IOException If an I/O error occurs.
  305. * @exception NonReadableChannelException If the source channel was not
  306. * opened for reading.
  307. * @exception NonWritableChannelException If this channel was not opened for
  308. * writing.
  309. */
  310. public abstract long transferFrom(ReadableByteChannel src, long position,
  311. long count) throws IOException;
  312. /**
  313. * Truncates the channel's file at <code>size</code>.
  314. *
  315. * @exception ClosedChannelException If this channel is closed.
  316. * @exception IllegalArgumentException If size is negative.
  317. * @exception IOException If an I/O error occurs.
  318. * @exception NonWritableChannelException If this channel was not opened for
  319. * writing.
  320. */
  321. public abstract FileChannel truncate(long size) throws IOException;
  322. }