CharBufferImpl.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* CharBufferImpl.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. /**
  33. * This is a Heap memory implementation
  34. */
  35. final class CharBufferImpl extends CharBuffer
  36. {
  37. private final boolean readOnly;
  38. CharBufferImpl (int capacity)
  39. {
  40. this (new char [capacity], 0, capacity, capacity, 0, -1, false);
  41. }
  42. CharBufferImpl (char[] buffer, int offset, int capacity, int limit, int position, int mark, boolean readOnly)
  43. {
  44. super (capacity, limit, position, mark, null, buffer, offset);
  45. this.readOnly = readOnly;
  46. }
  47. public CharBufferImpl (CharBufferImpl copy)
  48. {
  49. super (copy.capacity (), copy.limit (), copy.position (), 0, null, copy.backing_buffer, copy.array_offset);
  50. this.readOnly = copy.isReadOnly ();
  51. }
  52. public boolean isReadOnly ()
  53. {
  54. return readOnly;
  55. }
  56. public CharBuffer slice ()
  57. {
  58. return new CharBufferImpl (backing_buffer, array_offset + position (), remaining (), remaining (), 0, -1, isReadOnly ());
  59. }
  60. public CharBuffer duplicate ()
  61. {
  62. return new CharBufferImpl (backing_buffer, array_offset, capacity (), limit (), position (), mark, isReadOnly ());
  63. }
  64. public CharBuffer asReadOnlyBuffer ()
  65. {
  66. return new CharBufferImpl (backing_buffer, array_offset, capacity (), limit (), position (), mark, true);
  67. }
  68. public CharBuffer compact ()
  69. {
  70. checkIfReadOnly();
  71. mark = -1;
  72. int p = position();
  73. int n = limit() - p;
  74. if (n > 0)
  75. {
  76. System.arraycopy(backing_buffer, array_offset + p,
  77. backing_buffer, array_offset, n);
  78. }
  79. position(n);
  80. limit(capacity());
  81. return this;
  82. }
  83. public boolean isDirect ()
  84. {
  85. return false;
  86. }
  87. public CharSequence subSequence (int start, int end)
  88. {
  89. if (start < 0
  90. || start > length ()
  91. || end < start
  92. || end > length ())
  93. throw new IndexOutOfBoundsException ();
  94. return new CharBufferImpl (backing_buffer, array_offset, capacity (), position () + end, position () + start, -1, isReadOnly ());
  95. }
  96. /**
  97. * Reads the <code>char</code> at this buffer's current position,
  98. * and then increments the position.
  99. *
  100. * @exception BufferUnderflowException If there are no remaining
  101. * <code>char</code>s in this buffer.
  102. */
  103. public char get ()
  104. {
  105. if (pos >= limit)
  106. throw new BufferUnderflowException();
  107. return backing_buffer [(pos++) + array_offset];
  108. }
  109. /**
  110. * Relative put method. Writes <code>value</code> to the next position
  111. * in the buffer.
  112. *
  113. * @exception ReadOnlyBufferException If this buffer is read-only.
  114. */
  115. public CharBuffer put (char value)
  116. {
  117. if (readOnly)
  118. throw new ReadOnlyBufferException();
  119. if (pos >= limit)
  120. throw new BufferOverflowException();
  121. backing_buffer [(pos++) + array_offset] = value;
  122. return this;
  123. }
  124. /**
  125. * Absolute get method. Reads the <code>char</code> at position
  126. * <code>index</code>.
  127. *
  128. * @param index Position to read the <code>char</code> from.
  129. *
  130. * @exception IndexOutOfBoundsException If index is negative or not smaller
  131. * than the buffer's limit.
  132. */
  133. public char get (int index)
  134. {
  135. checkIndex(index);
  136. return backing_buffer [index + array_offset];
  137. }
  138. /**
  139. * Bulk get, overloaded for speed.
  140. */
  141. public CharBuffer get (char[] dst, int offset, int length)
  142. {
  143. checkArraySize(dst.length, offset, length);
  144. checkForUnderflow(length);
  145. System.arraycopy(backing_buffer, pos + array_offset,
  146. dst, offset, length);
  147. pos += length;
  148. return this;
  149. }
  150. /**
  151. * Bulk put, overloaded for speed.
  152. */
  153. public CharBuffer put (char[] src, int offset, int length)
  154. {
  155. checkArraySize(src.length, offset, length);
  156. checkForOverflow(length);
  157. System.arraycopy(src, offset,
  158. backing_buffer, pos + array_offset, length);
  159. pos += length;
  160. return this;
  161. }
  162. /**
  163. * Absolute put method. Writes <code>value</code> to position
  164. * <code>index</code> in the buffer.
  165. *
  166. * @exception IndexOutOfBoundsException If index is negative or not smaller
  167. * than the buffer's limit.
  168. * @exception ReadOnlyBufferException If this buffer is read-only.
  169. */
  170. public CharBuffer put (int index, char value)
  171. {
  172. checkIndex(index);
  173. checkIfReadOnly();
  174. backing_buffer [index + array_offset] = value;
  175. return this;
  176. }
  177. public ByteOrder order ()
  178. {
  179. return ByteOrder.nativeOrder ();
  180. }
  181. }