OSDN Git Service

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