001: /*
002: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License version
007: * 2 only, as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful, but
010: * WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * General Public License version 2 for more details (a copy is
013: * included at /legal/license.txt).
014: *
015: * You should have received a copy of the GNU General Public License
016: * version 2 along with this work; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
018: * 02110-1301 USA
019: *
020: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
021: * Clara, CA 95054 or visit www.sun.com if you need additional
022: * information or have any questions.
023: */
024:
025: package java.nio;
026:
027: /**
028: * An int buffer.
029: *
030: * <p> This class is provided as part of the JSR 239 NIO Buffer
031: * building block. It is a subset of the
032: * <code>java.nio.IntBuffer</code> class in Java(TM) Standard Edition
033: * version 1.4.2. Differences are noted in <b><i>bold italic</i></b>.
034: * The class documentation may make reference to classes that are not
035: * present in the building block.
036: *
037: * <p><b><i> I/O channels, marking and resetting, and read-only buffers
038: * are not supported. Allocation of non-direct int buffers,
039: * compaction, and duplication are not supported.
040: * The following methods are omitted:
041: *
042: * <ul>
043: * <li><code>IntBuffer allocate(int capacity)</code></li>
044: * <li><code>IntBuffer compact()</code></li>
045: * <li><code>IntBuffer duplicate()</code></li>
046: * <li><code>Buffer mark()</code></li>
047: * <li><code>Buffer reset()</code></li>
048: * <li><code>boolean isReadOnly()</code></li>
049: * <li><code>IntBuffer asReadOnlyBuffer()</code></li>
050: * </ul>
051: * </i></b>
052: *
053: * <p> This class defines four categories of operations upon
054: * int buffers:
055: *
056: * <ul>
057: *
058: * <li><p> Absolute and relative {@link #get() </code><i>get</i><code>} and
059: * {@link #put(int) </code><i>put</i><code>} methods that read and write
060: * single ints; </p></li>
061: *
062: * <li><p> Relative {@link #get(int[]) </code><i>bulk get</i><code>}
063: * methods that transfer contiguous sequences of ints from this buffer
064: * into an array;</li>
065: *
066: * <li><p> Relative {@link #put(int[]) </code><i>bulk put</i><code>}
067: * methods that transfer contiguous sequences of ints from an
068: * int array or some other int
069: * buffer into this buffer;  and </p></li>
070: *
071: * <li><p> Methods for compacting, duplicating, and {@link #slice
072: * slicing} an int buffer. <b><i>JSR 239 does
073: * not support compacting and duplicating buffers.</i></b> </p></li>
074: *
075: * </ul>
076: *
077: * <p> Int buffers can be created either by <i>allocation</i>, which
078: * allocates space for the buffer's content, by {@link #wrap(int[])
079: * </code><i>wrapping</i><code>} an existing int array into a buffer,
080: * or by creating a <a href="ByteBuffer.html#view"><i>view</i></a> of
081: * an existing byte buffer. <b><i>JSR 239 supports allocation of
082: * <code>ByteBuffer</code>s only.</i></b>
083: *
084: * <p> Like a byte buffer, an int buffer is either <a
085: * href="ByteBuffer.html#direct"><i>direct</i> or <i>non-direct</i></a>. A
086: * int buffer created via the <tt>wrap</tt> methods of this class will
087: * be non-direct. An int buffer created as a view of a byte buffer will
088: * be direct if, and only if, the byte buffer itself is direct. Whether or not
089: * an int buffer is direct may be determined by invoking the {@link
090: * #isDirect isDirect} method. </p>
091: *
092: * <p> Methods in this class that do not otherwise have a value to return are
093: * specified to return the buffer upon which they are invoked. This allows
094: * method invocations to be chained.
095: */
096: public abstract class IntBuffer extends Buffer implements Comparable {
097:
098: ByteBufferImpl parent;
099:
100: int[] array;
101: int arrayOffset;
102:
103: boolean isDirect;
104:
105: boolean disposed = false;
106:
107: /**
108: * Constructs a new <code>IntBuffer</code>.
109: */
110: IntBuffer() {
111: }
112:
113: /**
114: * Wraps an int array into a buffer.
115: *
116: * <p> The new buffer will be backed by the given int array;
117: * that is, modifications to the buffer will cause the array to be modified
118: * and vice versa. The new buffer's capacity will be
119: * <tt>array.length</tt>, its position will be <tt>offset</tt>, its limit
120: * will be <tt>offset + length</tt>, and its mark will be undefined. Its
121: * {@link #array </code>backing array<code>} will be the given array, and
122: * its {@link #arrayOffset </code>array offset<code>} will be zero. </p>
123: *
124: * @param array
125: * The array that will back the new buffer
126: *
127: * @param offset
128: * The offset of the subarray to be used; must be non-negative and
129: * no larger than <tt>array.length</tt>. The new buffer's position
130: * will be set to this value.
131: *
132: * @param length
133: * The length of the subarray to be used;
134: * must be non-negative and no larger than
135: * <tt>array.length - offset</tt>.
136: * The new buffer's limit will be set to <tt>offset + length</tt>.
137: *
138: * @return The new int buffer
139: *
140: * @throws IndexOutOfBoundsException
141: * If the preconditions on the <tt>offset</tt> and <tt>length</tt>
142: * parameters do not hold
143: */
144: public static IntBuffer wrap(int[] array, int offset, int length) {
145: if (offset < 0 || offset > array.length || length < 0
146: || length > array.length - offset) {
147: throw new IndexOutOfBoundsException();
148: }
149:
150: IntBufferImpl ibi = new IntBufferImpl(null, array.length,
151: array, 0, false);
152: ibi.position(offset);
153: ibi.limit(offset + length);
154: return ibi;
155: }
156:
157: /**
158: * Wraps an int array into a buffer.
159: *
160: * <p> The new buffer will be backed by the given int array;
161: * that is, modifications to the buffer will cause the array to be modified
162: * and vice versa. The new buffer's capacity and limit will be
163: * <tt>array.length</tt>, its position will be zero, and its mark will be
164: * undefined. Its {@link #array </code>backing array<code>} will be the
165: * given array, and its {@link #arrayOffset </code>array offset<code>} will
166: * be zero. </p>
167: *
168: * @param array
169: * The array that will back this buffer
170: *
171: * @return The new int buffer
172: */
173: public static IntBuffer wrap(int[] array) {
174: return wrap(array, 0, array.length);
175: }
176:
177: /**
178: * Creates a new int buffer whose content is a shared
179: * subsequence of this buffer's content.
180: *
181: * <p> The content of the new buffer will start at this buffer's
182: * current position. Changes to this buffer's content will be
183: * visible in the new buffer, and vice versa; the two buffers'
184: * position, limit, and mark values will be independent. <b><i>JSR
185: * 239 does not support the mark.</i></b>
186: *
187: * <p> The new buffer's position will be zero, its capacity and
188: * its limit will be the number of ints remaining in this
189: * buffer, and its mark will be undefined. The new buffer will be
190: * direct if, and only if, this buffer is direct, and it will be
191: * read-only if, and only if, this buffer is read-only. <b><i>JSR
192: * 239 does not support the mark or read-only buffers.</i></b>
193: * </p>
194: *
195: * @return The new int buffer.
196: */
197: public abstract IntBuffer slice();
198:
199: /**
200: * Relative <i>get</i> method. Reads the int at this
201: * buffer's current position, and then increments the
202: * position. </p>
203: *
204: * @return The int at the buffer's current position.
205: *
206: * @throws BufferUnderflowException If the buffer's current
207: * position is not smaller than its limit.
208: */
209: public abstract int get();
210:
211: /**
212: * Relative <i>put</i> method <i>(optional
213: * operation)</i>.
214: *
215: * <p> Writes the given int into this buffer at the current
216: * position, and then increments the position. </p>
217: *
218: * @param i The int to be written.
219: *
220: * @return This buffer.
221: *
222: * @throws BufferOverflowException If this buffer's current
223: * position is not smaller than its limit.
224: *
225: * @throws ReadOnlyBufferException If this buffer is
226: * read-only. <b><i>JSR 239 does not support read-only buffer or
227: * the <code>ReadOnlyBufferException</code> class.</i></b>
228: */
229: public abstract IntBuffer put(int i);
230:
231: /**
232: * Absolute <i>get</i> method. Reads the int at the given
233: * index. </p>
234: *
235: * @param index The index from which the int will be read.
236: *
237: * @return The int at the given index.
238: *
239: * @throws IndexOutOfBoundsException If <tt>index</tt> is negative
240: * or not smaller than the buffer's limit.
241: */
242: public abstract int get(int index);
243:
244: /**
245: * Absolute <i>put</i> method <i>(optional operation)</i>.
246: *
247: * <p> Writes the given int into this buffer at the given
248: * index. </p>
249: *
250: * @param index The index at which the int will be written.
251: *
252: * @param i The int value to be written.
253: *
254: * @return This buffer.
255: *
256: * @throws IndexOutOfBoundsException If <tt>index</tt> is negative
257: * or not smaller than the buffer's limit.
258: *
259: * @throws ReadOnlyBufferException If this buffer is
260: * read-only. <b><i>JSR 239 does not support read-only buffer or
261: * the <code>ReadOnlyBufferException</code> class.</i></b>
262: */
263: public abstract IntBuffer put(int index, int i);
264:
265: /**
266: * Relative bulk <i>get</i> method.
267: *
268: * <p> This method transfers ints from this buffer into the
269: * given destination array. If there are fewer ints
270: * remaining in the buffer than are required to satisfy the
271: * request, that is, if
272: * <tt>length</tt> <tt>></tt> <tt>remaining()</tt>,
273: * then no ints are transferred and a {@link
274: * BufferUnderflowException} is thrown.
275: *
276: * <p> Otherwise, this method copies <tt>length</tt> ints
277: * from this buffer into the given array, starting at the current
278: * position of this buffer and at the given offset in the array.
279: * The position of this buffer is then incremented by
280: * <tt>length</tt>.
281: *
282: * <p> In other words, an invocation of this method of the form
283: * <tt>src.get(dst, off, len)</tt> has exactly the same
284: * effect as the loop
285: *
286: * <pre>
287: * for (int i = off; i < off + len; i++)
288: * dst[i] = src.get(); </pre>
289: *
290: * except that it first checks that there are sufficient
291: * ints in this buffer and it is potentially much more
292: * efficient. </p>
293: *
294: * @param dst The array into which ints are to be written.
295: *
296: * @param offset The offset within the array of the first
297: * int to be written; must be non-negative and no larger
298: * than <tt>dst.length</tt>.
299: *
300: * @param length The maximum number of ints to be written
301: * to the given array; must be non-negative and no larger than
302: * <tt>dst.length - offset</tt>.
303: *
304: * @return This buffer.
305: *
306: * @throws BufferUnderflowException If there are fewer than
307: * <tt>dst.length</tt> ints remaining in this buffer.
308: *
309: * @throws IndexOutOfBoundsException If the preconditions on the
310: * <tt>offset</tt> and <tt>length</tt> parameters do not hold.
311: */
312: public IntBuffer get(int[] dst, int offset, int length) {
313: if (offset < 0 || offset > dst.length || length < 0
314: || length > dst.length - offset) {
315: throw new IndexOutOfBoundsException();
316: }
317: if (limit - position < length) {
318: throw new BufferUnderflowException();
319: }
320:
321: int bytePtr = arrayOffset + (position << 2);
322: if (isDirect) {
323: ByteBufferImpl._getInts(bytePtr, dst, offset, length);
324: } else if (array != null) {
325: System.arraycopy(array, arrayOffset + position, dst,
326: offset, length);
327: } else {
328: for (int i = 0; i < length; i++) {
329: dst[offset++] = parent.getInt(bytePtr);
330: bytePtr += 4;
331: }
332: }
333: position += length;
334: return this ;
335: }
336:
337: /**
338: * Relative bulk <i>get</i> method.
339: *
340: * <p> This method transfers ints from this buffer into the
341: * given destination array. An invocation of this method of the
342: * form <tt>src.get(a)</tt> behaves in exactly the same way as the
343: * invocation
344: *
345: * <pre>
346: * src.get(a, 0, a.length) </pre>
347: *
348: * @return This buffer.
349: *
350: * @throws BufferUnderflowException If there are fewer than
351: * <tt>length</tt> ints remaining in this buffer.
352: */
353: public IntBuffer get(int[] dst) {
354: return get(dst, 0, dst.length);
355: }
356:
357: /**
358: * Relative bulk <i>put</i> method <i>(optional
359: * operation)</i>.
360: *
361: * <p> This method transfers the ints remaining in the
362: * given source buffer into this buffer. If there are more
363: * ints remaining in the source buffer than in this buffer,
364: * that is, if
365: * <tt>src.remaining()</tt> <tt>></tt> <tt>remaining()</tt>,
366: * then no ints are transferred and a {@link
367: * BufferOverflowException} is thrown.
368: *
369: * <p> Otherwise, this method copies
370: * <i>n</i> = <tt>src.remaining()</tt> ints from
371: * the given buffer into this buffer, starting at each buffer's
372: * current position. The positions of both buffers are then
373: * incremented by <i>n</i>.
374: *
375: * <p> In other words, an invocation of this method of the form
376: * <tt>dst.put(src)</tt> has exactly the same effect as the loop
377: *
378: * <pre>
379: * while (src.hasRemaining())
380: * dst.put(src.get()); </pre>
381: *
382: * except that it first checks that there is sufficient space in
383: * this buffer and it is potentially much more efficient. </p>
384: *
385: * @param src The source buffer from which ints are to be
386: * read; must not be this buffer.
387: *
388: * @return This buffer.
389: *
390: * @throws BufferOverflowException If there is insufficient space
391: * in this buffer for the remaining ints in the source
392: * buffer.
393: *
394: * @throws IllegalArgumentException If the source buffer is this buffer.
395: *
396: * @throws ReadOnlyBufferException If this buffer is
397: * read-only. <b><i>JSR 239 does not support read-only buffer or
398: * the <code>ReadOnlyBufferException</code> class.</i></b>
399: */
400: public IntBuffer put(IntBuffer src) {
401: if (src == this ) {
402: throw new IllegalArgumentException();
403: }
404:
405: IntBufferImpl srci = (IntBufferImpl) src;
406:
407: int length = srci.limit - srci.position;
408: if (length > this .limit - this .position) {
409: throw new BufferOverflowException();
410: }
411: if (isDirect && srci.isDirect) {
412: ByteBufferImpl._copyBytes(srci.arrayOffset
413: + (srci.position << 2), this .arrayOffset
414: + (this .position << 2), (length << 2));
415: } else if (isDirect && !srci.isDirect) {
416: if (srci.array != null) {
417: ByteBufferImpl._putInts(this .arrayOffset
418: + (this .position << 2), srci.array,
419: srci.arrayOffset + srci.position, length);
420: } else {
421: byte[] srcArray = srci.parent.array;
422: int srciArrayOffset = srci.parent.arrayOffset
423: + srci.arrayOffset + (srci.position << 2);
424:
425: ByteBufferImpl._putBytes(this .arrayOffset
426: + (this .position << 2), srcArray,
427: srciArrayOffset, 4 * length);
428: }
429: } else if (!isDirect && srci.isDirect) {
430: if (array != null) {
431: ByteBufferImpl._getInts(srci.arrayOffset
432: + (srci.position << 2), this .array,
433: this .arrayOffset + this .position, length);
434: } else {
435: byte[] dstArray = parent.array;
436: int dstArrayOffset = parent.arrayOffset + arrayOffset
437: + (position << 2);
438:
439: ByteBufferImpl._getBytes(srci.arrayOffset
440: + (srci.position << 2), dstArray,
441: dstArrayOffset, 4 * length);
442: }
443: } else if (!isDirect && !srci.isDirect) {
444: if (array != null && srci.array != null) {
445: System.arraycopy(srci.array, srci.arrayOffset
446: + srci.position, this .array, this .arrayOffset
447: + this .position, length);
448: } else {
449: for (int i = 0; i < length; i++) {
450: put(i, srci.get(i));
451: }
452: }
453: }
454:
455: srci.position += length;
456: this .position += length;
457: return this ;
458: }
459:
460: /**
461: * Relative bulk <i>put</i> method <i>(optional
462: * operation)</i>.
463: *
464: * <p> This method transfers ints into this buffer from the
465: * given source array. If there are more ints to be copied
466: * from the array than remain in this buffer, that is, if
467: * <tt>length</tt> <tt>></tt> <tt>remaining()</tt>,
468: * then no ints are transferred and a {@link
469: * BufferOverflowException} is thrown.
470: *
471: * <p> Otherwise, this method copies <tt>length</tt> ints
472: * from the given array into this buffer, starting at the given
473: * offset in the array and at the current position of this buffer.
474: * The position of this buffer is then incremented by
475: * <tt>length</tt>.
476: *
477: * <p> In other words, an invocation of this method of the form
478: * <tt>dst.put(src, off, len)</tt> has exactly the same
479: * effect as the loop
480: *
481: * <pre>
482: * for (int i = off; i < off + len; i++)
483: * dst.put(a[i]); </pre>
484: *
485: * except that it first checks that there is sufficient space in
486: * this buffer and it is potentially much more efficient. </p>
487: *
488: * @param src The array from which ints are to be read.
489: *
490: * @param offset The offset within the array of the first
491: * int to be read; must be non-negative and no larger than
492: * <tt>array.length</tt>.
493: *
494: * @param length The number of ints to be read from the
495: * given array; must be non-negative and no larger than
496: * <tt>array.length - offset</tt>.
497: *
498: * @return This buffer.
499: *
500: * @throws BufferOverflowException If there is insufficient space
501: * in this buffer.
502: *
503: * @throws IndexOutOfBoundsException If the preconditions on the
504: * <tt>offset</tt> and <tt>length</tt> parameters do not hold.
505: *
506: * @throws ReadOnlyBufferException If this buffer is
507: * read-only. <b><i>JSR 239 does not support read-only buffer or
508: * the <code>ReadOnlyBufferException</code> class.</i></b>
509: */
510: public IntBuffer put(int[] src, int offset, int length) {
511: if (offset < 0 || offset > src.length || length < 0
512: || length > src.length - offset) {
513: throw new IndexOutOfBoundsException();
514: }
515: if (length > limit - position) {
516: throw new BufferOverflowException();
517: }
518:
519: int bytePtr = arrayOffset + (position << 2);
520: if (isDirect) {
521: ByteBufferImpl._putInts(bytePtr, src, offset, length);
522: } else if (array != null) {
523: System.arraycopy(src, offset, array,
524: arrayOffset + position, length);
525: } else {
526: for (int i = 0; i < length; i++) {
527: parent.putInt(bytePtr, src[offset++]);
528: bytePtr += 4;
529: }
530: }
531: position += length;
532: return this ;
533: }
534:
535: /**
536: * Relative bulk <i>put</i> method <i>(optional operation)</i>.
537: *
538: * <p> This method transfers the entire content of the given
539: * source int array into this buffer. An invocation of
540: * this method of the form <tt>dst.put(a)</tt> behaves in exactly
541: * the same way as the invocation
542: *
543: * <pre>
544: * dst.put(a, 0, a.length) </pre>
545: *
546: * @return This buffer.
547: *
548: * @throws BufferOverflowException If there is insufficient space
549: * in this buffer.
550: *
551: * @throws ReadOnlyBufferException If this buffer is
552: * read-only. <b><i>JSR 239 does not support read-only buffer or
553: * the <code>ReadOnlyBufferException</code> class.</i></b>
554: */
555: public final IntBuffer put(int[] src) {
556: return put(src, 0, src.length);
557: }
558:
559: /**
560: * Tells whether or not this buffer is backed by an accessible
561: * int array.
562: *
563: * <p> If this method returns <tt>true</tt> then the {@link
564: * #array() array} and {@link #arrayOffset() arrayOffset} methods
565: * may safely be invoked. </p>
566: *
567: * @return <tt>true</tt> if, and only if, this buffer is backed by
568: * an array and is not read-only. <b><i>JSR 239 does not support
569: * read-only buffers.</i></b>
570: */
571: public final boolean hasArray() {
572: return array != null;
573: }
574:
575: /**
576: * Returns the int array that backs this
577: * buffer <i>(optional operation)</i>.
578: *
579: * <p> Modifications to this buffer's content will cause the returned
580: * array's content to be modified, and vice versa.
581: *
582: * <p> Invoke the {@link #hasArray hasArray} method before
583: * invoking this method in order to ensure that this buffer has an
584: * accessible backing array. </p>
585: *
586: * @return The array that backs this buffer.
587: *
588: * @throws ReadOnlyBufferException If this buffer is
589: * read-only. <b><i>JSR 239 does not support read-only buffer or
590: * the <code>ReadOnlyBufferException</code> class.</i></b>
591: *
592: * @throws UnsupportedOperationException If this buffer is not
593: * backed by an accessible array.
594: */
595: public final int[] array() {
596: if (array == null) {
597: throw new UnsupportedOperationException();
598: }
599: return array;
600: }
601:
602: /**
603: * Returns the offset within this buffer's backing array of the
604: * first element of the buffer <i>(optional
605: * operation)</i>.
606: *
607: * <p> If this buffer is backed by an array then buffer position
608: * <i>p</i> corresponds to array index
609: * <i>p</i> + <tt>arrayOffset()</tt>.
610: *
611: * <p> Invoke the {@link #hasArray hasArray} method before
612: * invoking this method in order to ensure that this buffer has an
613: * accessible backing array. </p>
614: *
615: * @return The offset within this buffer's array of the first
616: * element of the buffer.
617: *
618: * @throws ReadOnlyBufferException If this buffer is
619: * read-only. <b><i>JSR 239 does not support read-only buffer or
620: * the <code>ReadOnlyBufferException</code> class.</i></b>
621: *
622: * @throws UnsupportedOperationException If this buffer is not
623: * backed by an accessible array.
624: */
625: public final int arrayOffset() {
626: if (array == null) {
627: throw new UnsupportedOperationException();
628: }
629: return arrayOffset;
630: }
631:
632: /**
633: * Tells whether or not this int buffer is direct. </p>
634: *
635: * @return <tt>true</tt> if, and only if, this buffer is direct.
636: */
637: public abstract boolean isDirect();
638:
639: /**
640: * Returns a string summarizing the state of this buffer.
641: *
642: * @return A summary string
643: */
644: public String toString() {
645: return "java.nio.IntBuffer[" + "pos=" + position() + "lim="
646: + limit() + "cap=" + capacity() + "]";
647: }
648:
649: /**
650: * Returns the current hash code of this buffer.
651: *
652: * <p> The hash code of an int buffer depends only upon its remaining
653: * elements; that is, upon the elements from <tt>position()</tt> up to, and
654: * including, the element at <tt>limit()</tt> - <tt>1</tt>.
655: *
656: * <p> Because buffer hash codes are content-dependent, it is inadvisable
657: * to use buffers as keys in hash maps or similar data structures unless it
658: * is known that their contents will not change. </p>
659: *
660: * @return The current hash code of this buffer
661: */
662: public int hashCode() {
663: int h = 1;
664: int p = position();
665: for (int i = limit() - 1; i >= p; i--)
666: h = 31 * h + (int) get(i);
667: return h;
668: }
669:
670: /**
671: * Tells whether or not this buffer is equal to another object.
672: *
673: * <p> Two int buffers are equal if, and only if,
674: *
675: * <p><ol>
676: *
677: * <li><p> They have the same element type, </p></li>
678: *
679: * <li><p> They have the same number of remaining elements, and
680: * </p></li>
681: *
682: * <li><p> The two sequences of remaining elements, considered
683: * independently of their starting positions, are pointwise equal.
684: * </p></li>
685: *
686: * </ol>
687: *
688: * <p> An int buffer is not equal to any other type of object. </p>
689: *
690: * @param ob The object to which this buffer is to be compared.
691: *
692: * @return <tt>true</tt> if, and only if, this buffer is equal to the
693: * given object.
694: */
695: public boolean equals(Object ob) {
696: if (!(ob instanceof IntBuffer))
697: return false;
698: IntBuffer that = (IntBuffer) ob;
699: if (this .remaining() != that.remaining())
700: return false;
701: int p = this .position();
702: for (int i = this .limit() - 1, j = that.limit() - 1; i >= p; i--, j--) {
703: int v1 = this .get(i);
704: int v2 = that.get(j);
705: if (v1 != v2) {
706: if ((v1 != v1) && (v2 != v2)) // For float and double
707: continue;
708: return false;
709: }
710: }
711: return true;
712: }
713:
714: /**
715: * Compares this buffer to another.
716: *
717: * <p> Two int buffers are compared by comparing their sequences of
718: * remaining elements lexicographically, without regard to the starting
719: * position of each sequence within its corresponding buffer.
720: *
721: * <p> An int buffer is not comparable to any other type of object.
722: *
723: * @return A negative integer, zero, or a positive integer as this buffer
724: * is less than, equal to, or greater than the given buffer.
725: * @throws ClassCastException If the argument is not an int buffer.
726: */
727: public int compareTo(Object ob) {
728: IntBuffer that = (IntBuffer) ob;
729: int n = this .position()
730: + Math.min(this .remaining(), that.remaining());
731: for (int i = this .position(), j = that.position(); i < n; i++, j++) {
732: int v1 = this .get(i);
733: int v2 = that.get(j);
734: if (v1 == v2)
735: continue;
736: if ((v1 != v1) && (v2 != v2)) // For float and double
737: continue;
738: if (v1 < v2)
739: return -1;
740: return +1;
741: }
742: return this.remaining() - that.remaining();
743: }
744:
745: }
|