2004-05-04 Michael Koch <konqueror@gmx.de>
[official-gcc.git] / libjava / java / nio / LongBuffer.java
blob1b420eb5ed47b9da56e610c505c944d2fbf465d2
1 /* LongBuffer.java --
2 Copyright (C) 2002, 2003, 2004 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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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
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 final public static 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 final public static LongBuffer wrap (long[] array)
82 return wrap (array, 0, array.length);
85 /**
86 * This method transfers <code>longs<code> from this buffer into the given
87 * destination array.
89 * @param dst The destination array
90 * @param offset The offset within the array of the first <code>long</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>longs</code> remaining in this buffer.
97 * @exception IndexOutOfBoundsException If the preconditions on the offset
98 * and length parameters do not hold.
100 public LongBuffer get (long[] dst, int offset, int length)
102 for (int i = offset; i < offset + length; i++)
104 dst [i] = get ();
107 return this;
111 * This method transfers <code>longs<code> from this buffer into the given
112 * destination array.
114 * @param dst The byte array to write into.
116 * @exception BufferUnderflowException If there are fewer than dst.length
117 * <code>longs</code> remaining in this buffer.
119 public LongBuffer get (long[] dst)
121 return get (dst, 0, dst.length);
125 * Writes the content of the the <code>LongBUFFER</code> src
126 * into the buffer.
128 * @param src The source data.
130 * @exception BufferOverflowException If there is insufficient space in this
131 * buffer for the remaining <code>longs<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 LongBuffer put (LongBuffer src)
137 if (src == this)
138 throw new IllegalArgumentException ();
140 if (src.remaining () > remaining ())
141 throw new BufferOverflowException ();
143 if (src.remaining () > 0)
145 long[] toPut = new long [src.remaining ()];
146 src.get (toPut);
147 src.put (toPut);
150 return this;
154 * Writes the content of the the <code>long array</code> src
155 * into the buffer.
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>longs<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 LongBuffer put (long[] src, int offset, int length)
171 for (int i = offset; i < offset + length; i++)
172 put (src [i]);
174 return this;
178 * Writes the content of the the <code>long array</code> src
179 * into the buffer.
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>longs<code> in the source array.
185 * @exception ReadOnlyBufferException If this buffer is read-only.
187 public final LongBuffer put (long[] src)
189 return put (src, 0, src.length);
193 * Tells whether ot not this buffer is backed by an accessible
194 * <code>long</code> array.
196 public final boolean hasArray ()
198 return (backing_buffer != null
199 && !isReadOnly ());
203 * Returns the <code>long</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 long[] array ()
211 if (backing_buffer == null)
212 throw new UnsupportedOperationException ();
214 if (isReadOnly ())
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 ();
232 if (isReadOnly ())
233 throw new ReadOnlyBufferException ();
235 return array_offset;
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 LongBuffer)
254 return compareTo (obj) == 0;
257 return false;
261 * Compares two <code>LongBuffer</code> objects.
263 * @exception ClassCastException If obj is not an object derived from
264 * <code>LongBuffer</code>.
266 public int compareTo (Object obj)
268 LongBuffer other = (LongBuffer) obj;
270 int num = Math.min(remaining(), other.remaining());
271 int pos_this = position();
272 int pos_other = other.position();
274 for (int count = 0; count < num; count++)
276 long a = get(pos_this++);
277 long b = other.get(pos_other++);
279 if (a == b)
280 continue;
282 if (a < b)
283 return -1;
285 return 1;
288 return remaining() - other.remaining();
292 * Returns the byte order of this buffer.
294 public abstract ByteOrder order ();
297 * Reads the <code>long</code> at this buffer's current position,
298 * and then increments the position.
300 * @exception BufferUnderflowException If there are no remaining
301 * <code>longs</code> in this buffer.
303 public abstract long get ();
306 * Writes the <code>long</code> at this buffer's current position,
307 * and then increments the position.
309 * @exception BufferOverflowException If there no remaining
310 * <code>longs</code> in this buffer.
311 * @exception ReadOnlyBufferException If this buffer is read-only.
313 public abstract LongBuffer put (long b);
316 * Absolute get method.
318 * @exception IndexOutOfBoundsException If index is negative or not smaller
319 * than the buffer's limit.
321 public abstract long get (int index);
324 * Absolute put method.
326 * @exception IndexOutOfBoundsException If index is negative or not smaller
327 * than the buffer's limit.
328 * @exception ReadOnlyBufferException If this buffer is read-only.
330 public abstract LongBuffer put (int index, long b);
333 * Compacts this buffer.
335 * @exception ReadOnlyBufferException If this buffer is read-only.
337 public abstract LongBuffer compact ();
340 * Tells wether or not this buffer is direct.
342 public abstract boolean isDirect ();
345 * Creates a new <code>LongBuffer</code> whose content is a shared
346 * subsequence of this buffer's content.
348 public abstract LongBuffer slice ();
351 * Creates a new <code>LongBuffer</code> that shares this buffer's
352 * content.
354 public abstract LongBuffer duplicate ();
357 * Creates a new read-only <code>LongBuffer</code> that shares this
358 * buffer's content.
360 public abstract LongBuffer asReadOnlyBuffer ();