Merged gcj-eclipse branch to trunk.
[official-gcc.git] / libjava / classpath / java / nio / LongBuffer.java
blob9c3bfa627416977fc0212d1ae5d5b931ae05a1fc
1 /* LongBuffer.java --
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)
9 any later version.
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
19 02110-1301 USA.
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.
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. */
39 package java.nio;
41 /**
42 * @since 1.4
44 public abstract class LongBuffer extends Buffer
45 implements Comparable<LongBuffer>
47 int array_offset;
48 long[] backing_buffer;
50 LongBuffer (int capacity, int limit, int position, int mark)
52 super (capacity, limit, position, mark);
53 array_offset = 0;
56 /**
57 * Allocates a new <code>LongBuffer</code> object with a given capacity.
59 public static LongBuffer allocate (int capacity)
61 return new LongBufferImpl (capacity);
64 /**
65 * Wraps a <code>long</code> array into a <code>LongBuffer</code>
66 * object.
68 * @exception IndexOutOfBoundsException If the preconditions on the offset
69 * and length parameters do not hold
71 public static final LongBuffer wrap (long[] array, int offset, int length)
73 return new LongBufferImpl (array, 0, array.length, offset + length, offset, -1, false);
76 /**
77 * Wraps a <code>long</code> array into a <code>LongBuffer</code>
78 * object.
80 public static final LongBuffer wrap (long[] array)
82 return wrap (array, 0, array.length);
85 /**
86 * This method transfers <code>long</code>s from this buffer into the given
87 * destination array. Before the transfer, it checks if there are fewer than
88 * length <code>long</code>s remaining in this buffer.
90 * @param dst The destination array
91 * @param offset The offset within the array of the first <code>long</code>
92 * to be written; must be non-negative and no larger than dst.length.
93 * @param length The maximum number of bytes to be written to the given array;
94 * must be non-negative and no larger than dst.length - offset.
96 * @exception BufferUnderflowException If there are fewer than length
97 * <code>long</code>s remaining in this buffer.
98 * @exception IndexOutOfBoundsException If the preconditions on the offset
99 * and length parameters do not hold.
101 public LongBuffer get (long[] dst, int offset, int length)
103 checkArraySize(dst.length, offset, length);
104 checkForUnderflow(length);
106 for (int i = offset; i < offset + length; i++)
108 dst [i] = get ();
111 return this;
115 * This method transfers <code>long</code>s from this buffer into the given
116 * destination array.
118 * @param dst The byte array to write into.
120 * @exception BufferUnderflowException If there are fewer than dst.length
121 * <code>long</code>s remaining in this buffer.
123 public LongBuffer get (long[] dst)
125 return get (dst, 0, dst.length);
129 * Writes the content of the the <code>LongBUFFER</code> src
130 * into the buffer. Before the transfer, it checks if there is fewer than
131 * <code>src.remaining()</code> space remaining in this buffer.
133 * @param src The source data.
135 * @exception BufferOverflowException If there is insufficient space in this
136 * buffer for the remaining <code>long</code>s in the source buffer.
137 * @exception IllegalArgumentException If the source buffer is this buffer.
138 * @exception ReadOnlyBufferException If this buffer is read-only.
140 public LongBuffer put (LongBuffer src)
142 if (src == this)
143 throw new IllegalArgumentException ();
145 checkForOverflow(src.remaining ());
147 if (src.remaining () > 0)
149 long[] toPut = new long [src.remaining ()];
150 src.get (toPut);
151 put (toPut);
154 return this;
158 * Writes the content of the the <code>long array</code> src
159 * into the buffer. Before the transfer, it checks if there is fewer than
160 * length space remaining in this buffer.
162 * @param src The array to copy into the buffer.
163 * @param offset The offset within the array of the first byte to be read;
164 * must be non-negative and no larger than src.length.
165 * @param length The number of bytes to be read from the given array;
166 * must be non-negative and no larger than src.length - offset.
168 * @exception BufferOverflowException If there is insufficient space in this
169 * buffer for the remaining <code>long</code>s in the source array.
170 * @exception IndexOutOfBoundsException If the preconditions on the offset
171 * and length parameters do not hold
172 * @exception ReadOnlyBufferException If this buffer is read-only.
174 public LongBuffer put (long[] src, int offset, int length)
176 checkArraySize(src.length, offset, length);
177 checkForOverflow(length);
179 for (int i = offset; i < offset + length; i++)
180 put (src [i]);
182 return this;
186 * Writes the content of the the <code>long array</code> src
187 * into the buffer.
189 * @param src The array to copy into the buffer.
191 * @exception BufferOverflowException If there is insufficient space in this
192 * buffer for the remaining <code>long</code>s in the source array.
193 * @exception ReadOnlyBufferException If this buffer is read-only.
195 public final LongBuffer put (long[] src)
197 return put (src, 0, src.length);
201 * Tells whether ot not this buffer is backed by an accessible
202 * <code>long</code> array.
204 public final boolean hasArray ()
206 return (backing_buffer != null
207 && !isReadOnly ());
211 * Returns the <code>long</code> array that backs this buffer.
213 * @exception ReadOnlyBufferException If this buffer is read-only.
214 * @exception UnsupportedOperationException If this buffer is not backed
215 * by an accessible array.
217 public final long[] array ()
219 if (backing_buffer == null)
220 throw new UnsupportedOperationException ();
222 checkIfReadOnly();
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 ();
239 checkIfReadOnly();
241 return array_offset;
245 * Calculates a hash code for this buffer.
247 * This is done with <code>long</code> arithmetic,
248 * where ** represents exponentiation, by this formula:<br>
249 * <code>s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... +
250 * (s[limit()-1]+30)*31**(limit()-1)</code>.
251 * Where s is the buffer data. Note that the hashcode is dependent
252 * on buffer content, and therefore is not useful if the buffer
253 * content may change.
255 * @return the hash code (casted to int)
257 public int hashCode ()
259 long hashCode = get(position()) + 31;
260 long multiplier = 1;
261 for (int i = position() + 1; i < limit(); ++i)
263 multiplier *= 31;
264 hashCode += (get(i) + 30)*multiplier;
266 return ((int)hashCode);
270 * Checks if this buffer is equal to obj.
272 public boolean equals (Object obj)
274 if (obj instanceof LongBuffer)
276 return compareTo ((LongBuffer) obj) == 0;
279 return false;
283 * Compares two <code>LongBuffer</code> objects.
285 * @exception ClassCastException If obj is not an object derived from
286 * <code>LongBuffer</code>.
288 public int compareTo (LongBuffer other)
290 int num = Math.min(remaining(), other.remaining());
291 int pos_this = position();
292 int pos_other = other.position();
294 for (int count = 0; count < num; count++)
296 long a = get(pos_this++);
297 long b = other.get(pos_other++);
299 if (a == b)
300 continue;
302 if (a < b)
303 return -1;
305 return 1;
308 return remaining() - other.remaining();
312 * Returns the byte order of this buffer.
314 public abstract ByteOrder order ();
317 * Reads the <code>long</code> at this buffer's current position,
318 * and then increments the position.
320 * @exception BufferUnderflowException If there are no remaining
321 * <code>long</code>s in this buffer.
323 public abstract long get ();
326 * Writes the <code>long</code> at this buffer's current position,
327 * and then increments the position.
329 * @exception BufferOverflowException If there no remaining
330 * <code>long</code>s in this buffer.
331 * @exception ReadOnlyBufferException If this buffer is read-only.
333 public abstract LongBuffer put (long b);
336 * Absolute get method.
338 * @exception IndexOutOfBoundsException If index is negative or not smaller
339 * than the buffer's limit.
341 public abstract long get (int index);
344 * Absolute put method.
346 * @exception IndexOutOfBoundsException If index is negative or not smaller
347 * than the buffer's limit.
348 * @exception ReadOnlyBufferException If this buffer is read-only.
350 public abstract LongBuffer put (int index, long b);
353 * Compacts this buffer.
355 * @exception ReadOnlyBufferException If this buffer is read-only.
357 public abstract LongBuffer compact ();
360 * Tells wether or not this buffer is direct.
362 public abstract boolean isDirect ();
365 * Creates a new <code>LongBuffer</code> whose content is a shared
366 * subsequence of this buffer's content.
368 public abstract LongBuffer slice ();
371 * Creates a new <code>LongBuffer</code> that shares this buffer's
372 * content.
374 public abstract LongBuffer duplicate ();
377 * Creates a new read-only <code>LongBuffer</code> that shares this
378 * buffer's content.
380 public abstract LongBuffer asReadOnlyBuffer ();