1. /*
  2. * @(#)Heap-X-Buffer.java 1.28 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. // -- This file was mechanically generated: Do not edit! -- //
  8. package java.nio;
  9. /**
  10. * A read/write HeapCharBuffer.
  11. */
  12. class HeapCharBuffer
  13. extends CharBuffer
  14. {
  15. // For speed these fields are actually declared in X-Buffer;
  16. // these declarations are here as documentation
  17. /*
  18. protected final char[] hb;
  19. protected final int offset;
  20. */
  21. HeapCharBuffer(int cap, int lim) { // package-private
  22. super(-1, 0, lim, cap, new char[cap], 0);
  23. /*
  24. hb = new char[cap];
  25. offset = 0;
  26. */
  27. }
  28. HeapCharBuffer(char[] buf, int off, int len) { // package-private
  29. super(-1, off, off + len, buf.length, buf, 0);
  30. /*
  31. hb = buf;
  32. offset = 0;
  33. */
  34. }
  35. protected HeapCharBuffer(char[] buf,
  36. int mark, int pos, int lim, int cap,
  37. int off)
  38. {
  39. super(mark, pos, lim, cap, buf, off);
  40. /*
  41. hb = buf;
  42. offset = off;
  43. */
  44. }
  45. public CharBuffer slice() {
  46. return new HeapCharBuffer(hb,
  47. -1,
  48. 0,
  49. this.remaining(),
  50. this.remaining(),
  51. this.position() + offset);
  52. }
  53. public CharBuffer duplicate() {
  54. return new HeapCharBuffer(hb,
  55. this.markValue(),
  56. this.position(),
  57. this.limit(),
  58. this.capacity(),
  59. offset);
  60. }
  61. public CharBuffer asReadOnlyBuffer() {
  62. return new HeapCharBufferR(hb,
  63. this.markValue(),
  64. this.position(),
  65. this.limit(),
  66. this.capacity(),
  67. offset);
  68. }
  69. protected int ix(int i) {
  70. return i + offset;
  71. }
  72. public char get() {
  73. return hb[ix(nextGetIndex())];
  74. }
  75. public char get(int i) {
  76. return hb[ix(checkIndex(i))];
  77. }
  78. public CharBuffer get(char[] dst, int offset, int length) {
  79. checkBounds(offset, length, dst.length);
  80. if (length > remaining())
  81. throw new BufferUnderflowException();
  82. System.arraycopy(hb, ix(position()), dst, offset, length);
  83. position(position() + length);
  84. return this;
  85. }
  86. public boolean isDirect() {
  87. return false;
  88. }
  89. public boolean isReadOnly() {
  90. return false;
  91. }
  92. public CharBuffer put(char x) {
  93. hb[ix(nextPutIndex())] = x;
  94. return this;
  95. }
  96. public CharBuffer put(int i, char x) {
  97. hb[ix(checkIndex(i))] = x;
  98. return this;
  99. }
  100. public CharBuffer put(char[] src, int offset, int length) {
  101. checkBounds(offset, length, src.length);
  102. if (length > remaining())
  103. throw new BufferOverflowException();
  104. System.arraycopy(src, offset, hb, ix(position()), length);
  105. position(position() + length);
  106. return this;
  107. }
  108. public CharBuffer put(CharBuffer src) {
  109. if (src instanceof HeapCharBuffer) {
  110. if (src == this)
  111. throw new IllegalArgumentException();
  112. HeapCharBuffer sb = (HeapCharBuffer)src;
  113. int n = sb.remaining();
  114. if (n > remaining())
  115. throw new BufferOverflowException();
  116. System.arraycopy(sb.hb, sb.ix(sb.position()),
  117. hb, ix(position()), n);
  118. sb.position(sb.position() + n);
  119. position(position() + n);
  120. } else if (src.isDirect()) {
  121. int n = src.remaining();
  122. if (n > remaining())
  123. throw new BufferOverflowException();
  124. src.get(hb, ix(position()), n);
  125. position(position() + n);
  126. } else {
  127. super.put(src);
  128. }
  129. return this;
  130. }
  131. public CharBuffer compact() {
  132. System.arraycopy(hb, ix(position()), hb, ix(0), remaining());
  133. position(remaining());
  134. limit(capacity());
  135. return this;
  136. }
  137. String toString(int start, int end) { // package-private
  138. try {
  139. return new String(hb, start + offset, end - start);
  140. } catch (StringIndexOutOfBoundsException x) {
  141. throw new IndexOutOfBoundsException();
  142. }
  143. }
  144. // --- Methods to support CharSequence ---
  145. public CharSequence subSequence(int start, int end) {
  146. if ((start < 0)
  147. || (end > length())
  148. || (start > end))
  149. throw new IndexOutOfBoundsException();
  150. int len = end - start;
  151. return new HeapCharBuffer(hb,
  152. -1, 0, len, len,
  153. offset + position() + start);
  154. }
  155. public ByteOrder order() {
  156. return ByteOrder.nativeOrder();
  157. }
  158. }