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