1. /*
  2. * @(#)ByteBufferAs-X-Buffer.java 1.14 03/01/23
  3. *
  4. * Copyright 2003 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. class ByteBufferAsCharBufferB // package-private
  10. extends CharBuffer
  11. {
  12. protected final ByteBuffer bb;
  13. protected final int offset;
  14. ByteBufferAsCharBufferB(ByteBuffer bb) { // package-private
  15. super(-1, 0,
  16. bb.remaining() >> 1,
  17. bb.remaining() >> 1);
  18. this.bb = bb;
  19. // enforce limit == capacity
  20. int cap = this.capacity();
  21. this.limit(cap);
  22. int pos = this.position();
  23. assert (pos <= cap);
  24. offset = pos;
  25. }
  26. ByteBufferAsCharBufferB(ByteBuffer bb,
  27. int mark, int pos, int lim, int cap,
  28. int off)
  29. {
  30. super(mark, pos, lim, cap);
  31. this.bb = bb;
  32. offset = off;
  33. }
  34. public CharBuffer slice() {
  35. int pos = this.position();
  36. int lim = this.limit();
  37. assert (pos <= lim);
  38. int rem = (pos <= lim ? lim - pos : 0);
  39. int off = (pos << 1) + offset;
  40. return new ByteBufferAsCharBufferB(bb, -1, 0, rem, rem, off);
  41. }
  42. public CharBuffer duplicate() {
  43. return new ByteBufferAsCharBufferB(bb,
  44. this.markValue(),
  45. this.position(),
  46. this.limit(),
  47. this.capacity(),
  48. offset);
  49. }
  50. public CharBuffer asReadOnlyBuffer() {
  51. return new ByteBufferAsCharBufferRB(bb,
  52. this.markValue(),
  53. this.position(),
  54. this.limit(),
  55. this.capacity(),
  56. offset);
  57. }
  58. protected int ix(int i) {
  59. return (i << 1) + offset;
  60. }
  61. public char get() {
  62. return Bits.getCharB(bb, ix(nextGetIndex()));
  63. }
  64. public char get(int i) {
  65. return Bits.getCharB(bb, ix(checkIndex(i)));
  66. }
  67. public CharBuffer put(char x) {
  68. Bits.putCharB(bb, ix(nextPutIndex()), x);
  69. return this;
  70. }
  71. public CharBuffer put(int i, char x) {
  72. Bits.putCharB(bb, ix(checkIndex(i)), x);
  73. return this;
  74. }
  75. public CharBuffer compact() {
  76. int pos = position();
  77. int lim = limit();
  78. assert (pos <= lim);
  79. int rem = (pos <= lim ? lim - pos : 0);
  80. ByteBuffer db = bb.duplicate();
  81. db.limit(ix(lim));
  82. db.position(ix(0));
  83. ByteBuffer sb = db.slice();
  84. sb.position(pos << 1);
  85. sb.compact();
  86. position(rem);
  87. limit(capacity());
  88. return this;
  89. }
  90. public boolean isDirect() {
  91. return bb.isDirect();
  92. }
  93. public boolean isReadOnly() {
  94. return false;
  95. }
  96. public String toString(int start, int end) {
  97. if ((end > limit()) || (start > end))
  98. throw new IndexOutOfBoundsException();
  99. try {
  100. int len = end - start;
  101. char[] ca = new char[len];
  102. CharBuffer cb = CharBuffer.wrap(ca);
  103. CharBuffer db = this.duplicate();
  104. db.position(start);
  105. db.limit(end);
  106. cb.put(db);
  107. return new String(ca);
  108. } catch (StringIndexOutOfBoundsException x) {
  109. throw new IndexOutOfBoundsException();
  110. }
  111. }
  112. // --- Methods to support CharSequence ---
  113. public CharSequence subSequence(int start, int end) {
  114. int len = length();
  115. int pos = position();
  116. assert (pos <= len);
  117. pos = (pos <= len ? pos : len);
  118. if ((start < 0) || (end > len) || (start > end))
  119. throw new IndexOutOfBoundsException();
  120. int sublen = end - start;
  121. int off = offset + ((pos + start) << 1);
  122. return new ByteBufferAsCharBufferB(bb, -1, 0, sublen, sublen, off);
  123. }
  124. public ByteOrder order() {
  125. return ByteOrder.BIG_ENDIAN;
  126. }
  127. }