Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / nio / DoubleBuffer.java
blob70010508e896e5287d37b387c79b649f3f9d09fd
1 /* DoubleBuffer.java --
2 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.nio;
41 /**
42 * @since 1.4
44 public abstract class DoubleBuffer extends Buffer
45 implements Comparable
47 int array_offset;
48 double[] backing_buffer;
50 DoubleBuffer (int capacity, int limit, int position, int mark)
52 super (capacity, limit, position, mark);
53 array_offset = 0;
56 /**
57 * Allocates a new <code>DoubleBuffer</code> object with a given capacity.
59 public static DoubleBuffer allocate (int capacity)
61 return new DoubleBufferImpl (capacity);
64 /**
65 * Wraps a <code>double</code> array into a <code>DoubleBuffer</code>
66 * object.
68 * @exception IndexOutOfBoundsException If the preconditions on the offset
69 * and length parameters do not hold
71 public static final DoubleBuffer wrap (double[] array, int offset, int length)
73 return new DoubleBufferImpl (array, 0, array.length, offset + length, offset, -1, false);
76 /**
77 * Wraps a <code>double</code> array into a <code>DoubleBuffer</code>
78 * object.
80 public static final DoubleBuffer wrap (double[] array)
82 return wrap (array, 0, array.length);
85 /**
86 * This method transfers <code>double</code>s from this buffer into the given
87 * destination array. Before the transfer, it checks if there are fewer than
88 * length <code>double</code>s remaining in this buffer.
90 * @param dst The destination array
91 * @param offset The offset within the array of the first <code>double</code>
92 * to be written; must be non-negative and no larger than dst.length.
93 * @param length The maximum number of bytes to be written to the given array;
94 * must be non-negative and no larger than dst.length - offset.
96 * @exception BufferUnderflowException If there are fewer than length
97 * <code>double</code>s remaining in this buffer.
98 * @exception IndexOutOfBoundsException If the preconditions on the offset
99 * and length parameters do not hold.
101 public DoubleBuffer get (double[] dst, int offset, int length)
103 checkArraySize(dst.length, offset, length);
104 checkForUnderflow(length);
106 for (int i = offset; i < offset + length; i++)
108 dst [i] = get ();
111 return this;
115 * This method transfers <code>double</code>s from this buffer into the given
116 * destination array.
118 * @param dst The byte array to write into.
120 * @exception BufferUnderflowException If there are fewer than dst.length
121 * <code>double</code>s remaining in this buffer.
123 public DoubleBuffer get (double[] dst)
125 return get (dst, 0, dst.length);
129 * Writes the content of the the <code>DoubleBUFFER</code> src
130 * into the buffer. Before the transfer, it checks if there is fewer than
131 * <code>src.remaining()</code> space remaining in this buffer.
133 * @param src The source data.
135 * @exception BufferOverflowException If there is insufficient space in this
136 * buffer for the remaining <code>double</code>s in the source buffer.
137 * @exception IllegalArgumentException If the source buffer is this buffer.
138 * @exception ReadOnlyBufferException If this buffer is read-only.
140 public DoubleBuffer put (DoubleBuffer src)
142 if (src == this)
143 throw new IllegalArgumentException ();
145 checkForOverflow(src.remaining ());
147 if (src.remaining () > 0)
149 double[] toPut = new double [src.remaining ()];
150 src.get (toPut);
151 put (toPut);
154 return this;
158 * Writes the content of the the <code>double array</code> src
159 * into the buffer. Before the transfer, it checks if there is fewer than
160 * length space remaining in this buffer.
162 * @param src The array to copy into the buffer.
163 * @param offset The offset within the array of the first byte to be read;
164 * must be non-negative and no larger than src.length.
165 * @param length The number of bytes to be read from the given array;
166 * must be non-negative and no larger than src.length - offset.
168 * @exception BufferOverflowException If there is insufficient space in this
169 * buffer for the remaining <code>double</code>s in the source array.
170 * @exception IndexOutOfBoundsException If the preconditions on the offset
171 * and length parameters do not hold
172 * @exception ReadOnlyBufferException If this buffer is read-only.
174 public DoubleBuffer put (double[] src, int offset, int length)
176 checkArraySize(src.length, offset, length);
177 checkForOverflow(length);
179 for (int i = offset; i < offset + length; i++)
180 put (src [i]);
182 return this;
186 * Writes the content of the the <code>double array</code> src
187 * into the buffer.
189 * @param src The array to copy into the buffer.
191 * @exception BufferOverflowException If there is insufficient space in this
192 * buffer for the remaining <code>double</code>s in the source array.
193 * @exception ReadOnlyBufferException If this buffer is read-only.
195 public final DoubleBuffer put (double[] src)
197 return put (src, 0, src.length);
201 * Tells whether ot not this buffer is backed by an accessible
202 * <code>double</code> array.
204 public final boolean hasArray ()
206 return (backing_buffer != null
207 && !isReadOnly ());
211 * Returns the <code>double</code> array that backs this buffer.
213 * @exception ReadOnlyBufferException If this buffer is read-only.
214 * @exception UnsupportedOperationException If this buffer is not backed
215 * by an accessible array.
217 public final double[] array ()
219 if (backing_buffer == null)
220 throw new UnsupportedOperationException ();
222 checkIfReadOnly();
224 return backing_buffer;
228 * Returns the offset within this buffer's backing array of the first element.
230 * @exception ReadOnlyBufferException If this buffer is read-only.
231 * @exception UnsupportedOperationException If this buffer is not backed
232 * by an accessible array.
234 public final int arrayOffset ()
236 if (backing_buffer == null)
237 throw new UnsupportedOperationException ();
239 checkIfReadOnly();
241 return array_offset;
245 * Calculates a hash code for this buffer.
247 * This is done with <code>long</code> arithmetic,
248 * where ** represents exponentiation, by this formula:<br>
249 * <code>s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... +
250 * (s[limit()-1]+30)*31**(limit()-1)</code>.
251 * Where s is the buffer data, in Double.doubleToLongBits() form
252 * Note that the hashcode is dependent on buffer content,
253 * and therefore is not useful if the buffer content may change.
255 * @return the hash code (casted to int)
257 public int hashCode ()
259 long hashCode = Double.doubleToLongBits(get(position())) + 31;
260 long multiplier = 1;
261 for (int i = position() + 1; i < limit(); ++i)
263 multiplier *= 31;
264 hashCode += (Double.doubleToLongBits(get(i)) + 30)*multiplier;
266 return ((int)hashCode);
270 * Checks if this buffer is equal to obj.
272 public boolean equals (Object obj)
274 if (obj instanceof DoubleBuffer)
276 return compareTo (obj) == 0;
279 return false;
283 * Compares two <code>DoubleBuffer</code> objects.
285 * @exception ClassCastException If obj is not an object derived from
286 * <code>DoubleBuffer</code>.
288 public int compareTo (Object obj)
290 DoubleBuffer other = (DoubleBuffer) obj;
292 int num = Math.min(remaining(), other.remaining());
293 int pos_this = position();
294 int pos_other = other.position();
296 for (int count = 0; count < num; count++)
298 double a = get(pos_this++);
299 double b = other.get(pos_other++);
301 if (a == b)
302 continue;
304 if (a < b)
305 return -1;
307 return 1;
310 return remaining() - other.remaining();
314 * Returns the byte order of this buffer.
316 public abstract ByteOrder order ();
319 * Reads the <code>double</code> at this buffer's current position,
320 * and then increments the position.
322 * @exception BufferUnderflowException If there are no remaining
323 * <code>double</code>s in this buffer.
325 public abstract double get ();
328 * Writes the <code>double</code> at this buffer's current position,
329 * and then increments the position.
331 * @exception BufferOverflowException If there no remaining
332 * <code>double</code>s in this buffer.
333 * @exception ReadOnlyBufferException If this buffer is read-only.
335 public abstract DoubleBuffer put (double b);
338 * Absolute get method.
340 * @exception IndexOutOfBoundsException If index is negative or not smaller
341 * than the buffer's limit.
343 public abstract double get (int index);
346 * Absolute put method.
348 * @exception IndexOutOfBoundsException If index is negative or not smaller
349 * than the buffer's limit.
350 * @exception ReadOnlyBufferException If this buffer is read-only.
352 public abstract DoubleBuffer put (int index, double b);
355 * Compacts this buffer.
357 * @exception ReadOnlyBufferException If this buffer is read-only.
359 public abstract DoubleBuffer compact ();
362 * Tells wether or not this buffer is direct.
364 public abstract boolean isDirect ();
367 * Creates a new <code>DoubleBuffer</code> whose content is a shared
368 * subsequence of this buffer's content.
370 public abstract DoubleBuffer slice ();
373 * Creates a new <code>DoubleBuffer</code> that shares this buffer's
374 * content.
376 public abstract DoubleBuffer duplicate ();
379 * Creates a new read-only <code>DoubleBuffer</code> that shares this
380 * buffer's content.
382 public abstract DoubleBuffer asReadOnlyBuffer ();