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);
57 * Allocates a new <code>ShortBuffer</code> object with a given capacity.
59 public static ShortBuffer allocate (int capacity)
61 return new ShortBufferImpl (capacity);
65 * Wraps a <code>short</code> array into a <code>ShortBuffer</code>
68 * @exception IndexOutOfBoundsException If the preconditions on the offset
69 * and length parameters do not hold
71 final public static ShortBuffer wrap (short[] array, int offset, int length)
73 return new ShortBufferImpl (array, 0, array.length, offset + length, offset, -1, false);
77 * Wraps a <code>short</code> array into a <code>ShortBuffer</code>
80 final public static ShortBuffer wrap (short[] array)
82 return wrap (array, 0, array.length);
86 * This method transfers <code>shorts<code> from this buffer into the given
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.
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.
100 public ShortBuffer get (short[] dst, int offset, int length)
102 for (int i = offset; i < offset + length; i++)
111 * This method transfers <code>shorts<code> from this buffer into the given
114 * @param dst The byte array to write into.
116 * @exception BufferUnderflowException If there are fewer than dst.length
117 * <code>shorts</code> remaining in this buffer.
119 public ShortBuffer get (short[] dst)
121 return get (dst, 0, dst.length);
125 * Writes the content of the the <code>ShortBUFFER</code> src
128 * @param src The source data.
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.
135 public ShortBuffer put (ShortBuffer src)
138 throw new IllegalArgumentException ();
140 if (src.remaining () > remaining ())
141 throw new BufferOverflowException ();
143 if (src.remaining () > 0)
145 short[] toPut = new short [src.remaining ()];
154 * Writes the content of the the <code>short array</code> src
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.
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.
169 public ShortBuffer put (short[] src, int offset, int length)
171 for (int i = offset; i < offset + length; i++)
178 * Writes the content of the the <code>short array</code> src
181 * @param src The array to copy into the buffer.
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.
187 public final ShortBuffer put (short[] src)
189 return put (src, 0, src.length);
193 * Tells whether ot not this buffer is backed by an accessible
194 * <code>short</code> array.
196 public final boolean hasArray ()
198 return (backing_buffer != null
203 * Returns the <code>short</code> array that backs this buffer.
205 * @exception ReadOnlyBufferException If this buffer is read-only.
206 * @exception UnsupportedOperationException If this buffer is not backed
207 * by an accessible array.
209 public final short[] array ()
211 if (backing_buffer == null)
212 throw new UnsupportedOperationException ();
215 throw new ReadOnlyBufferException ();
217 return backing_buffer;
221 * Returns the offset within this buffer's backing array of the first element.
223 * @exception ReadOnlyBufferException If this buffer is read-only.
224 * @exception UnsupportedOperationException If this buffer is not backed
225 * by an accessible array.
227 public final int arrayOffset ()
229 if (backing_buffer == null)
230 throw new UnsupportedOperationException ();
233 throw new ReadOnlyBufferException ();
239 * Calculates a hash code for this buffer.
241 public int hashCode ()
243 // FIXME: Check what SUN calculates here.
244 return super.hashCode ();
248 * Checks if this buffer is equal to obj.
250 public boolean equals (Object obj)
252 if (obj instanceof ShortBuffer)
254 return compareTo (obj) == 0;
261 * Compares two <code>ShortBuffer</code> objects.
263 * @exception ClassCastException If obj is not an object derived from
264 * <code>ShortBuffer</code>.
266 public int compareTo (Object obj)
268 ShortBuffer a = (ShortBuffer) obj;
270 if (a.remaining () != remaining ())
279 int r = remaining ();
280 int i1 = position ();
281 int i2 = a.position ();
283 for (int i = 0; i < r; i++)
285 int t = (int) (get (i1) - a.get (i2));
297 * Returns the byte order of this buffer.
299 public abstract ByteOrder order ();
302 * Reads the <code>short</code> at this buffer's current position,
303 * and then increments the position.
305 * @exception BufferUnderflowException If there are no remaining
306 * <code>shorts</code> in this buffer.
308 public abstract short get ();
311 * Writes the <code>short</code> at this buffer's current position,
312 * and then increments the position.
314 * @exception BufferOverflowException If there no remaining
315 * <code>shorts</code> in this buffer.
316 * @exception ReadOnlyBufferException If this buffer is read-only.
318 public abstract ShortBuffer put (short b);
321 * Absolute get method.
323 * @exception IndexOutOfBoundsException If index is negative or not smaller
324 * than the buffer's limit.
326 public abstract short get (int index);
329 * Absolute put method.
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.
335 public abstract ShortBuffer put (int index, short b);
338 * Compacts this buffer.
340 * @exception ReadOnlyBufferException If this buffer is read-only.
342 public abstract ShortBuffer compact ();
345 * Tells wether or not this buffer is direct.
347 public abstract boolean isDirect ();
350 * Creates a new <code>ShortBuffer</code> whose content is a shared
351 * subsequence of this buffer's content.
353 public abstract ShortBuffer slice ();
356 * Creates a new <code>ShortBuffer</code> that shares this buffer's
359 public abstract ShortBuffer duplicate ();
362 * Creates a new read-only <code>ShortBuffer</code> that shares this
365 public abstract ShortBuffer asReadOnlyBuffer ();