DirectByteBufferImpl.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /* DirectByteBufferImpl.java --
  2. Copyright (C) 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. abstract class DirectByteBufferImpl extends ByteBuffer
  34. {
  35. /**
  36. * The owner is used to keep alive the object that actually owns the
  37. * memory. There are three possibilities:
  38. * 1) owner == this: We allocated the memory and we should free it,
  39. * but *only* in finalize (if we've been sliced
  40. * other objects will also have access to the
  41. * memory).
  42. * 2) owner == null: The byte buffer was created thru
  43. * JNI.NewDirectByteBuffer. The JNI code is
  44. * responsible for freeing the memory.
  45. * 3) owner == some other object: The other object allocated the
  46. * memory and should free it.
  47. */
  48. private final Object owner;
  49. static final class ReadOnly extends DirectByteBufferImpl
  50. {
  51. ReadOnly(Object owner, Pointer address,
  52. int capacity, int limit,
  53. int position)
  54. {
  55. super(owner, address, capacity, limit, position);
  56. }
  57. public ByteBuffer put(byte value)
  58. {
  59. throw new ReadOnlyBufferException ();
  60. }
  61. public ByteBuffer put(int index, byte value)
  62. {
  63. throw new ReadOnlyBufferException ();
  64. }
  65. public boolean isReadOnly()
  66. {
  67. return true;
  68. }
  69. }
  70. static final class ReadWrite extends DirectByteBufferImpl
  71. {
  72. ReadWrite(int capacity)
  73. {
  74. super(capacity);
  75. }
  76. ReadWrite(Pointer address, int capacity)
  77. {
  78. super(address, capacity);
  79. }
  80. ReadWrite(Object owner, Pointer address,
  81. int capacity, int limit,
  82. int position)
  83. {
  84. super(owner, address, capacity, limit, position);
  85. }
  86. public boolean isReadOnly()
  87. {
  88. return false;
  89. }
  90. }
  91. DirectByteBufferImpl(int capacity)
  92. {
  93. super(capacity, capacity, 0, -1,
  94. VMDirectByteBuffer.allocate(capacity), null, 0);
  95. this.owner = this;
  96. }
  97. DirectByteBufferImpl(Pointer address, int capacity)
  98. {
  99. super(capacity, capacity, 0, -1, address, null, 0);
  100. this.owner = null;
  101. }
  102. DirectByteBufferImpl(Object owner, Pointer address,
  103. int capacity, int limit,
  104. int position)
  105. {
  106. super(capacity, limit, position, -1, address, null, 0);
  107. this.owner = owner;
  108. }
  109. /**
  110. * Allocates a new direct byte buffer.
  111. */
  112. public static ByteBuffer allocate(int capacity)
  113. {
  114. return new DirectByteBufferImpl.ReadWrite(capacity);
  115. }
  116. protected void finalize() throws Throwable
  117. {
  118. if (owner == this)
  119. VMDirectByteBuffer.free(address);
  120. }
  121. public byte get()
  122. {
  123. checkForUnderflow();
  124. int pos = position();
  125. byte result = VMDirectByteBuffer.get(address, pos);
  126. position(pos + 1);
  127. return result;
  128. }
  129. public byte get(int index)
  130. {
  131. checkIndex(index);
  132. return VMDirectByteBuffer.get(address, index);
  133. }
  134. public ByteBuffer get(byte[] dst, int offset, int length)
  135. {
  136. checkArraySize(dst.length, offset, length);
  137. checkForUnderflow(length);
  138. int index = position();
  139. VMDirectByteBuffer.get(address, index, dst, offset, length);
  140. position(index+length);
  141. return this;
  142. }
  143. public ByteBuffer put(byte value)
  144. {
  145. checkForOverflow();
  146. int pos = position();
  147. VMDirectByteBuffer.put(address, pos, value);
  148. position(pos + 1);
  149. return this;
  150. }
  151. public ByteBuffer put(int index, byte value)
  152. {
  153. checkIndex(index);
  154. VMDirectByteBuffer.put(address, index, value);
  155. return this;
  156. }
  157. public ByteBuffer put (byte[] src, int offset, int length)
  158. {
  159. checkArraySize (src.length, offset, length);
  160. checkForUnderflow (length);
  161. int index = position ();
  162. VMDirectByteBuffer.put (address, index, src, offset, length);
  163. position (index + length);
  164. return this;
  165. }
  166. void shiftDown(int dst_offset, int src_offset, int count)
  167. {
  168. VMDirectByteBuffer.shiftDown(address, dst_offset, src_offset, count);
  169. }
  170. public ByteBuffer compact()
  171. {
  172. checkIfReadOnly();
  173. mark = -1;
  174. int pos = position();
  175. if (pos > 0)
  176. {
  177. int count = remaining();
  178. VMDirectByteBuffer.shiftDown(address, 0, pos, count);
  179. position(count);
  180. limit(capacity());
  181. }
  182. else
  183. {
  184. position(limit());
  185. limit(capacity());
  186. }
  187. return this;
  188. }
  189. public ByteBuffer slice()
  190. {
  191. int rem = remaining();
  192. if (isReadOnly())
  193. return new DirectByteBufferImpl.ReadOnly
  194. (owner, VMDirectByteBuffer.adjustAddress(address, position()),
  195. rem, rem, 0);
  196. else
  197. return new DirectByteBufferImpl.ReadWrite
  198. (owner, VMDirectByteBuffer.adjustAddress(address, position()),
  199. rem, rem, 0);
  200. }
  201. private ByteBuffer duplicate(boolean readOnly)
  202. {
  203. int pos = position();
  204. if (this.mark != -1)
  205. reset();
  206. int mark = position();
  207. position(pos);
  208. DirectByteBufferImpl result;
  209. if (readOnly)
  210. result = new DirectByteBufferImpl.ReadOnly(owner, address, capacity(),
  211. limit(), pos);
  212. else
  213. result = new DirectByteBufferImpl.ReadWrite(owner, address, capacity(),
  214. limit(), pos);
  215. if (mark != pos)
  216. {
  217. result.position(mark);
  218. result.mark();
  219. result.position(pos);
  220. }
  221. return result;
  222. }
  223. public ByteBuffer duplicate()
  224. {
  225. return duplicate(isReadOnly());
  226. }
  227. public ByteBuffer asReadOnlyBuffer()
  228. {
  229. return duplicate(true);
  230. }
  231. public boolean isDirect()
  232. {
  233. return true;
  234. }
  235. public CharBuffer asCharBuffer()
  236. {
  237. return new CharViewBufferImpl(this, remaining() >> 1);
  238. }
  239. public ShortBuffer asShortBuffer()
  240. {
  241. return new ShortViewBufferImpl(this, remaining() >> 1);
  242. }
  243. public IntBuffer asIntBuffer()
  244. {
  245. return new IntViewBufferImpl(this, remaining() >> 2);
  246. }
  247. public LongBuffer asLongBuffer()
  248. {
  249. return new LongViewBufferImpl(this, remaining() >> 3);
  250. }
  251. public FloatBuffer asFloatBuffer()
  252. {
  253. return new FloatViewBufferImpl(this, remaining() >> 2);
  254. }
  255. public DoubleBuffer asDoubleBuffer()
  256. {
  257. return new DoubleViewBufferImpl(this, remaining() >> 3);
  258. }
  259. public char getChar()
  260. {
  261. return ByteBufferHelper.getChar(this, order());
  262. }
  263. public ByteBuffer putChar(char value)
  264. {
  265. ByteBufferHelper.putChar(this, value, order());
  266. return this;
  267. }
  268. public char getChar(int index)
  269. {
  270. return ByteBufferHelper.getChar(this, index, order());
  271. }
  272. public ByteBuffer putChar(int index, char value)
  273. {
  274. ByteBufferHelper.putChar(this, index, value, order());
  275. return this;
  276. }
  277. public short getShort()
  278. {
  279. return ByteBufferHelper.getShort(this, order());
  280. }
  281. public ByteBuffer putShort(short value)
  282. {
  283. ByteBufferHelper.putShort(this, value, order());
  284. return this;
  285. }
  286. public short getShort(int index)
  287. {
  288. return ByteBufferHelper.getShort(this, index, order());
  289. }
  290. public ByteBuffer putShort(int index, short value)
  291. {
  292. ByteBufferHelper.putShort(this, index, value, order());
  293. return this;
  294. }
  295. public int getInt()
  296. {
  297. return ByteBufferHelper.getInt(this, order());
  298. }
  299. public ByteBuffer putInt(int value)
  300. {
  301. ByteBufferHelper.putInt(this, value, order());
  302. return this;
  303. }
  304. public int getInt(int index)
  305. {
  306. return ByteBufferHelper.getInt(this, index, order());
  307. }
  308. public ByteBuffer putInt(int index, int value)
  309. {
  310. ByteBufferHelper.putInt(this, index, value, order());
  311. return this;
  312. }
  313. public long getLong()
  314. {
  315. return ByteBufferHelper.getLong(this, order());
  316. }
  317. public ByteBuffer putLong(long value)
  318. {
  319. ByteBufferHelper.putLong(this, value, order());
  320. return this;
  321. }
  322. public long getLong(int index)
  323. {
  324. return ByteBufferHelper.getLong(this, index, order());
  325. }
  326. public ByteBuffer putLong(int index, long value)
  327. {
  328. ByteBufferHelper.putLong(this, index, value, order());
  329. return this;
  330. }
  331. public float getFloat()
  332. {
  333. return ByteBufferHelper.getFloat(this, order());
  334. }
  335. public ByteBuffer putFloat(float value)
  336. {
  337. ByteBufferHelper.putFloat(this, value, order());
  338. return this;
  339. }
  340. public float getFloat(int index)
  341. {
  342. return ByteBufferHelper.getFloat(this, index, order());
  343. }
  344. public ByteBuffer putFloat(int index, float value)
  345. {
  346. ByteBufferHelper.putFloat(this, index, value, order());
  347. return this;
  348. }
  349. public double getDouble()
  350. {
  351. return ByteBufferHelper.getDouble(this, order());
  352. }
  353. public ByteBuffer putDouble(double value)
  354. {
  355. ByteBufferHelper.putDouble(this, value, order());
  356. return this;
  357. }
  358. public double getDouble(int index)
  359. {
  360. return ByteBufferHelper.getDouble(this, index, order());
  361. }
  362. public ByteBuffer putDouble(int index, double value)
  363. {
  364. ByteBufferHelper.putDouble(this, index, value, order());
  365. return this;
  366. }
  367. }