FileChannel.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 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. public int m;
  46. public static MapMode READ_ONLY = new MapMode(0);
  47. public static MapMode READ_WRITE = new MapMode(1);
  48. public static 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 long write (ByteBuffer[] srcs) throws IOException
  99. {
  100. long result = 0;
  101. for (int i = 0; i < srcs.length; i++)
  102. {
  103. result += write (srcs[i]);
  104. }
  105. return result;
  106. }
  107. /**
  108. * Writes data to the channel.
  109. *
  110. * @exception IOException If an I/O error occurs.
  111. */
  112. public abstract int write (ByteBuffer src) throws IOException;
  113. /**
  114. * Writes data to the channel.
  115. *
  116. * @exception AsynchronousCloseException If another thread closes this channel
  117. * while the transfer is in progress.
  118. * @exception ClosedByInterruptException If another thread interrupts the
  119. * current thread while the transfer is in progress, thereby closing both
  120. * channels and setting the current thread's interrupt status.
  121. * @exception ClosedChannelException If this channel is closed.
  122. * @exception IllegalArgumentException If position is negative.
  123. * @exception IOException If an I/O error occurs.
  124. * @exception NonWritableChannelException If this channel was not opened for
  125. * writing.
  126. */
  127. public abstract int write (ByteBuffer srcs, long position) throws IOException;
  128. /**
  129. * Writes data to the channel.
  130. *
  131. * @exception IOException If an I/O error occurs.
  132. */
  133. public abstract long write(ByteBuffer[] srcs, int offset, int length)
  134. throws IOException;
  135. /**
  136. * Reads data from the channel.
  137. *
  138. * @exception IOException If an I/O error occurs.
  139. */
  140. public abstract long read (ByteBuffer[] dsts, int offset, int length)
  141. throws IOException;
  142. /**
  143. * Reads data from the channel.
  144. *
  145. * @exception IOException If an I/O error occurs.
  146. */
  147. public final long read (ByteBuffer[] dsts) throws IOException
  148. {
  149. long result = 0;
  150. for (int i = 0; i < dsts.length; i++)
  151. {
  152. read (dsts [i]);
  153. }
  154. return result;
  155. }
  156. /**
  157. * Reads data from the channel.
  158. *
  159. * @exception IOException If an I/O error occurs.
  160. */
  161. public abstract int read(ByteBuffer dst) throws IOException;
  162. /**
  163. * Reads data from the channel.
  164. *
  165. * @exception AsynchronousCloseException If another thread closes this channel
  166. * while the transfer is in progress.
  167. * @exception ClosedByInterruptException If another thread interrupts the
  168. * current thread while the transfer is in progress, thereby closing both
  169. * channels and setting the current thread's interrupt status.
  170. * @exception ClosedChannelException If this channel is closed.
  171. * @exception IllegalArgumentException If position is negative.
  172. * @exception IOException If an I/O error occurs.
  173. * @exception NonReadableChannelException If this channel was not opened for
  174. * reading.
  175. */
  176. public abstract int read(ByteBuffer dst, long position) throws IOException;
  177. /**
  178. * Closes the channel.
  179. *
  180. * This is called from @see close.
  181. *
  182. * @exception IOException If an I/O error occurs.
  183. */
  184. protected abstract void implCloseChannel() throws IOException;
  185. /**
  186. * msync with the disk
  187. *
  188. * @exception ClosedChannelException If this channel is closed.
  189. * @exception IOException If an I/O error occurs.
  190. */
  191. public abstract void force(boolean metaData) throws IOException;
  192. /**
  193. * Creates a file lock for the whole assoziated file.
  194. *
  195. * @exception AsynchronousCloseException If another thread closes this channel
  196. * while the transfer is in progress.
  197. * @exception ClosedChannelException If this channel is closed.
  198. * @exception FileLockInterruptionException If the invoking thread is
  199. * interrupted while blocked in this method.
  200. * @exception IOException If an I/O error occurs.
  201. * @exception NonReadableChannelException If shared is true and this channel
  202. * was not opened for reading.
  203. * @exception NonWritableChannelException If shared is false and this channel
  204. * was not opened for writing.
  205. * @exception OverlappingFileLockException If a lock that overlaps the
  206. * requested region is already held by this Java virtual machine, or if
  207. * another thread is already blocked in this method and is attempting to lock
  208. * an overlapping region.
  209. */
  210. public final FileLock lock () throws IOException
  211. {
  212. return lock (0, Long.MAX_VALUE, false);
  213. }
  214. /**
  215. * Creates a file lock for a region of the assoziated file.
  216. *
  217. * @exception AsynchronousCloseException If another thread closes this channel
  218. * while the transfer is in progress.
  219. * @exception ClosedChannelException If this channel is closed.
  220. * @exception FileLockInterruptionException If the invoking thread is
  221. * interrupted while blocked in this method.
  222. * @exception IllegalArgumentException If the preconditions on the parameters
  223. * do not hold.
  224. * @exception IOException If an I/O error occurs.
  225. * @exception OverlappingFileLockException If a lock that overlaps the
  226. * requested region is already held by this Java virtual machine, or if
  227. * another thread is already blocked in this method and is attempting to lock
  228. * an overlapping region.
  229. * @exception NonReadableChannelException If shared is true and this channel
  230. * was not opened for reading.
  231. * @exception NonWritableChannelException If shared is false and this channel
  232. * was not opened for writing.
  233. */
  234. public abstract FileLock lock (long position, long size, boolean shared)
  235. throws IOException;
  236. /**
  237. * Tries to aqquire alock on the whole assoziated file.
  238. *
  239. * @exception ClosedChannelException If this channel is closed.
  240. * @exception IOException If an I/O error occurs.
  241. * @exception OverlappingFileLockException If a lock that overlaps the
  242. * requested region is already held by this Java virtual machine, or if
  243. * another thread is already blocked in this method and is attempting to lock
  244. * an overlapping region.
  245. */
  246. public final FileLock tryLock () throws IOException
  247. {
  248. return tryLock (0, Long.MAX_VALUE, false);
  249. }
  250. /**
  251. * Tries to aqquire a lock on a region of the assoziated file.
  252. *
  253. * @exception ClosedChannelException If this channel is closed.
  254. * @exception IllegalArgumentException If the preconditions on the parameters
  255. * do not hold.
  256. * @exception IOException If an I/O error occurs.
  257. * @exception OverlappingFileLockException If a lock that overlaps the
  258. * requested region is already held by this Java virtual machine, or if
  259. * another thread is already blocked in this method and is attempting to lock
  260. * an overlapping region.
  261. */
  262. public abstract FileLock tryLock (long position, long size, boolean shared)
  263. throws IOException;
  264. /**
  265. * Returns the current position on the file.
  266. *
  267. * @exception ClosedChannelException If this channel is closed.
  268. * @exception IOException If an I/O error occurs.
  269. */
  270. public abstract long position () throws IOException;
  271. /**
  272. * Sets the position of the channel on the assoziated file.
  273. *
  274. * @exception ClosedChannelException If this channel is closed.
  275. * @exception IllegalArgumentException If newPosition is negative.
  276. * @exception IOException If an I/O error occurs.
  277. */
  278. public abstract FileChannel position (long newPosition) throws IOException;
  279. /**
  280. * Transfers bytes from this channel's file to the given writable byte
  281. * channel.
  282. *
  283. * @exception AsynchronousCloseException If another thread closes this channel
  284. * while the transfer is in progress.
  285. * @exception ClosedByInterruptException If another thread interrupts the
  286. * current thread while the transfer is in progress, thereby closing both
  287. * channels and setting the current thread's interrupt status.
  288. * @exception ClosedChannelException If this channel is closed.
  289. * @exception IllegalArgumentException If the preconditions on the parameters
  290. * do not hold.
  291. * @exception IOException If an I/O error occurs.
  292. * @exception NonReadableChannelException If this channel was not opened for
  293. * reading.
  294. * @exception NonWritableChannelException If the target channel was not
  295. * opened for writing.
  296. */
  297. public abstract long transferTo (long position, long count,
  298. WritableByteChannel target)
  299. throws IOException;
  300. /**
  301. * Transfers bytes from the given readable channel into this channel.
  302. *
  303. * @exception AsynchronousCloseException If another thread closes this channel
  304. * while the transfer is in progress.
  305. * @exception ClosedByInterruptException If another thread interrupts the
  306. * current thread while the transfer is in progress, thereby closing both
  307. * channels and setting the current thread's interrupt status.
  308. * @exception ClosedChannelException If this channel is closed.
  309. * @exception IllegalArgumentException If the preconditions on the parameters
  310. * do not hold.
  311. * @exception IOException If an I/O error occurs.
  312. * @exception NonReadableChannelException If the source channel was not
  313. * opened for reading.
  314. * @exception NonWritableChannelException If this channel was not opened for
  315. * writing.
  316. */
  317. public abstract long transferFrom (ReadableByteChannel src, long position,
  318. long count) throws IOException;
  319. /**
  320. * Truncates the channel's file at <code>size</code>.
  321. *
  322. * @exception ClosedChannelException If this channel is closed.
  323. * @exception IllegalArgumentException If size is negative.
  324. * @exception IOException If an I/O error occurs.
  325. * @exception NonWritableChannelException If this channel was not opened for
  326. * writing.
  327. */
  328. public abstract FileChannel truncate (long size) throws IOException;
  329. }