2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / nio / LongBuffer.java
bloba39a83d94e8723c1c2672d190b6b4eafd98f9757
1 /* LongBuffer.java --
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)
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 LongBuffer (long[] 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;
63 /**
64 * Allocates a new <code>LongBuffer</code> object with a given capacity.
66 public static LongBuffer allocate (int capacity)
68 return new LongBufferImpl (capacity);
71 /**
72 * Wraps a <code>long</code> array into a <code>LongBuffer</code>
73 * object.
75 * @exception IndexOutOfBoundsException If the preconditions on the offset
76 * and length parameters do not hold
78 final public static LongBuffer wrap (long[] array, int offset, int length)
80 return new LongBufferImpl (array, 0, array.length, offset + length, offset, -1, false);
83 /**
84 * Wraps a <code>long</code> array into a <code>LongBuffer</code>
85 * object.
87 final public static LongBuffer wrap (long[] array)
89 return wrap (array, 0, array.length);
92 /**
93 * This method transfers <code>longs<code> from this buffer into the given
94 * destination array.
96 * @param dst The destination array
97 * @param offset The offset within the array of the first <code>long</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>longs</code> remaining in this buffer.
104 * @exception IndexOutOfBoundsException If the preconditions on the offset
105 * and length parameters do not hold.
107 public LongBuffer get (long[] dst, int offset, int length)
109 for (int i = offset; i < offset + length; i++)
111 dst [i] = get ();
114 return this;
118 * This method transfers <code>longs<code> from this buffer into the given
119 * destination array.
121 * @param dst The byte array to write into.
123 * @exception BufferUnderflowException If there are fewer than dst.length
124 * <code>longs</code> remaining in this buffer.
126 public LongBuffer get (long[] dst)
128 return get (dst, 0, dst.length);
132 * Writes the content of the the <code>LongBUFFER</code> src
133 * into the buffer.
135 * @param src The source data.
137 * @exception BufferOverflowException If there is insufficient space in this
138 * buffer for the remaining <code>longs<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 LongBuffer put (LongBuffer src)
144 if (src == this)
145 throw new IllegalArgumentException ();
147 if (src.remaining () > remaining ())
148 throw new BufferOverflowException ();
150 if (src.remaining () > 0)
152 long[] toPut = new long [src.remaining ()];
153 src.get (toPut);
154 src.put (toPut);
157 return this;
161 * Writes the content of the the <code>long array</code> src
162 * into the buffer.
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>longs<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 LongBuffer put (long[] src, int offset, int length)
178 for (int i = offset; i < offset + length; i++)
179 put (src [i]);
181 return this;
185 * Writes the content of the the <code>long array</code> src
186 * into the buffer.
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>longs<code> in the source array.
192 * @exception ReadOnlyBufferException If this buffer is read-only.
194 public final LongBuffer put (long[] src)
196 return put (src, 0, src.length);
200 * Tells whether ot not this buffer is backed by an accessible
201 * <code>long</code> array.
203 public final boolean hasArray ()
205 return (backing_buffer != null
206 && !isReadOnly ());
210 * Returns the <code>long</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 long[] array ()
218 if (backing_buffer == null)
219 throw new UnsupportedOperationException ();
221 if (isReadOnly ())
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 ();
239 if (isReadOnly ())
240 throw new ReadOnlyBufferException ();
242 return array_offset;
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 LongBuffer)
261 return compareTo (obj) == 0;
264 return false;
268 * Compares two <code>LongBuffer</code> objects.
270 * @exception ClassCastException If obj is not an object derived from
271 * <code>LongBuffer</code>.
273 public int compareTo (Object obj)
275 LongBuffer a = (LongBuffer) obj;
277 if (a.remaining () != remaining ())
278 return 1;
280 if (! hasArray () ||
281 ! a.hasArray ())
283 return 1;
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));
294 if (t != 0)
296 return (int) t;
300 return 0;
304 * Returns the byte order of this buffer.
306 public abstract ByteOrder order ();
309 * Reads the <code>long</code> at this buffer's current position,
310 * and then increments the position.
312 * @exception BufferUnderflowException If there are no remaining
313 * <code>longs</code> in this buffer.
315 public abstract long get ();
318 * Writes the <code>long</code> at this buffer's current position,
319 * and then increments the position.
321 * @exception BufferOverflowException If there no remaining
322 * <code>longs</code> in this buffer.
323 * @exception ReadOnlyBufferException If this buffer is read-only.
325 public abstract LongBuffer put (long b);
328 * Absolute get method.
330 * @exception IndexOutOfBoundsException If index is negative or not smaller
331 * than the buffer's limit.
333 public abstract long 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 LongBuffer put (int index, long b);
345 * Compacts this buffer.
347 * @exception ReadOnlyBufferException If this buffer is read-only.
349 public abstract LongBuffer compact ();
352 * Tells wether or not this buffer is direct.
354 public abstract boolean isDirect ();
357 * Creates a new <code>LongBuffer</code> whose content is a shared
358 * subsequence of this buffer's content.
360 public abstract LongBuffer slice ();
363 * Creates a new <code>LongBuffer</code> that shares this buffer's
364 * content.
366 public abstract LongBuffer duplicate ();
369 * Creates a new read-only <code>LongBuffer</code> that shares this
370 * buffer's content.
372 public abstract LongBuffer asReadOnlyBuffer ();