Buffer.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /* Buffer.java --
  2. Copyright (C) 2002, 2003, 2004 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;
  32. import gnu.classpath.Pointer;
  33. /**
  34. * @since 1.4
  35. */
  36. public abstract class Buffer
  37. {
  38. private final int cap;
  39. int limit;
  40. int pos;
  41. int mark;
  42. final Pointer address;
  43. /**
  44. * Creates a new Buffer.
  45. *
  46. * Should be package private.
  47. */
  48. Buffer (int capacity, int limit, int position, int mark,
  49. Pointer address)
  50. {
  51. this.address = address;
  52. if (capacity < 0)
  53. throw new IllegalArgumentException ();
  54. cap = capacity;
  55. limit (limit);
  56. position (position);
  57. if (mark >= 0)
  58. {
  59. if (mark > pos)
  60. throw new IllegalArgumentException ();
  61. this.mark = mark;
  62. }
  63. else
  64. {
  65. this.mark = -1;
  66. }
  67. }
  68. /**
  69. * Retrieves the capacity of the buffer.
  70. *
  71. * @return the capacity of the buffer
  72. */
  73. public final int capacity ()
  74. {
  75. return cap;
  76. }
  77. /**
  78. * Clears the buffer.
  79. *
  80. * @return this buffer
  81. */
  82. public final Buffer clear ()
  83. {
  84. limit = cap;
  85. pos = 0;
  86. mark = -1;
  87. return this;
  88. }
  89. /**
  90. * Flips the buffer.
  91. *
  92. * @return this buffer
  93. */
  94. public final Buffer flip ()
  95. {
  96. limit = pos;
  97. pos = 0;
  98. mark = -1;
  99. return this;
  100. }
  101. /**
  102. * Tells whether the buffer has remaining data to read or not.
  103. *
  104. * @return true if the buffer contains remaining data to read,
  105. * false otherwise
  106. */
  107. public final boolean hasRemaining ()
  108. {
  109. return remaining() > 0;
  110. }
  111. /**
  112. * Tells whether this buffer is read only or not.
  113. *
  114. * @return true if the buffer is read only, false otherwise
  115. */
  116. public abstract boolean isReadOnly ();
  117. /**
  118. * Retrieves the current limit of the buffer.
  119. *
  120. * @return the limit of the buffer
  121. */
  122. public final int limit ()
  123. {
  124. return limit;
  125. }
  126. /**
  127. * Sets this buffer's limit.
  128. *
  129. * @param newLimit The new limit value; must be non-negative and no larger
  130. * than this buffer's capacity.
  131. *
  132. * @return this buffer
  133. *
  134. * @exception IllegalArgumentException If the preconditions on newLimit
  135. * do not hold.
  136. */
  137. public final Buffer limit (int newLimit)
  138. {
  139. if ((newLimit < 0) || (newLimit > cap))
  140. throw new IllegalArgumentException ();
  141. if (newLimit < mark)
  142. mark = -1;
  143. if (pos > newLimit)
  144. pos = newLimit;
  145. limit = newLimit;
  146. return this;
  147. }
  148. /**
  149. * Sets this buffer's mark at its position.
  150. *
  151. * @return this buffer
  152. */
  153. public final Buffer mark ()
  154. {
  155. mark = pos;
  156. return this;
  157. }
  158. /**
  159. * Retrieves the current position of this buffer.
  160. *
  161. * @return the current position of this buffer
  162. */
  163. public final int position ()
  164. {
  165. return pos;
  166. }
  167. /**
  168. * Sets this buffer's position. If the mark is defined and larger than the
  169. * new position then it is discarded.
  170. *
  171. * @param newPosition The new position value; must be non-negative and no
  172. * larger than the current limit.
  173. *
  174. * @return this buffer
  175. *
  176. * @exception IllegalArgumentException If the preconditions on newPosition
  177. * do not hold
  178. */
  179. public final Buffer position (int newPosition)
  180. {
  181. if ((newPosition < 0) || (newPosition > limit))
  182. throw new IllegalArgumentException ();
  183. if (newPosition <= mark)
  184. mark = -1;
  185. pos = newPosition;
  186. return this;
  187. }
  188. /**
  189. * Returns the number of elements between the current position and the limit.
  190. *
  191. * @return the number of remaining elements
  192. */
  193. public final int remaining()
  194. {
  195. return limit - pos;
  196. }
  197. /**
  198. * Resets this buffer's position to the previously-marked position.
  199. *
  200. * @return this buffer
  201. *
  202. * @exception InvalidMarkException If the mark has not been set.
  203. */
  204. public final Buffer reset()
  205. {
  206. if (mark == -1)
  207. throw new InvalidMarkException ();
  208. pos = mark;
  209. return this;
  210. }
  211. /**
  212. * Rewinds this buffer. The position is set to zero and the mark
  213. * is discarded.
  214. *
  215. * @return this buffer
  216. */
  217. public final Buffer rewind()
  218. {
  219. pos = 0;
  220. mark = -1;
  221. return this;
  222. }
  223. /**
  224. * Checks for underflow. This method is used internally to check
  225. * whether a buffer has enough elements left to satisfy a read
  226. * request.
  227. *
  228. * @exception BufferUnderflowException If there are no remaining
  229. * elements in this buffer.
  230. */
  231. final void checkForUnderflow()
  232. {
  233. if (!hasRemaining())
  234. throw new BufferUnderflowException();
  235. }
  236. /**
  237. * Checks for underflow. This method is used internally to check
  238. * whether a buffer has enough elements left to satisfy a read
  239. * request for a given number of elements.
  240. *
  241. * @param length The length of a sequence of elements.
  242. *
  243. * @exception BufferUnderflowException If there are not enough
  244. * remaining elements in this buffer.
  245. */
  246. final void checkForUnderflow(int length)
  247. {
  248. if (remaining() < length)
  249. throw new BufferUnderflowException();
  250. }
  251. /**
  252. * Checks for overflow. This method is used internally to check
  253. * whether a buffer has enough space left to satisfy a write
  254. * request.
  255. *
  256. * @exception BufferOverflowException If there is no remaining
  257. * space in this buffer.
  258. */
  259. final void checkForOverflow()
  260. {
  261. if (!hasRemaining())
  262. throw new BufferOverflowException();
  263. }
  264. /**
  265. * Checks for overflow. This method is used internally to check
  266. * whether a buffer has enough space left to satisfy a write
  267. * request for a given number of elements.
  268. *
  269. * @param length The length of a sequence of elements.
  270. *
  271. * @exception BufferUnderflowException If there is not enough
  272. * remaining space in this buffer.
  273. */
  274. final void checkForOverflow(int length)
  275. {
  276. if (remaining() < length)
  277. throw new BufferOverflowException();
  278. }
  279. /**
  280. * Checks if index is negative or not smaller than the buffer's
  281. * limit. This method is used internally to check whether
  282. * an indexed request can be fulfilled.
  283. *
  284. * @param index The requested position in the buffer.
  285. *
  286. * @exception IndexOutOfBoundsException If index is negative or not smaller
  287. * than the buffer's limit.
  288. */
  289. final void checkIndex(int index)
  290. {
  291. if (index < 0
  292. || index >= limit ())
  293. throw new IndexOutOfBoundsException ();
  294. }
  295. /**
  296. * Checks if buffer is read-only. This method is used internally to
  297. * check if elements can be put into a buffer.
  298. *
  299. * @exception ReadOnlyBufferException If this buffer is read-only.
  300. */
  301. final void checkIfReadOnly()
  302. {
  303. if (isReadOnly())
  304. throw new ReadOnlyBufferException ();
  305. }
  306. /**
  307. * Checks whether an array is large enough to hold the given number of
  308. * elements at the given offset. This method is used internally to
  309. * check if an array is big enough.
  310. *
  311. * @param arraylength The length of the array.
  312. * @param offset The offset within the array of the first byte to be read;
  313. * must be non-negative and no larger than arraylength.
  314. * @param length The number of bytes to be read from the given array;
  315. * must be non-negative and no larger than arraylength - offset.
  316. *
  317. * @exception IndexOutOfBoundsException If the preconditions on the offset
  318. * and length parameters do not hold
  319. */
  320. static final void checkArraySize(int arraylength, int offset, int length)
  321. {
  322. if ((offset < 0) ||
  323. (length < 0) ||
  324. (arraylength < length + offset))
  325. throw new IndexOutOfBoundsException ();
  326. }
  327. /**
  328. * Returns the backing array of this buffer, if this buffer has one.
  329. * Modification to the array are directly visible in this buffer and vice
  330. * versa.
  331. *
  332. * <p>
  333. * If this is a read-only buffer, then a {@link ReadOnlyBufferException} is
  334. * thrown because exposing the array would allow to circumvent the read-only
  335. * property. If this buffer doesn't have an array, then an
  336. * {@link UnsupportedOperationException} is thrown. Applications should check
  337. * if this buffer supports a backing array by calling {@link #hasArray}
  338. * first.</p>
  339. *
  340. * @return the backing array of this buffer
  341. *
  342. * @throws ReadOnlyBufferException when this buffer is read only
  343. * @throws UnsupportedOperationException when this buffer does not provide
  344. * a backing array
  345. *
  346. * @since 1.6
  347. */
  348. public abstract Object array();
  349. /**
  350. * Returns <code>true</code> if this buffer can provide a backing array,
  351. * <code>false</code> otherwise. When <code>true</code>, application code
  352. * can call {@link #array()} to access this backing array.
  353. *
  354. * @return <code>true</code> if this buffer can provide a backing array,
  355. * <code>false</code> otherwise
  356. *
  357. * @since 1.6
  358. */
  359. public abstract boolean hasArray();
  360. /**
  361. * For buffers that are backed by a Java array, this returns the offset
  362. * into that array at which the buffer content starts.
  363. *
  364. * @return the offset into the backing array at which the buffer content
  365. * starts
  366. * @throws ReadOnlyBufferException when this buffer is read only
  367. * @throws UnsupportedOperationException when this buffer does not provide
  368. * a backing array
  369. *
  370. * @since 1.6
  371. */
  372. public abstract int arrayOffset();
  373. /**
  374. * Returns <code>true</code> when this buffer is direct, <code>false</code>
  375. * otherwise. A direct buffer is usually backed by a raw memory area instead
  376. * of a Java array.
  377. *
  378. * @return <code>true</code> when this buffer is direct, <code>false</code>
  379. * otherwise
  380. *
  381. * @since 1.6
  382. */
  383. public abstract boolean isDirect();
  384. }