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 HeapByteBuffer.
  11. */
  12. class HeapByteBuffer
  13. extends ByteBuffer
  14. {
  15. // For speed these fields are actually declared in X-Buffer;
  16. // these declarations are here as documentation
  17. /*
  18. protected final byte[] hb;
  19. protected final int offset;
  20. */
  21. HeapByteBuffer(int cap, int lim) { // package-private
  22. super(-1, 0, lim, cap, new byte[cap], 0);
  23. /*
  24. hb = new byte[cap];
  25. offset = 0;
  26. */
  27. }
  28. HeapByteBuffer(byte[] 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 HeapByteBuffer(byte[] 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 ByteBuffer slice() {
  46. return new HeapByteBuffer(hb,
  47. -1,
  48. 0,
  49. this.remaining(),
  50. this.remaining(),
  51. this.position() + offset);
  52. }
  53. public ByteBuffer duplicate() {
  54. return new HeapByteBuffer(hb,
  55. this.markValue(),
  56. this.position(),
  57. this.limit(),
  58. this.capacity(),
  59. offset);
  60. }
  61. public ByteBuffer asReadOnlyBuffer() {
  62. return new HeapByteBufferR(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 byte get() {
  73. return hb[ix(nextGetIndex())];
  74. }
  75. public byte get(int i) {
  76. return hb[ix(checkIndex(i))];
  77. }
  78. public ByteBuffer get(byte[] 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 ByteBuffer put(byte x) {
  93. hb[ix(nextPutIndex())] = x;
  94. return this;
  95. }
  96. public ByteBuffer put(int i, byte x) {
  97. hb[ix(checkIndex(i))] = x;
  98. return this;
  99. }
  100. public ByteBuffer put(byte[] 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 ByteBuffer put(ByteBuffer src) {
  109. if (src instanceof HeapByteBuffer) {
  110. if (src == this)
  111. throw new IllegalArgumentException();
  112. HeapByteBuffer sb = (HeapByteBuffer)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 ByteBuffer compact() {
  132. System.arraycopy(hb, ix(position()), hb, ix(0), remaining());
  133. position(remaining());
  134. limit(capacity());
  135. return this;
  136. }
  137. byte _get(int i) { // package-private
  138. return hb[i];
  139. }
  140. void _put(int i, byte b) { // package-private
  141. hb[i] = b;
  142. }
  143. // char
  144. public char getChar() {
  145. return Bits.getChar(this, ix(nextGetIndex(2)), bigEndian);
  146. }
  147. public char getChar(int i) {
  148. return Bits.getChar(this, ix(checkIndex(i, 2)), bigEndian);
  149. }
  150. public ByteBuffer putChar(char x) {
  151. Bits.putChar(this, ix(nextPutIndex(2)), x, bigEndian);
  152. return this;
  153. }
  154. public ByteBuffer putChar(int i, char x) {
  155. Bits.putChar(this, ix(checkIndex(i, 2)), x, bigEndian);
  156. return this;
  157. }
  158. public CharBuffer asCharBuffer() {
  159. int size = this.remaining() >> 1;
  160. int off = offset + position();
  161. return (bigEndian
  162. ? (CharBuffer)(new ByteBufferAsCharBufferB(this,
  163. -1,
  164. 0,
  165. size,
  166. size,
  167. off))
  168. : (CharBuffer)(new ByteBufferAsCharBufferL(this,
  169. -1,
  170. 0,
  171. size,
  172. size,
  173. off)));
  174. }
  175. // short
  176. public short getShort() {
  177. return Bits.getShort(this, ix(nextGetIndex(2)), bigEndian);
  178. }
  179. public short getShort(int i) {
  180. return Bits.getShort(this, ix(checkIndex(i, 2)), bigEndian);
  181. }
  182. public ByteBuffer putShort(short x) {
  183. Bits.putShort(this, ix(nextPutIndex(2)), x, bigEndian);
  184. return this;
  185. }
  186. public ByteBuffer putShort(int i, short x) {
  187. Bits.putShort(this, ix(checkIndex(i, 2)), x, bigEndian);
  188. return this;
  189. }
  190. public ShortBuffer asShortBuffer() {
  191. int size = this.remaining() >> 1;
  192. int off = offset + position();
  193. return (bigEndian
  194. ? (ShortBuffer)(new ByteBufferAsShortBufferB(this,
  195. -1,
  196. 0,
  197. size,
  198. size,
  199. off))
  200. : (ShortBuffer)(new ByteBufferAsShortBufferL(this,
  201. -1,
  202. 0,
  203. size,
  204. size,
  205. off)));
  206. }
  207. // int
  208. public int getInt() {
  209. return Bits.getInt(this, ix(nextGetIndex(4)), bigEndian);
  210. }
  211. public int getInt(int i) {
  212. return Bits.getInt(this, ix(checkIndex(i, 4)), bigEndian);
  213. }
  214. public ByteBuffer putInt(int x) {
  215. Bits.putInt(this, ix(nextPutIndex(4)), x, bigEndian);
  216. return this;
  217. }
  218. public ByteBuffer putInt(int i, int x) {
  219. Bits.putInt(this, ix(checkIndex(i, 4)), x, bigEndian);
  220. return this;
  221. }
  222. public IntBuffer asIntBuffer() {
  223. int size = this.remaining() >> 2;
  224. int off = offset + position();
  225. return (bigEndian
  226. ? (IntBuffer)(new ByteBufferAsIntBufferB(this,
  227. -1,
  228. 0,
  229. size,
  230. size,
  231. off))
  232. : (IntBuffer)(new ByteBufferAsIntBufferL(this,
  233. -1,
  234. 0,
  235. size,
  236. size,
  237. off)));
  238. }
  239. // long
  240. public long getLong() {
  241. return Bits.getLong(this, ix(nextGetIndex(8)), bigEndian);
  242. }
  243. public long getLong(int i) {
  244. return Bits.getLong(this, ix(checkIndex(i, 8)), bigEndian);
  245. }
  246. public ByteBuffer putLong(long x) {
  247. Bits.putLong(this, ix(nextPutIndex(8)), x, bigEndian);
  248. return this;
  249. }
  250. public ByteBuffer putLong(int i, long x) {
  251. Bits.putLong(this, ix(checkIndex(i, 8)), x, bigEndian);
  252. return this;
  253. }
  254. public LongBuffer asLongBuffer() {
  255. int size = this.remaining() >> 3;
  256. int off = offset + position();
  257. return (bigEndian
  258. ? (LongBuffer)(new ByteBufferAsLongBufferB(this,
  259. -1,
  260. 0,
  261. size,
  262. size,
  263. off))
  264. : (LongBuffer)(new ByteBufferAsLongBufferL(this,
  265. -1,
  266. 0,
  267. size,
  268. size,
  269. off)));
  270. }
  271. // float
  272. public float getFloat() {
  273. return Bits.getFloat(this, ix(nextGetIndex(4)), bigEndian);
  274. }
  275. public float getFloat(int i) {
  276. return Bits.getFloat(this, ix(checkIndex(i, 4)), bigEndian);
  277. }
  278. public ByteBuffer putFloat(float x) {
  279. Bits.putFloat(this, ix(nextPutIndex(4)), x, bigEndian);
  280. return this;
  281. }
  282. public ByteBuffer putFloat(int i, float x) {
  283. Bits.putFloat(this, ix(checkIndex(i, 4)), x, bigEndian);
  284. return this;
  285. }
  286. public FloatBuffer asFloatBuffer() {
  287. int size = this.remaining() >> 2;
  288. int off = offset + position();
  289. return (bigEndian
  290. ? (FloatBuffer)(new ByteBufferAsFloatBufferB(this,
  291. -1,
  292. 0,
  293. size,
  294. size,
  295. off))
  296. : (FloatBuffer)(new ByteBufferAsFloatBufferL(this,
  297. -1,
  298. 0,
  299. size,
  300. size,
  301. off)));
  302. }
  303. // double
  304. public double getDouble() {
  305. return Bits.getDouble(this, ix(nextGetIndex(8)), bigEndian);
  306. }
  307. public double getDouble(int i) {
  308. return Bits.getDouble(this, ix(checkIndex(i, 8)), bigEndian);
  309. }
  310. public ByteBuffer putDouble(double x) {
  311. Bits.putDouble(this, ix(nextPutIndex(8)), x, bigEndian);
  312. return this;
  313. }
  314. public ByteBuffer putDouble(int i, double x) {
  315. Bits.putDouble(this, ix(checkIndex(i, 8)), x, bigEndian);
  316. return this;
  317. }
  318. public DoubleBuffer asDoubleBuffer() {
  319. int size = this.remaining() >> 3;
  320. int off = offset + position();
  321. return (bigEndian
  322. ? (DoubleBuffer)(new ByteBufferAsDoubleBufferB(this,
  323. -1,
  324. 0,
  325. size,
  326. size,
  327. off))
  328. : (DoubleBuffer)(new ByteBufferAsDoubleBufferL(this,
  329. -1,
  330. 0,
  331. size,
  332. size,
  333. off)));
  334. }
  335. }