ShortViewBufferImpl.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* ShortViewBufferImpl.java --
  2. Copyright (C) 2003, 2004 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. final class ShortViewBufferImpl extends ShortBuffer
  33. {
  34. /** Position in bb (i.e. a byte offset) where this buffer starts. */
  35. private final int offset;
  36. private final ByteBuffer bb;
  37. private final boolean readOnly;
  38. private final ByteOrder endian;
  39. ShortViewBufferImpl (ByteBuffer bb, int capacity)
  40. {
  41. super (capacity, capacity, 0, -1, bb.isDirect() ?
  42. VMDirectByteBuffer.adjustAddress(bb.address, bb.position()):null,
  43. null, 0);
  44. this.bb = bb;
  45. this.offset = bb.position();
  46. this.readOnly = bb.isReadOnly();
  47. this.endian = bb.order();
  48. }
  49. public ShortViewBufferImpl (ByteBuffer bb, int offset, int capacity,
  50. int limit, int position, int mark,
  51. boolean readOnly, ByteOrder endian)
  52. {
  53. super (capacity, limit, position, mark, bb.isDirect() ?
  54. VMDirectByteBuffer.adjustAddress(bb.address, offset):null,
  55. null, 0);
  56. this.bb = bb;
  57. this.offset = offset;
  58. this.readOnly = readOnly;
  59. this.endian = endian;
  60. }
  61. /**
  62. * Reads the <code>short</code> at this buffer's current position,
  63. * and then increments the position.
  64. *
  65. * @exception BufferUnderflowException If there are no remaining
  66. * <code>short</code>s in this buffer.
  67. */
  68. public short get ()
  69. {
  70. int p = position();
  71. short result = ByteBufferHelper.getShort(bb, (p << 1) + offset, endian);
  72. position(p + 1);
  73. return result;
  74. }
  75. /**
  76. * Absolute get method. Reads the <code>short</code> at position
  77. * <code>index</code>.
  78. *
  79. * @exception IndexOutOfBoundsException If index is negative or not smaller
  80. * than the buffer's limit.
  81. */
  82. public short get (int index)
  83. {
  84. return ByteBufferHelper.getShort(bb, (index << 1) + offset, endian);
  85. }
  86. public ShortBuffer put (short value)
  87. {
  88. int p = position();
  89. ByteBufferHelper.putShort(bb, (p << 1) + offset, value, endian);
  90. position(p + 1);
  91. return this;
  92. }
  93. public ShortBuffer put (int index, short value)
  94. {
  95. ByteBufferHelper.putShort(bb, (index << 1) + offset, value, endian);
  96. return this;
  97. }
  98. public ShortBuffer compact ()
  99. {
  100. if (position () > 0)
  101. {
  102. int count = limit () - position ();
  103. bb.shiftDown(offset, offset + 2 * position(), 2 * count);
  104. position (count);
  105. limit (capacity ());
  106. }
  107. else
  108. {
  109. position(limit());
  110. limit(capacity());
  111. }
  112. return this;
  113. }
  114. public ShortBuffer slice ()
  115. {
  116. // Create a sliced copy of this object that shares its content.
  117. return new ShortViewBufferImpl (bb, (position () << 1) + offset,
  118. remaining(), remaining(), 0, -1,
  119. readOnly, endian);
  120. }
  121. ShortBuffer duplicate (boolean readOnly)
  122. {
  123. int pos = position();
  124. reset();
  125. int mark = position();
  126. position(pos);
  127. return new ShortViewBufferImpl (bb, offset, capacity(), limit(),
  128. pos, mark, readOnly, endian);
  129. }
  130. public ShortBuffer duplicate ()
  131. {
  132. return duplicate(readOnly);
  133. }
  134. public ShortBuffer asReadOnlyBuffer ()
  135. {
  136. return duplicate(true);
  137. }
  138. public boolean isReadOnly ()
  139. {
  140. return readOnly;
  141. }
  142. public boolean isDirect ()
  143. {
  144. return bb.isDirect ();
  145. }
  146. public ByteOrder order ()
  147. {
  148. return endian;
  149. }
  150. }