1. /*
  2. * @(#)Direct-X-Buffer.java 1.48 04/05/03
  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. import sun.misc.Cleaner;
  10. import sun.misc.Unsafe;
  11. import sun.nio.ch.DirectBuffer;
  12. import sun.nio.ch.FileChannelImpl;
  13. class DirectCharBufferU
  14. extends CharBuffer
  15. implements DirectBuffer
  16. {
  17. // Cached unsafe-access object
  18. protected static final Unsafe unsafe = Bits.unsafe();
  19. // Cached unaligned-access capability
  20. protected static final boolean unaligned = Bits.unaligned();
  21. // Base address, used in all indexing calculations
  22. // NOTE: moved up to Buffer.java for speed in JNI GetDirectBufferAddress
  23. // protected long address;
  24. // If this buffer is a view of another buffer then we keep a reference to
  25. // that buffer so that its memory isn't freed before we're done with it
  26. protected Object viewedBuffer = null;
  27. public Object viewedBuffer() {
  28. return viewedBuffer;
  29. }
  30. public Cleaner cleaner() { return null; }
  31. // For duplicates and slices
  32. //
  33. DirectCharBufferU(DirectBuffer db, // package-private
  34. int mark, int pos, int lim, int cap,
  35. int off)
  36. {
  37. super(mark, pos, lim, cap);
  38. address = db.address() + off;
  39. viewedBuffer = db;
  40. }
  41. public CharBuffer slice() {
  42. int pos = this.position();
  43. int lim = this.limit();
  44. assert (pos <= lim);
  45. int rem = (pos <= lim ? lim - pos : 0);
  46. int off = (pos << 1);
  47. assert (off >= 0);
  48. return new DirectCharBufferU(this, -1, 0, rem, rem, off);
  49. }
  50. public CharBuffer duplicate() {
  51. return new DirectCharBufferU(this,
  52. this.markValue(),
  53. this.position(),
  54. this.limit(),
  55. this.capacity(),
  56. 0);
  57. }
  58. public CharBuffer asReadOnlyBuffer() {
  59. return new DirectCharBufferRU(this,
  60. this.markValue(),
  61. this.position(),
  62. this.limit(),
  63. this.capacity(),
  64. 0);
  65. }
  66. public long address() {
  67. return address;
  68. }
  69. private long ix(int i) {
  70. return address + (i << 1);
  71. }
  72. public char get() {
  73. return ((unsafe.getChar(ix(nextGetIndex()))));
  74. }
  75. public char get(int i) {
  76. return ((unsafe.getChar(ix(checkIndex(i)))));
  77. }
  78. public CharBuffer get(char[] dst, int offset, int length) {
  79. if ((length << 1) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) {
  80. checkBounds(offset, length, dst.length);
  81. int pos = position();
  82. int lim = limit();
  83. assert (pos <= lim);
  84. int rem = (pos <= lim ? lim - pos : 0);
  85. if (length > rem)
  86. throw new BufferUnderflowException();
  87. if (order() != ByteOrder.nativeOrder())
  88. Bits.copyToCharArray(ix(pos), dst,
  89. offset << 1,
  90. length << 1);
  91. else
  92. Bits.copyToByteArray(ix(pos), dst,
  93. offset << 1,
  94. length << 1);
  95. position(pos + length);
  96. } else {
  97. super.get(dst, offset, length);
  98. }
  99. return this;
  100. }
  101. public CharBuffer put(char x) {
  102. unsafe.putChar(ix(nextPutIndex()), ((x)));
  103. return this;
  104. }
  105. public CharBuffer put(int i, char x) {
  106. unsafe.putChar(ix(checkIndex(i)), ((x)));
  107. return this;
  108. }
  109. public CharBuffer put(CharBuffer src) {
  110. if (src instanceof DirectCharBufferU) {
  111. if (src == this)
  112. throw new IllegalArgumentException();
  113. DirectCharBufferU sb = (DirectCharBufferU)src;
  114. int spos = sb.position();
  115. int slim = sb.limit();
  116. assert (spos <= slim);
  117. int srem = (spos <= slim ? slim - spos : 0);
  118. int pos = position();
  119. int lim = limit();
  120. assert (pos <= lim);
  121. int rem = (pos <= lim ? lim - pos : 0);
  122. if (srem > rem)
  123. throw new BufferOverflowException();
  124. unsafe.copyMemory(sb.ix(spos), ix(pos), srem << 1);
  125. sb.position(spos + srem);
  126. position(pos + srem);
  127. } else if (!src.isDirect()) {
  128. int spos = src.position();
  129. int slim = src.limit();
  130. assert (spos <= slim);
  131. int srem = (spos <= slim ? slim - spos : 0);
  132. put(src.hb, src.offset + spos, srem);
  133. src.position(spos + srem);
  134. } else {
  135. super.put(src);
  136. }
  137. return this;
  138. }
  139. public CharBuffer put(char[] src, int offset, int length) {
  140. if ((length << 1) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) {
  141. checkBounds(offset, length, src.length);
  142. int pos = position();
  143. int lim = limit();
  144. assert (pos <= lim);
  145. int rem = (pos <= lim ? lim - pos : 0);
  146. if (length > rem)
  147. throw new BufferOverflowException();
  148. if (order() != ByteOrder.nativeOrder())
  149. Bits.copyFromCharArray(src, offset << 1,
  150. ix(pos), length << 1);
  151. else
  152. Bits.copyFromByteArray(src, offset << 1,
  153. ix(pos), length << 1);
  154. position(pos + length);
  155. } else {
  156. super.put(src, offset, length);
  157. }
  158. return this;
  159. }
  160. public CharBuffer compact() {
  161. int pos = position();
  162. int lim = limit();
  163. assert (pos <= lim);
  164. int rem = (pos <= lim ? lim - pos : 0);
  165. unsafe.copyMemory(ix(pos), ix(0), rem << 1);
  166. position(rem);
  167. limit(capacity());
  168. return this;
  169. }
  170. public boolean isDirect() {
  171. return true;
  172. }
  173. public boolean isReadOnly() {
  174. return false;
  175. }
  176. public String toString(int start, int end) {
  177. if ((end > limit()) || (start > end))
  178. throw new IndexOutOfBoundsException();
  179. try {
  180. int len = end - start;
  181. char[] ca = new char[len];
  182. CharBuffer cb = CharBuffer.wrap(ca);
  183. CharBuffer db = this.duplicate();
  184. db.position(start);
  185. db.limit(end);
  186. cb.put(db);
  187. return new String(ca);
  188. } catch (StringIndexOutOfBoundsException x) {
  189. throw new IndexOutOfBoundsException();
  190. }
  191. }
  192. // --- Methods to support CharSequence ---
  193. public CharSequence subSequence(int start, int end) {
  194. int pos = position();
  195. int lim = limit();
  196. assert (pos <= lim);
  197. pos = (pos <= lim ? pos : lim);
  198. int len = lim - pos;
  199. if ((start < 0) || (end > len) || (start > end))
  200. throw new IndexOutOfBoundsException();
  201. int sublen = end - start;
  202. int off = (pos + start) << 1;
  203. assert (off >= 0);
  204. return new DirectCharBufferU(this, -1, 0, sublen, sublen, off);
  205. }
  206. public ByteOrder order() {
  207. return ((ByteOrder.nativeOrder() != ByteOrder.BIG_ENDIAN)
  208. ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN);
  209. }
  210. }