CharSequenceBuffer.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* CharBuffer.java --
  2. Copyright (C) 2007 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. * A CharBuffer that wraps a {@link CharSequence}.
  34. */
  35. final class CharSequenceBuffer
  36. extends CharBuffer
  37. {
  38. /**
  39. * The wrapped char sequence.
  40. */
  41. private final CharSequence charSequence;
  42. /**
  43. * Creates a new CharSequenceBuffer.
  44. *
  45. * @param charSeq the CharSequence to wrap
  46. * @param capacity the capacity
  47. * @param limit the limit
  48. * @param position the position
  49. * @param mark the mark
  50. * @param offs the offset
  51. */
  52. CharSequenceBuffer(CharSequence charSeq, int capacity, int limit,
  53. int position, int mark, int offs)
  54. {
  55. super(capacity, limit, position, mark, null, null, offs);
  56. this.charSequence = charSeq;
  57. }
  58. /**
  59. * Creates a new instance of CharSequenceBuffer, wrapping the specified
  60. * {@link CharSequence}.
  61. *
  62. * @param charSeq the char sequence to wrap
  63. * @param start the start index in the char sequence
  64. * @param end the end index in the char sequence
  65. */
  66. CharSequenceBuffer(CharSequence charSeq, int start, int end)
  67. {
  68. this(charSeq, charSeq.length(), end, start, -1, 0);
  69. }
  70. /**
  71. * Returns a read-only view of this buffer.
  72. */
  73. public CharBuffer asReadOnlyBuffer()
  74. {
  75. return duplicate();
  76. }
  77. /**
  78. * This buffer class is not writable by definition and thus throws
  79. * a ReadOnlyBufferException here.
  80. */
  81. public CharBuffer compact()
  82. {
  83. throw new ReadOnlyBufferException();
  84. }
  85. /**
  86. * Returns a duplicate of this buffer.
  87. *
  88. * @return a duplicate of this buffer
  89. */
  90. public CharBuffer duplicate()
  91. {
  92. return new CharSequenceBuffer(charSequence, capacity(), limit, pos, mark, 0);
  93. }
  94. /**
  95. * Returns the character at the current position.
  96. *
  97. * @return the character at the current position
  98. */
  99. public char get()
  100. {
  101. if (pos >= limit)
  102. throw new BufferUnderflowException();
  103. return charSequence.charAt(array_offset + pos++);
  104. }
  105. /**
  106. * Returns the character at the specified position.
  107. *
  108. * @return the character at the specified position
  109. */
  110. public char get(int index)
  111. {
  112. if (index < 0 || index >= limit)
  113. throw new IndexOutOfBoundsException();
  114. return charSequence.charAt(array_offset + index);
  115. }
  116. /**
  117. * Cannot be direct, return <code>false</code> here.
  118. *
  119. * @return false
  120. */
  121. public boolean isDirect()
  122. {
  123. return false;
  124. }
  125. /**
  126. * Returns the byte order of this buffer. This is always the native byte
  127. * order.
  128. *
  129. * @return the byte order of this buffer
  130. */
  131. public ByteOrder order()
  132. {
  133. return ByteOrder.nativeOrder();
  134. }
  135. /**
  136. * This buffer class is not writable by definition and thus throws
  137. * a ReadOnlyBufferException here.
  138. */
  139. public CharBuffer put(char b)
  140. {
  141. throw new ReadOnlyBufferException();
  142. }
  143. /**
  144. * This buffer class is not writable by definition and thus throws
  145. * a ReadOnlyBufferException here.
  146. */
  147. public CharBuffer put(int index, char b)
  148. {
  149. throw new ReadOnlyBufferException();
  150. }
  151. /**
  152. * Returns a slice of this buffer, exposing the current position and limit.
  153. */
  154. public CharBuffer slice()
  155. {
  156. int newCapacity = limit - pos;
  157. return new CharSequenceBuffer(charSequence, newCapacity, newCapacity, 0,
  158. -1, pos);
  159. }
  160. /**
  161. * Returns a sub sequence from the specified start index and with the
  162. * specified length.
  163. *
  164. * @param start the start index
  165. * @param length the length of the sub sequence
  166. */
  167. public CharSequence subSequence(int start, int length)
  168. {
  169. int begin = array_offset + start + pos;
  170. return charSequence.subSequence(begin, begin + length);
  171. }
  172. /**
  173. * This kind of CharBuffer is read-only, so we return <code>true</code>
  174. * here.
  175. */
  176. public boolean isReadOnly()
  177. {
  178. return true;
  179. }
  180. }