001 /* LongBuffer.java --
002 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package java.nio;
040
041 // GCJ LOCAL: Change gnu.classpath.Pointer to RawData
042 import gnu.gcj.RawData;
043
044 /**
045 * @since 1.4
046 */
047 public abstract class LongBuffer extends Buffer
048 implements Comparable<LongBuffer>
049 {
050 final int array_offset;
051 final long[] backing_buffer;
052
053 LongBuffer (int capacity, int limit, int position, int mark,
054 RawData address, long[] backing_buffer, int array_offset)
055 {
056 super (capacity, limit, position, mark, address);
057 this.backing_buffer = backing_buffer;
058 this.array_offset = array_offset;
059 }
060
061 /**
062 * Allocates a new <code>LongBuffer</code> object with a given capacity.
063 */
064 public static LongBuffer allocate (int capacity)
065 {
066 return new LongBufferImpl (capacity);
067 }
068
069 /**
070 * Wraps a <code>long</code> array into a <code>LongBuffer</code>
071 * object.
072 *
073 * @exception IndexOutOfBoundsException If the preconditions on the offset
074 * and length parameters do not hold
075 */
076 public static final LongBuffer wrap (long[] array, int offset, int length)
077 {
078 return new LongBufferImpl (array, 0, array.length, offset + length, offset, -1, false);
079 }
080
081 /**
082 * Wraps a <code>long</code> array into a <code>LongBuffer</code>
083 * object.
084 */
085 public static final LongBuffer wrap (long[] array)
086 {
087 return wrap (array, 0, array.length);
088 }
089
090 /**
091 * This method transfers <code>long</code>s from this buffer into the given
092 * destination array. Before the transfer, it checks if there are fewer than
093 * length <code>long</code>s remaining in this buffer.
094 *
095 * @param dst The destination array
096 * @param offset The offset within the array of the first <code>long</code>
097 * to be written; must be non-negative and no larger than dst.length.
098 * @param length The maximum number of bytes to be written to the given array;
099 * must be non-negative and no larger than dst.length - offset.
100 *
101 * @exception BufferUnderflowException If there are fewer than length
102 * <code>long</code>s remaining in this buffer.
103 * @exception IndexOutOfBoundsException If the preconditions on the offset
104 * and length parameters do not hold.
105 */
106 public LongBuffer get (long[] dst, int offset, int length)
107 {
108 checkArraySize(dst.length, offset, length);
109 checkForUnderflow(length);
110
111 for (int i = offset; i < offset + length; i++)
112 {
113 dst [i] = get ();
114 }
115
116 return this;
117 }
118
119 /**
120 * This method transfers <code>long</code>s from this buffer into the given
121 * destination array.
122 *
123 * @param dst The byte array to write into.
124 *
125 * @exception BufferUnderflowException If there are fewer than dst.length
126 * <code>long</code>s remaining in this buffer.
127 */
128 public LongBuffer get (long[] dst)
129 {
130 return get (dst, 0, dst.length);
131 }
132
133 /**
134 * Writes the content of the the <code>LongBUFFER</code> src
135 * into the buffer. Before the transfer, it checks if there is fewer than
136 * <code>src.remaining()</code> space remaining in this buffer.
137 *
138 * @param src The source data.
139 *
140 * @exception BufferOverflowException If there is insufficient space in this
141 * buffer for the remaining <code>long</code>s in the source buffer.
142 * @exception IllegalArgumentException If the source buffer is this buffer.
143 * @exception ReadOnlyBufferException If this buffer is read-only.
144 */
145 public LongBuffer put (LongBuffer src)
146 {
147 if (src == this)
148 throw new IllegalArgumentException ();
149
150 checkForOverflow(src.remaining ());
151
152 if (src.remaining () > 0)
153 {
154 long[] toPut = new long [src.remaining ()];
155 src.get (toPut);
156 put (toPut);
157 }
158
159 return this;
160 }
161
162 /**
163 * Writes the content of the the <code>long array</code> src
164 * into the buffer. Before the transfer, it checks if there is fewer than
165 * length space remaining in this buffer.
166 *
167 * @param src The array to copy into the buffer.
168 * @param offset The offset within the array of the first byte to be read;
169 * must be non-negative and no larger than src.length.
170 * @param length The number of bytes to be read from the given array;
171 * must be non-negative and no larger than src.length - offset.
172 *
173 * @exception BufferOverflowException If there is insufficient space in this
174 * buffer for the remaining <code>long</code>s in the source array.
175 * @exception IndexOutOfBoundsException If the preconditions on the offset
176 * and length parameters do not hold
177 * @exception ReadOnlyBufferException If this buffer is read-only.
178 */
179 public LongBuffer put (long[] src, int offset, int length)
180 {
181 checkArraySize(src.length, offset, length);
182 checkForOverflow(length);
183
184 for (int i = offset; i < offset + length; i++)
185 put (src [i]);
186
187 return this;
188 }
189
190 /**
191 * Writes the content of the the <code>long array</code> src
192 * into the buffer.
193 *
194 * @param src The array to copy into the buffer.
195 *
196 * @exception BufferOverflowException If there is insufficient space in this
197 * buffer for the remaining <code>long</code>s in the source array.
198 * @exception ReadOnlyBufferException If this buffer is read-only.
199 */
200 public final LongBuffer put (long[] src)
201 {
202 return put (src, 0, src.length);
203 }
204
205 /**
206 * Tells whether ot not this buffer is backed by an accessible
207 * <code>long</code> array.
208 */
209 public final boolean hasArray ()
210 {
211 return (backing_buffer != null
212 && !isReadOnly ());
213 }
214
215 /**
216 * Returns the <code>long</code> array that backs this buffer.
217 *
218 * @exception ReadOnlyBufferException If this buffer is read-only.
219 * @exception UnsupportedOperationException If this buffer is not backed
220 * by an accessible array.
221 */
222 public final long[] array ()
223 {
224 if (backing_buffer == null)
225 throw new UnsupportedOperationException ();
226
227 checkIfReadOnly();
228
229 return backing_buffer;
230 }
231
232 /**
233 * Returns the offset within this buffer's backing array of the first element.
234 *
235 * @exception ReadOnlyBufferException If this buffer is read-only.
236 * @exception UnsupportedOperationException If this buffer is not backed
237 * by an accessible array.
238 */
239 public final int arrayOffset ()
240 {
241 if (backing_buffer == null)
242 throw new UnsupportedOperationException ();
243
244 checkIfReadOnly();
245
246 return array_offset;
247 }
248
249 /**
250 * Calculates a hash code for this buffer.
251 *
252 * This is done with <code>long</code> arithmetic,
253 * where ** represents exponentiation, by this formula:<br>
254 * <code>s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... +
255 * (s[limit()-1]+30)*31**(limit()-1)</code>.
256 * Where s is the buffer data. Note that the hashcode is dependent
257 * on buffer content, and therefore is not useful if the buffer
258 * content may change.
259 *
260 * @return the hash code (casted to int)
261 */
262 public int hashCode ()
263 {
264 long hashCode = get(position()) + 31;
265 long multiplier = 1;
266 for (int i = position() + 1; i < limit(); ++i)
267 {
268 multiplier *= 31;
269 hashCode += (get(i) + 30)*multiplier;
270 }
271 return ((int)hashCode);
272 }
273
274 /**
275 * Checks if this buffer is equal to obj.
276 */
277 public boolean equals (Object obj)
278 {
279 if (obj instanceof LongBuffer)
280 {
281 return compareTo ((LongBuffer) obj) == 0;
282 }
283
284 return false;
285 }
286
287 /**
288 * Compares two <code>LongBuffer</code> objects.
289 *
290 * @exception ClassCastException If obj is not an object derived from
291 * <code>LongBuffer</code>.
292 */
293 public int compareTo (LongBuffer other)
294 {
295 int num = Math.min(remaining(), other.remaining());
296 int pos_this = position();
297 int pos_other = other.position();
298
299 for (int count = 0; count < num; count++)
300 {
301 long a = get(pos_this++);
302 long b = other.get(pos_other++);
303
304 if (a == b)
305 continue;
306
307 if (a < b)
308 return -1;
309
310 return 1;
311 }
312
313 return remaining() - other.remaining();
314 }
315
316 /**
317 * Returns the byte order of this buffer.
318 */
319 public abstract ByteOrder order ();
320
321 /**
322 * Reads the <code>long</code> at this buffer's current position,
323 * and then increments the position.
324 *
325 * @exception BufferUnderflowException If there are no remaining
326 * <code>long</code>s in this buffer.
327 */
328 public abstract long get ();
329
330 /**
331 * Writes the <code>long</code> at this buffer's current position,
332 * and then increments the position.
333 *
334 * @exception BufferOverflowException If there no remaining
335 * <code>long</code>s in this buffer.
336 * @exception ReadOnlyBufferException If this buffer is read-only.
337 */
338 public abstract LongBuffer put (long b);
339
340 /**
341 * Absolute get method.
342 *
343 * @exception IndexOutOfBoundsException If index is negative or not smaller
344 * than the buffer's limit.
345 */
346 public abstract long get (int index);
347
348 /**
349 * Absolute put method.
350 *
351 * @exception IndexOutOfBoundsException If index is negative or not smaller
352 * than the buffer's limit.
353 * @exception ReadOnlyBufferException If this buffer is read-only.
354 */
355 public abstract LongBuffer put (int index, long b);
356
357 /**
358 * Compacts this buffer.
359 *
360 * @exception ReadOnlyBufferException If this buffer is read-only.
361 */
362 public abstract LongBuffer compact ();
363
364 /**
365 * Tells wether or not this buffer is direct.
366 */
367 public abstract boolean isDirect ();
368
369 /**
370 * Creates a new <code>LongBuffer</code> whose content is a shared
371 * subsequence of this buffer's content.
372 */
373 public abstract LongBuffer slice ();
374
375 /**
376 * Creates a new <code>LongBuffer</code> that shares this buffer's
377 * content.
378 */
379 public abstract LongBuffer duplicate ();
380
381 /**
382 * Creates a new read-only <code>LongBuffer</code> that shares this
383 * buffer's content.
384 */
385 public abstract LongBuffer asReadOnlyBuffer ();
386 }