2 Copyright (C) 2002, 2003 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)
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
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
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. */
44 public abstract class ShortBuffer
extends Buffer
48 short[] backing_buffer
;
50 ShortBuffer (int capacity
, int limit
, int position
, int mark
)
52 super (capacity
, limit
, position
, mark
);
56 ShortBuffer (short[] buffer
, int offset
, int capacity
, int limit
, int position
, int mark
)
58 super (capacity
, limit
, position
, mark
);
59 this.backing_buffer
= buffer
;
60 this.array_offset
= offset
;
64 * Allocates a new <code>ShortBuffer</code> object with a given capacity.
66 public static ShortBuffer
allocate (int capacity
)
68 return new ShortBufferImpl (capacity
);
72 * Wraps a <code>short</code> array into a <code>ShortBuffer</code>
75 * @exception IndexOutOfBoundsException If the preconditions on the offset
76 * and length parameters do not hold
78 final public static ShortBuffer
wrap (short[] array
, int offset
, int length
)
80 return new ShortBufferImpl (array
, 0, array
.length
, offset
+ length
, offset
, -1, false);
84 * Wraps a <code>short</code> array into a <code>ShortBuffer</code>
87 final public static ShortBuffer
wrap (short[] array
)
89 return wrap (array
, 0, array
.length
);
93 * This method transfers <code>shorts<code> from this buffer into the given
96 * @param dst The destination array
97 * @param offset The offset within the array of the first <code>short</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.
102 * @exception BufferUnderflowException If there are fewer than length
103 * <code>shorts</code> remaining in this buffer.
104 * @exception IndexOutOfBoundsException If the preconditions on the offset
105 * and length parameters do not hold.
107 public ShortBuffer
get (short[] dst
, int offset
, int length
)
109 for (int i
= offset
; i
< offset
+ length
; i
++)
118 * This method transfers <code>shorts<code> from this buffer into the given
121 * @param dst The byte array to write into.
123 * @exception BufferUnderflowException If there are fewer than dst.length
124 * <code>shorts</code> remaining in this buffer.
126 public ShortBuffer
get (short[] dst
)
128 return get (dst
, 0, dst
.length
);
132 * Writes the content of the the <code>ShortBUFFER</code> src
135 * @param src The source data.
137 * @exception BufferOverflowException If there is insufficient space in this
138 * buffer for the remaining <code>shorts<code> in the source buffer.
139 * @exception IllegalArgumentException If the source buffer is this buffer.
140 * @exception ReadOnlyBufferException If this buffer is read-only.
142 public ShortBuffer
put (ShortBuffer src
)
145 throw new IllegalArgumentException ();
147 if (src
.remaining () > remaining ())
148 throw new BufferOverflowException ();
150 if (src
.remaining () > 0)
152 short[] toPut
= new short [src
.remaining ()];
161 * Writes the content of the the <code>short array</code> src
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.
170 * @exception BufferOverflowException If there is insufficient space in this
171 * buffer for the remaining <code>shorts<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.
176 public ShortBuffer
put (short[] src
, int offset
, int length
)
178 for (int i
= offset
; i
< offset
+ length
; i
++)
185 * Writes the content of the the <code>short array</code> src
188 * @param src The array to copy into the buffer.
190 * @exception BufferOverflowException If there is insufficient space in this
191 * buffer for the remaining <code>shorts<code> in the source array.
192 * @exception ReadOnlyBufferException If this buffer is read-only.
194 public final ShortBuffer
put (short[] src
)
196 return put (src
, 0, src
.length
);
200 * Tells whether ot not this buffer is backed by an accessible
201 * <code>short</code> array.
203 public final boolean hasArray ()
205 return (backing_buffer
!= null
210 * Returns the <code>short</code> array that backs this buffer.
212 * @exception ReadOnlyBufferException If this buffer is read-only.
213 * @exception UnsupportedOperationException If this buffer is not backed
214 * by an accessible array.
216 public final short[] array ()
218 if (backing_buffer
== null)
219 throw new UnsupportedOperationException ();
222 throw new ReadOnlyBufferException ();
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 ();
240 throw new ReadOnlyBufferException ();
246 * Calculates a hash code for this buffer.
248 public int hashCode ()
250 // FIXME: Check what SUN calculates here.
251 return super.hashCode ();
255 * Checks if this buffer is equal to obj.
257 public boolean equals (Object obj
)
259 if (obj
instanceof ShortBuffer
)
261 return compareTo (obj
) == 0;
268 * Compares two <code>ShortBuffer</code> objects.
270 * @exception ClassCastException If obj is not an object derived from
271 * <code>ShortBuffer</code>.
273 public int compareTo (Object obj
)
275 ShortBuffer a
= (ShortBuffer
) obj
;
277 if (a
.remaining () != remaining ())
286 int r
= remaining ();
287 int i1
= position ();
288 int i2
= a
.position ();
290 for (int i
= 0; i
< r
; i
++)
292 int t
= (int) (get (i1
) - a
.get (i2
));
304 * Returns the byte order of this buffer.
306 public abstract ByteOrder
order ();
309 * Reads the <code>short</code> at this buffer's current position,
310 * and then increments the position.
312 * @exception BufferUnderflowException If there are no remaining
313 * <code>shorts</code> in this buffer.
315 public abstract short get ();
318 * Writes the <code>short</code> at this buffer's current position,
319 * and then increments the position.
321 * @exception BufferOverflowException If there no remaining
322 * <code>shorts</code> in this buffer.
323 * @exception ReadOnlyBufferException If this buffer is read-only.
325 public abstract ShortBuffer
put (short b
);
328 * Absolute get method.
330 * @exception IndexOutOfBoundsException If index is negative or not smaller
331 * than the buffer's limit.
333 public abstract short get (int index
);
336 * Absolute put method.
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.
342 public abstract ShortBuffer
put (int index
, short b
);
345 * Compacts this buffer.
347 * @exception ReadOnlyBufferException If this buffer is read-only.
349 public abstract ShortBuffer
compact ();
352 * Tells wether or not this buffer is direct.
354 public abstract boolean isDirect ();
357 * Creates a new <code>ShortBuffer</code> whose content is a shared
358 * subsequence of this buffer's content.
360 public abstract ShortBuffer
slice ();
363 * Creates a new <code>ShortBuffer</code> that shares this buffer's
366 public abstract ShortBuffer
duplicate ();
369 * Creates a new read-only <code>ShortBuffer</code> that shares this
372 public abstract ShortBuffer
asReadOnlyBuffer ();