2 Copyright (C) 2002, 2003, 2004, 2005 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., 51 Franklin Street, Fifth Floor, 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. */
41 // GCJ LOCAL: Change gnu.classpath.Pointer to RawData
42 import gnu.gcj.RawData;
47 public abstract class IntBuffer extends Buffer
48 implements Comparable<IntBuffer>
50 final int array_offset;
51 final int[] backing_buffer;
53 IntBuffer (int capacity, int limit, int position, int mark,
54 RawData address, int[] backing_buffer, int array_offset)
56 super (capacity, limit, position, mark, address);
57 this.backing_buffer = backing_buffer;
58 this.array_offset = array_offset;
62 * Allocates a new <code>IntBuffer</code> object with a given capacity.
64 public static IntBuffer allocate (int capacity)
66 return new IntBufferImpl (capacity);
70 * Wraps a <code>int</code> array into a <code>IntBuffer</code>
73 * @exception IndexOutOfBoundsException If the preconditions on the offset
74 * and length parameters do not hold
76 public static final IntBuffer wrap (int[] array, int offset, int length)
78 return new IntBufferImpl (array, 0, array.length, offset + length, offset,
83 * Wraps a <code>int</code> array into a <code>IntBuffer</code>
86 public static final IntBuffer wrap (int[] array)
88 return wrap (array, 0, array.length);
92 * This method transfers <code>int</code>s from this buffer into the given
93 * destination array. Before the transfer, it checks if there are fewer than
94 * length <code>int</code>s remaining in this buffer.
96 * @param dst The destination array
97 * @param offset The offset within the array of the first <code>int</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>int</code>s remaining in this buffer.
104 * @exception IndexOutOfBoundsException If the preconditions on the offset
105 * and length parameters do not hold.
107 public IntBuffer get (int[] dst, int offset, int length)
109 checkArraySize(dst.length, offset, length);
110 checkForUnderflow(length);
112 for (int i = offset; i < offset + length; i++)
121 * This method transfers <code>int</code>s from this buffer into the given
124 * @param dst The byte array to write into.
126 * @exception BufferUnderflowException If there are fewer than dst.length
127 * <code>int</code>s remaining in this buffer.
129 public IntBuffer get (int[] dst)
131 return get (dst, 0, dst.length);
135 * Writes the content of the the <code>IntBUFFER</code> src
136 * into the buffer. Before the transfer, it checks if there is fewer than
137 * <code>src.remaining()</code> space remaining in this buffer.
139 * @param src The source data.
141 * @exception BufferOverflowException If there is insufficient space in this
142 * buffer for the remaining <code>int</code>s in the source buffer.
143 * @exception IllegalArgumentException If the source buffer is this buffer.
144 * @exception ReadOnlyBufferException If this buffer is read-only.
146 public IntBuffer put (IntBuffer src)
149 throw new IllegalArgumentException ();
151 checkForOverflow(src.remaining ());
153 if (src.remaining () > 0)
155 int[] toPut = new int [src.remaining ()];
164 * Writes the content of the the <code>int array</code> src
165 * into the buffer. Before the transfer, it checks if there is fewer than
166 * length space remaining in this buffer.
168 * @param src The array to copy into the buffer.
169 * @param offset The offset within the array of the first byte to be read;
170 * must be non-negative and no larger than src.length.
171 * @param length The number of bytes to be read from the given array;
172 * must be non-negative and no larger than src.length - offset.
174 * @exception BufferOverflowException If there is insufficient space in this
175 * buffer for the remaining <code>int</code>s in the source array.
176 * @exception IndexOutOfBoundsException If the preconditions on the offset
177 * and length parameters do not hold
178 * @exception ReadOnlyBufferException If this buffer is read-only.
180 public IntBuffer put (int[] src, int offset, int length)
182 checkArraySize(src.length, offset, length);
183 checkForOverflow(length);
185 for (int i = offset; i < offset + length; i++)
192 * Writes the content of the the <code>int array</code> src
195 * @param src The array to copy into the buffer.
197 * @exception BufferOverflowException If there is insufficient space in this
198 * buffer for the remaining <code>int</code>s in the source array.
199 * @exception ReadOnlyBufferException If this buffer is read-only.
201 public final IntBuffer put (int[] src)
203 return put (src, 0, src.length);
207 * Tells whether ot not this buffer is backed by an accessible
208 * <code>int</code> array.
210 public final boolean hasArray ()
212 return (backing_buffer != null
217 * Returns the <code>int</code> array that backs this buffer.
219 * @exception ReadOnlyBufferException If this buffer is read-only.
220 * @exception UnsupportedOperationException If this buffer is not backed
221 * by an accessible array.
223 public final int[] array ()
225 if (backing_buffer == null)
226 throw new UnsupportedOperationException ();
230 return backing_buffer;
234 * Returns the offset within this buffer's backing array of the first element.
236 * @exception ReadOnlyBufferException If this buffer is read-only.
237 * @exception UnsupportedOperationException If this buffer is not backed
238 * by an accessible array.
240 public final int arrayOffset ()
242 if (backing_buffer == null)
243 throw new UnsupportedOperationException ();
251 * Calculates a hash code for this buffer.
253 * This is done with <code>int</code> arithmetic,
254 * where ** represents exponentiation, by this formula:<br>
255 * <code>s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... +
256 * (s[limit()-1]+30)*31**(limit()-1)</code>.
257 * Where s is the buffer data. Note that the hashcode is dependent
258 * on buffer content, and therefore is not useful if the buffer
259 * content may change.
261 * @return the hash code
263 public int hashCode ()
265 int hashCode = get(position()) + 31;
267 for (int i = position() + 1; i < limit(); ++i)
270 hashCode += (get(i) + 30)*multiplier;
276 * Checks if this buffer is equal to obj.
278 public boolean equals (Object obj)
280 if (obj instanceof IntBuffer)
282 return compareTo ((IntBuffer) obj) == 0;
289 * Compares two <code>IntBuffer</code> objects.
291 * @exception ClassCastException If obj is not an object derived from
292 * <code>IntBuffer</code>.
294 public int compareTo (IntBuffer other)
296 int num = Math.min(remaining(), other.remaining());
297 int pos_this = position();
298 int pos_other = other.position();
300 for (int count = 0; count < num; count++)
302 int a = get(pos_this++);
303 int b = other.get(pos_other++);
314 return remaining() - other.remaining();
318 * Returns the byte order of this buffer.
320 public abstract ByteOrder order ();
323 * Reads the <code>int</code> at this buffer's current position,
324 * and then increments the position.
326 * @exception BufferUnderflowException If there are no remaining
327 * <code>int</code>s in this buffer.
329 public abstract int get ();
332 * Writes the <code>int</code> at this buffer's current position,
333 * and then increments the position.
335 * @exception BufferOverflowException If there no remaining
336 * <code>int</code>s in this buffer.
337 * @exception ReadOnlyBufferException If this buffer is read-only.
339 public abstract IntBuffer put (int b);
342 * Absolute get method.
344 * @exception IndexOutOfBoundsException If index is negative or not smaller
345 * than the buffer's limit.
347 public abstract int get (int index);
350 * Absolute put method.
352 * @exception IndexOutOfBoundsException If index is negative or not smaller
353 * than the buffer's limit.
354 * @exception ReadOnlyBufferException If this buffer is read-only.
356 public abstract IntBuffer put (int index, int b);
359 * Compacts this buffer.
361 * @exception ReadOnlyBufferException If this buffer is read-only.
363 public abstract IntBuffer compact ();
366 * Tells wether or not this buffer is direct.
368 public abstract boolean isDirect ();
371 * Creates a new <code>IntBuffer</code> whose content is a shared
372 * subsequence of this buffer's content.
374 public abstract IntBuffer slice ();
377 * Creates a new <code>IntBuffer</code> that shares this buffer's
380 public abstract IntBuffer duplicate ();
383 * Creates a new read-only <code>IntBuffer</code> that shares this
386 public abstract IntBuffer asReadOnlyBuffer ();