FloatViewBufferImpl.java 5.0 KB

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