ByteBufferImpl.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /* ByteBufferImpl.java --
  2. Copyright (C) 2002, 2003, 2004, 2005 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. /**
  33. * This is a Heap memory implementation
  34. */
  35. final class ByteBufferImpl extends ByteBuffer
  36. {
  37. private final boolean readOnly;
  38. ByteBufferImpl (byte[] buffer, int offset, int capacity, int limit,
  39. int position, int mark, boolean readOnly)
  40. {
  41. super (capacity, limit, position, mark, null, buffer, offset);
  42. this.readOnly = readOnly;
  43. }
  44. public CharBuffer asCharBuffer ()
  45. {
  46. return new CharViewBufferImpl (this, remaining() >> 1);
  47. }
  48. public ShortBuffer asShortBuffer ()
  49. {
  50. return new ShortViewBufferImpl (this, remaining() >> 1);
  51. }
  52. public IntBuffer asIntBuffer ()
  53. {
  54. return new IntViewBufferImpl (this, remaining() >> 2);
  55. }
  56. public LongBuffer asLongBuffer ()
  57. {
  58. return new LongViewBufferImpl (this, remaining() >> 3);
  59. }
  60. public FloatBuffer asFloatBuffer ()
  61. {
  62. return new FloatViewBufferImpl (this, remaining() >> 2);
  63. }
  64. public DoubleBuffer asDoubleBuffer ()
  65. {
  66. return new DoubleViewBufferImpl (this, remaining() >> 3);
  67. }
  68. public boolean isReadOnly ()
  69. {
  70. return readOnly;
  71. }
  72. public ByteBuffer slice ()
  73. {
  74. return new ByteBufferImpl (backing_buffer, array_offset + position (),
  75. remaining (), remaining (), 0, -1, isReadOnly ());
  76. }
  77. public ByteBuffer duplicate ()
  78. {
  79. return new ByteBufferImpl (backing_buffer, array_offset, capacity (),
  80. limit (), position (), mark, isReadOnly ());
  81. }
  82. public ByteBuffer asReadOnlyBuffer ()
  83. {
  84. return new ByteBufferImpl (backing_buffer, array_offset, capacity (),
  85. limit (), position (), mark, true);
  86. }
  87. void shiftDown (int dst_offset, int src_offset, int count)
  88. {
  89. System.arraycopy(backing_buffer, array_offset + src_offset,
  90. backing_buffer, array_offset + dst_offset,
  91. count);
  92. }
  93. public ByteBuffer compact ()
  94. {
  95. checkIfReadOnly();
  96. mark = -1;
  97. int pos = position();
  98. int n = limit() - pos;
  99. if (n > 0)
  100. shiftDown(0, pos, n);
  101. position(n);
  102. limit(capacity());
  103. return this;
  104. }
  105. public boolean isDirect ()
  106. {
  107. return false;
  108. }
  109. /**
  110. * Reads the <code>byte</code> at this buffer's current position,
  111. * and then increments the position.
  112. *
  113. * @exception BufferUnderflowException If there are no remaining
  114. * <code>bytes</code> in this buffer.
  115. */
  116. public byte get ()
  117. {
  118. if (pos >= limit)
  119. throw new BufferUnderflowException();
  120. return backing_buffer [(pos++) + array_offset];
  121. }
  122. /**
  123. * Bulk get
  124. */
  125. public ByteBuffer get (byte[] dst, int offset, int length)
  126. {
  127. checkArraySize(dst.length, offset, length);
  128. if ( (limit - pos) < length) // check for overflow
  129. throw new BufferUnderflowException();
  130. System.arraycopy(backing_buffer, pos + array_offset,
  131. dst, offset, length);
  132. pos += length;
  133. return this;
  134. }
  135. /**
  136. * Relative bulk put(), overloads the ByteBuffer impl.
  137. */
  138. public ByteBuffer put (byte[] src, int offset, int length)
  139. {
  140. if ( (limit - pos) < length) // check for overflow
  141. throw new BufferOverflowException();
  142. checkArraySize(src.length, offset, length);
  143. System.arraycopy(src, offset, backing_buffer, pos + array_offset, length);
  144. pos += length;
  145. return this;
  146. }
  147. /**
  148. * Relative put method. Writes <code>value</code> to the next position
  149. * in the buffer.
  150. *
  151. * @exception BufferOverflowException If there is no remaining
  152. * space in this buffer.
  153. * @exception ReadOnlyBufferException If this buffer is read-only.
  154. */
  155. public ByteBuffer put (byte value)
  156. {
  157. if (readOnly)
  158. throw new ReadOnlyBufferException();
  159. if (pos >= limit)
  160. throw new BufferOverflowException();
  161. backing_buffer [(pos++) + array_offset] = value;
  162. return this;
  163. }
  164. /**
  165. * Absolute get method. Reads the <code>byte</code> at position
  166. * <code>index</code>.
  167. *
  168. * @exception IndexOutOfBoundsException If index is negative or not smaller
  169. * than the buffer's limit.
  170. */
  171. public byte get (int index)
  172. {
  173. checkIndex(index);
  174. return backing_buffer [index + array_offset];
  175. }
  176. /**
  177. * Absolute put method. Writes <code>value</code> to position
  178. * <code>index</code> in the buffer.
  179. *
  180. * @exception IndexOutOfBoundsException If index is negative or not smaller
  181. * than the buffer's limit.
  182. * @exception ReadOnlyBufferException If this buffer is read-only.
  183. */
  184. public ByteBuffer put (int index, byte value)
  185. {
  186. checkIfReadOnly();
  187. checkIndex(index);
  188. backing_buffer [index + array_offset] = value;
  189. return this;
  190. }
  191. public char getChar ()
  192. {
  193. return ByteBufferHelper.getChar(this, order());
  194. }
  195. public ByteBuffer putChar (char value)
  196. {
  197. if (readOnly)
  198. throw new ReadOnlyBufferException ();
  199. if ( (limit-pos) < 2)
  200. throw new BufferOverflowException();
  201. if (endian == ByteOrder.LITTLE_ENDIAN)
  202. {
  203. backing_buffer [(pos++) + array_offset] = (byte)(value&0xFF);
  204. backing_buffer [(pos++) + array_offset] = (byte)(value>>8);
  205. }
  206. else
  207. {
  208. backing_buffer [(pos++) + array_offset] = (byte)(value>>8);
  209. backing_buffer [(pos++) + array_offset] = (byte)(value&0xFF);
  210. }
  211. return this;
  212. }
  213. public char getChar (int index)
  214. {
  215. return ByteBufferHelper.getChar(this, index, order());
  216. }
  217. public ByteBuffer putChar (int index, char value)
  218. {
  219. ByteBufferHelper.putChar(this, index, value, order());
  220. return this;
  221. }
  222. public short getShort ()
  223. {
  224. return ByteBufferHelper.getShort(this, order());
  225. }
  226. public ByteBuffer putShort (short value)
  227. {
  228. ByteBufferHelper.putShort(this, value, order());
  229. return this;
  230. }
  231. public short getShort (int index)
  232. {
  233. return ByteBufferHelper.getShort(this, index, order());
  234. }
  235. public ByteBuffer putShort (int index, short value)
  236. {
  237. ByteBufferHelper.putShort(this, index, value, order());
  238. return this;
  239. }
  240. public int getInt ()
  241. {
  242. return ByteBufferHelper.getInt(this, order());
  243. }
  244. public ByteBuffer putInt (int value)
  245. {
  246. ByteBufferHelper.putInt(this, value, order());
  247. return this;
  248. }
  249. public int getInt (int index)
  250. {
  251. return ByteBufferHelper.getInt(this, index, order());
  252. }
  253. public ByteBuffer putInt (int index, int value)
  254. {
  255. ByteBufferHelper.putInt(this, index, value, order());
  256. return this;
  257. }
  258. public long getLong ()
  259. {
  260. return ByteBufferHelper.getLong(this, order());
  261. }
  262. public ByteBuffer putLong (long value)
  263. {
  264. ByteBufferHelper.putLong (this, value, order());
  265. return this;
  266. }
  267. public long getLong (int index)
  268. {
  269. return ByteBufferHelper.getLong (this, index, order());
  270. }
  271. public ByteBuffer putLong (int index, long value)
  272. {
  273. ByteBufferHelper.putLong (this, index, value, order());
  274. return this;
  275. }
  276. public float getFloat ()
  277. {
  278. return ByteBufferHelper.getFloat (this, order());
  279. }
  280. public ByteBuffer putFloat (float value)
  281. {
  282. ByteBufferHelper.putFloat (this, value, order());
  283. return this;
  284. }
  285. public float getFloat (int index)
  286. {
  287. return ByteBufferHelper.getFloat (this, index, order());
  288. }
  289. public ByteBuffer putFloat (int index, float value)
  290. {
  291. ByteBufferHelper.putFloat (this, index, value, order());
  292. return this;
  293. }
  294. public double getDouble ()
  295. {
  296. return ByteBufferHelper.getDouble (this, order());
  297. }
  298. public ByteBuffer putDouble (double value)
  299. {
  300. ByteBufferHelper.putDouble (this, value, order());
  301. return this;
  302. }
  303. public double getDouble (int index)
  304. {
  305. return ByteBufferHelper.getDouble (this, index, order());
  306. }
  307. public ByteBuffer putDouble (int index, double value)
  308. {
  309. ByteBufferHelper.putDouble (this, index, value, order());
  310. return this;
  311. }
  312. }