DoubleBufferImpl.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* DoubleBufferImpl.java --
  2. Copyright (C) 2002, 2003, 2004, 2005 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. /**
  33. * This is a Heap memory implementation
  34. */
  35. final class DoubleBufferImpl extends DoubleBuffer
  36. {
  37. private final boolean readOnly;
  38. DoubleBufferImpl (int capacity)
  39. {
  40. this (new double [capacity], 0, capacity, capacity, 0, -1, false);
  41. }
  42. DoubleBufferImpl (double[] buffer, int offset, int capacity, int limit,
  43. int position, int mark, boolean readOnly)
  44. {
  45. super (capacity, limit, position, mark, null, buffer, offset);
  46. this.readOnly = readOnly;
  47. }
  48. public boolean isReadOnly ()
  49. {
  50. return readOnly;
  51. }
  52. public DoubleBuffer slice ()
  53. {
  54. return new DoubleBufferImpl (backing_buffer, array_offset + position (),
  55. remaining (), remaining (), 0, -1, isReadOnly ());
  56. }
  57. public DoubleBuffer duplicate ()
  58. {
  59. return new DoubleBufferImpl (backing_buffer, array_offset, capacity (),
  60. limit (), position (), mark, isReadOnly ());
  61. }
  62. public DoubleBuffer asReadOnlyBuffer ()
  63. {
  64. return new DoubleBufferImpl (backing_buffer, array_offset, capacity (),
  65. limit (), position (), mark, true);
  66. }
  67. public DoubleBuffer compact ()
  68. {
  69. checkIfReadOnly();
  70. mark = -1;
  71. int p = position();
  72. int n = limit() - p;
  73. if (n > 0)
  74. {
  75. System.arraycopy(backing_buffer, array_offset + p,
  76. backing_buffer, array_offset, n);
  77. }
  78. position(n);
  79. limit(capacity());
  80. return this;
  81. }
  82. public boolean isDirect ()
  83. {
  84. return false;
  85. }
  86. /**
  87. * Reads the <code>double</code> at this buffer's current position,
  88. * and then increments the position.
  89. *
  90. * @exception BufferUnderflowException If there are no remaining
  91. * <code>double</code>s in this buffer.
  92. */
  93. public double get ()
  94. {
  95. checkForUnderflow();
  96. double result = backing_buffer [position ()];
  97. position (position () + 1);
  98. return result;
  99. }
  100. /**
  101. * Relative put method. Writes <code>value</code> to the next position
  102. * in the buffer.
  103. *
  104. * @exception BufferOverflowException If there no remaining
  105. * space in this buffer.
  106. * @exception ReadOnlyBufferException If this buffer is read-only.
  107. */
  108. public DoubleBuffer put (double value)
  109. {
  110. checkIfReadOnly();
  111. checkForOverflow();
  112. backing_buffer [position ()] = value;
  113. position (position () + 1);
  114. return this;
  115. }
  116. /**
  117. * Absolute get method. Reads the <code>double</code> at position
  118. * <code>index</code>.
  119. *
  120. * @exception IndexOutOfBoundsException If index is negative or not smaller
  121. * than the buffer's limit.
  122. */
  123. public double get (int index)
  124. {
  125. checkIndex(index);
  126. return backing_buffer [index];
  127. }
  128. /**
  129. * Absolute put method. Writes <code>value</code> to position
  130. * <code>index</code> in the buffer.
  131. *
  132. * @exception IndexOutOfBoundsException If index is negative or not smaller
  133. * than the buffer's limit.
  134. * @exception ReadOnlyBufferException If this buffer is read-only.
  135. */
  136. public DoubleBuffer put (int index, double value)
  137. {
  138. checkIfReadOnly();
  139. checkIndex(index);
  140. backing_buffer [index] = value;
  141. return this;
  142. }
  143. public ByteOrder order ()
  144. {
  145. return ByteOrder.nativeOrder ();
  146. }
  147. }