1 /* DoubleBuffer.java --
2    Copyright (C) 2002, 2003 Free Software Foundation, Inc.
3 
4 This file is part of GNU Classpath.
5 
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.
10 
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.
15 
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.
20 
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.
25 
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. */
37 
38 
39 package java.nio;
40 
41 /**
42  * @since 1.4
43  */
44 public abstract class DoubleBuffer extends Buffer
45   implements Comparable
46 {
47   int array_offset;
48   double[] backing_buffer;
49 
DoubleBuffer(int capacity, int limit, int position, int mark)50   DoubleBuffer (int capacity, int limit, int position, int mark)
51   {
52     super (capacity, limit, position, mark);
53     array_offset = 0;
54   }
55 
DoubleBuffer(double[] buffer, int offset, int capacity, int limit, int position, int mark)56   DoubleBuffer (double[] buffer, int offset, int capacity, int limit, int position, int mark)
57   {
58     super (capacity, limit, position, mark);
59     this.backing_buffer = buffer;
60     this.array_offset = offset;
61   }
62 
63   /**
64    * Allocates a new <code>DoubleBuffer</code> object with a given capacity.
65    */
allocate(int capacity)66   public static DoubleBuffer allocate (int capacity)
67   {
68     return new DoubleBufferImpl (capacity);
69   }
70 
71   /**
72    * Wraps a <code>double</code> array into a <code>DoubleBuffer</code>
73    * object.
74    *
75    * @exception IndexOutOfBoundsException If the preconditions on the offset
76    * and length parameters do not hold
77    */
wrap(double[] array, int offset, int length)78   final public static DoubleBuffer wrap (double[] array, int offset, int length)
79   {
80     return new DoubleBufferImpl (array, 0, array.length, offset + length, offset, -1, false);
81   }
82 
83   /**
84    * Wraps a <code>double</code> array into a <code>DoubleBuffer</code>
85    * object.
86    */
wrap(double[] array)87   final public static DoubleBuffer wrap (double[] array)
88   {
89     return wrap (array, 0, array.length);
90   }
91 
92   /**
93    * This method transfers <code>doubles<code> from this buffer into the given
94    * destination array.
95    *
96    * @param dst The destination array
97    * @param offset The offset within the array of the first <code>double</code>
98    * to be written; must be non-negative and no larger than dst.length.
99    * @param length The maximum number of bytes to be written to the given array;
100    * must be non-negative and no larger than dst.length - offset.
101    *
102    * @exception BufferUnderflowException If there are fewer than length
103    * <code>doubles</code> remaining in this buffer.
104    * @exception IndexOutOfBoundsException If the preconditions on the offset
105    * and length parameters do not hold.
106    */
get(double[] dst, int offset, int length)107   public DoubleBuffer get (double[] dst, int offset, int length)
108   {
109     for (int i = offset; i < offset + length; i++)
110       {
111         dst [i] = get ();
112       }
113 
114     return this;
115   }
116 
117   /**
118    * This method transfers <code>doubles<code> from this buffer into the given
119    * destination array.
120    *
121    * @param dst The byte array to write into.
122    *
123    * @exception BufferUnderflowException If there are fewer than dst.length
124    * <code>doubles</code> remaining in this buffer.
125    */
get(double[] dst)126   public DoubleBuffer get (double[] dst)
127   {
128     return get (dst, 0, dst.length);
129   }
130 
131   /**
132    * Writes the content of the the <code>DoubleBUFFER</code> src
133    * into the buffer.
134    *
135    * @param src The source data.
136    *
137    * @exception BufferOverflowException If there is insufficient space in this
138    * buffer for the remaining <code>doubles<code> in the source buffer.
139    * @exception IllegalArgumentException If the source buffer is this buffer.
140    * @exception ReadOnlyBufferException If this buffer is read-only.
141    */
put(DoubleBuffer src)142   public DoubleBuffer put (DoubleBuffer src)
143   {
144     if (src == this)
145       throw new IllegalArgumentException ();
146 
147     if (src.remaining () > remaining ())
148       throw new BufferOverflowException ();
149 
150     if (src.remaining () > 0)
151       {
152         double[] toPut = new double [src.remaining ()];
153         src.get (toPut);
154         src.put (toPut);
155       }
156 
157     return this;
158   }
159 
160   /**
161    * Writes the content of the the <code>double array</code> src
162    * into the buffer.
163    *
164    * @param src The array to copy into the buffer.
165    * @param offset The offset within the array of the first byte to be read;
166    * must be non-negative and no larger than src.length.
167    * @param length The number of bytes to be read from the given array;
168    * must be non-negative and no larger than src.length - offset.
169    *
170    * @exception BufferOverflowException If there is insufficient space in this
171    * buffer for the remaining <code>doubles<code> in the source array.
172    * @exception IndexOutOfBoundsException If the preconditions on the offset
173    * and length parameters do not hold
174    * @exception ReadOnlyBufferException If this buffer is read-only.
175    */
put(double[] src, int offset, int length)176   public DoubleBuffer put (double[] src, int offset, int length)
177   {
178     for (int i = offset; i < offset + length; i++)
179       put (src [i]);
180 
181     return this;
182   }
183 
184   /**
185    * Writes the content of the the <code>double array</code> src
186    * into the buffer.
187    *
188    * @param src The array to copy into the buffer.
189    *
190    * @exception BufferOverflowException If there is insufficient space in this
191    * buffer for the remaining <code>doubles<code> in the source array.
192    * @exception ReadOnlyBufferException If this buffer is read-only.
193    */
put(double[] src)194   public final DoubleBuffer put (double[] src)
195   {
196     return put (src, 0, src.length);
197   }
198 
199   /**
200    * Tells whether ot not this buffer is backed by an accessible
201    * <code>double</code> array.
202    */
hasArray()203   public final boolean hasArray ()
204   {
205     return (backing_buffer != null
206             && !isReadOnly ());
207   }
208 
209   /**
210    * Returns the <code>double</code> array that backs this buffer.
211    *
212    * @exception ReadOnlyBufferException If this buffer is read-only.
213    * @exception UnsupportedOperationException If this buffer is not backed
214    * by an accessible array.
215    */
array()216   public final double[] array ()
217   {
218     if (backing_buffer == null)
219       throw new UnsupportedOperationException ();
220 
221     if (isReadOnly ())
222       throw new ReadOnlyBufferException ();
223 
224     return backing_buffer;
225   }
226 
227   /**
228    * Returns the offset within this buffer's backing array of the first element.
229    *
230    * @exception ReadOnlyBufferException If this buffer is read-only.
231    * @exception UnsupportedOperationException If this buffer is not backed
232    * by an accessible array.
233    */
arrayOffset()234   public final int arrayOffset ()
235   {
236     if (backing_buffer == null)
237       throw new UnsupportedOperationException ();
238 
239     if (isReadOnly ())
240       throw new ReadOnlyBufferException ();
241 
242     return array_offset;
243   }
244 
245   /**
246    * Calculates a hash code for this buffer.
247    */
hashCode()248   public int hashCode ()
249   {
250     // FIXME: Check what SUN calculates here.
251     return super.hashCode ();
252   }
253 
254   /**
255    * Checks if this buffer is equal to obj.
256    */
equals(Object obj)257   public boolean equals (Object obj)
258   {
259     if (obj instanceof DoubleBuffer)
260       {
261         return compareTo (obj) == 0;
262       }
263 
264     return false;
265   }
266 
267   /**
268    * Compares two <code>DoubleBuffer</code> objects.
269    *
270    * @exception ClassCastException If obj is not an object derived from
271    * <code>DoubleBuffer</code>.
272    */
compareTo(Object obj)273   public int compareTo (Object obj)
274   {
275     DoubleBuffer a = (DoubleBuffer) obj;
276 
277     if (a.remaining () != remaining ())
278       return 1;
279 
280     if (! hasArray () ||
281         ! a.hasArray ())
282       {
283         return 1;
284       }
285 
286     int r = remaining ();
287     int i1 = position ();
288     int i2 = a.position ();
289 
290     for (int i = 0; i < r; i++)
291       {
292         int t = (int) (get (i1) - a.get (i2));
293 
294         if (t != 0)
295           {
296             return (int) t;
297           }
298       }
299 
300     return 0;
301   }
302 
303   /**
304    * Returns the byte order of this buffer.
305    */
order()306   public abstract ByteOrder order ();
307 
308   /**
309    * Reads the <code>double</code> at this buffer's current position,
310    * and then increments the position.
311    *
312    * @exception BufferUnderflowException If there are no remaining
313    * <code>doubles</code> in this buffer.
314    */
get()315   public abstract double get ();
316 
317   /**
318    * Writes the <code>double</code> at this buffer's current position,
319    * and then increments the position.
320    *
321    * @exception BufferOverflowException If there no remaining
322    * <code>doubles</code> in this buffer.
323    * @exception ReadOnlyBufferException If this buffer is read-only.
324    */
put(double b)325   public abstract DoubleBuffer put (double b);
326 
327   /**
328    * Absolute get method.
329    *
330    * @exception IndexOutOfBoundsException If index is negative or not smaller
331    * than the buffer's limit.
332    */
get(int index)333   public abstract double get (int index);
334 
335   /**
336    * Absolute put method.
337    *
338    * @exception IndexOutOfBoundsException If index is negative or not smaller
339    * than the buffer's limit.
340    * @exception ReadOnlyBufferException If this buffer is read-only.
341    */
put(int index, double b)342   public abstract DoubleBuffer put (int index, double b);
343 
344   /**
345    * Compacts this buffer.
346    *
347    * @exception ReadOnlyBufferException If this buffer is read-only.
348    */
compact()349   public abstract DoubleBuffer compact ();
350 
351   /**
352    * Tells wether or not this buffer is direct.
353    */
isDirect()354   public abstract boolean isDirect ();
355 
356   /**
357    * Creates a new <code>DoubleBuffer</code> whose content is a shared
358    * subsequence of this buffer's content.
359    */
slice()360   public abstract DoubleBuffer slice ();
361 
362   /**
363    * Creates a new <code>DoubleBuffer</code> that shares this buffer's
364    * content.
365    */
duplicate()366   public abstract DoubleBuffer duplicate ();
367 
368   /**
369    * Creates a new read-only <code>DoubleBuffer</code> that shares this
370    * buffer's content.
371    */
asReadOnlyBuffer()372   public abstract DoubleBuffer asReadOnlyBuffer ();
373 }
374