KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > nio > DoubleBuffer


1 /*
2  * @(#)X-Buffer.java 1.56 04/07/16
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 // -- This file was mechanically generated: Do not edit! -- //
9

10 package java.nio;
11
12
13
14
15
16 /**
17  * A double buffer.
18  *
19  * <p> This class defines four categories of operations upon
20  * double buffers:
21  *
22  * <ul>
23  *
24  * <li><p> Absolute and relative {@link #get() </code><i>get</i><code>} and
25  * {@link #put(double) </code><i>put</i><code>} methods that read and write
26  * single doubles; </p></li>
27  *
28  * <li><p> Relative {@link #get(double[]) </code><i>bulk get</i><code>}
29  * methods that transfer contiguous sequences of doubles from this buffer
30  * into an array; and</p></li>
31  *
32  * <li><p> Relative {@link #put(double[]) </code><i>bulk put</i><code>}
33  * methods that transfer contiguous sequences of doubles from a
34  * double array or some other double
35  * buffer into this buffer;&#32;and </p></li>
36  *
37
38
39
40
41
42
43
44
45
46
47
48
49  *
50  * <li><p> Methods for {@link #compact </code>compacting<code>}, {@link
51  * #duplicate </code>duplicating<code>}, and {@link #slice
52  * </code>slicing<code>} a double buffer. </p></li>
53  *
54  * </ul>
55  *
56  * <p> Double buffers can be created either by {@link #allocate
57  * </code><i>allocation</i><code>}, which allocates space for the buffer's
58  *
59
60
61
62
63
64
65  *
66  * content, by {@link #wrap(double[]) </code><i>wrapping</i><code>} an existing
67  * double array into a buffer, or by creating a
68  * <a HREF="ByteBuffer.html#view"><i>view</i></a> of an existing byte buffer
69  *
70
71  *
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171 *
172
173  *
174  * <p> Like a byte buffer, a double buffer is either <a
175  * HREF="ByteBuffer.html#direct"><i>direct</i> or <i>non-direct</i></a>. A
176  * double buffer created via the <tt>wrap</tt> methods of this class will
177  * be non-direct. A double buffer created as a view of a byte buffer will
178  * be direct if, and only if, the byte buffer itself is direct. Whether or not
179  * a double buffer is direct may be determined by invoking the {@link
180  * #isDirect isDirect} method. </p>
181  *
182
183 *
184
185
186
187
188
189
190
191
192  *
193
194
195
196  *
197  * <p> Methods in this class that do not otherwise have a value to return are
198  * specified to return the buffer upon which they are invoked. This allows
199  * method invocations to be chained.
200  *
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232  *
233  *
234  * @author Mark Reinhold
235  * @author JSR-51 Expert Group
236  * @version 1.56, 04/07/16
237  * @since 1.4
238  */

239
240 public abstract class DoubleBuffer
241     extends Buffer JavaDoc
242     implements Comparable JavaDoc<DoubleBuffer JavaDoc>
243 {
244
245     // These fields are declared here rather than in Heap-X-Buffer in order to
246
// reduce the number of virtual method invocations needed to access these
247
// values, which is especially costly when coding small buffers.
248
//
249
final double[] hb; // Non-null only for heap buffers
250
final int offset;
251     boolean isReadOnly; // Valid only for heap buffers
252

253     // Creates a new buffer with the given mark, position, limit, capacity,
254
// backing array, and array offset
255
//
256
DoubleBuffer(int mark, int pos, int lim, int cap, // package-private
257
double[] hb, int offset)
258     {
259     super(mark, pos, lim, cap);
260     this.hb = hb;
261     this.offset = offset;
262     }
263
264     // Creates a new buffer with the given mark, position, limit, and capacity
265
//
266
DoubleBuffer(int mark, int pos, int lim, int cap) { // package-private
267
this(mark, pos, lim, cap, null, 0);
268     }
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293     /**
294      * Allocates a new double buffer.
295      *
296      * <p> The new buffer's position will be zero, its limit will be its
297      * capacity, and its mark will be undefined. It will have a {@link #array
298      * </code>backing array<code>}, and its {@link #arrayOffset </code>array
299      * offset<code>} will be zero.
300      *
301      * @param capacity
302      * The new buffer's capacity, in doubles
303      *
304      * @return The new double buffer
305      *
306      * @throws IllegalArgumentException
307      * If the <tt>capacity</tt> is a negative integer
308      */

309     public static DoubleBuffer JavaDoc allocate(int capacity) {
310     if (capacity < 0)
311         throw new IllegalArgumentException JavaDoc();
312     return new HeapDoubleBuffer JavaDoc(capacity, capacity);
313     }
314
315     /**
316      * Wraps a double array into a buffer.
317      *
318      * <p> The new buffer will be backed by the given double array;
319      * that is, modifications to the buffer will cause the array to be modified
320      * and vice versa. The new buffer's capacity will be
321      * <tt>array.length</tt>, its position will be <tt>offset</tt>, its limit
322      * will be <tt>offset + length</tt>, and its mark will be undefined. Its
323      * {@link #array </code>backing array<code>} will be the given array, and
324      * its {@link #arrayOffset </code>array offset<code>} will be zero. </p>
325      *
326      * @param array
327      * The array that will back the new buffer
328      *
329      * @param offset
330      * The offset of the subarray to be used; must be non-negative and
331      * no larger than <tt>array.length</tt>. The new buffer's position
332      * will be set to this value.
333      *
334      * @param length
335      * The length of the subarray to be used;
336      * must be non-negative and no larger than
337      * <tt>array.length - offset</tt>.
338      * The new buffer's limit will be set to <tt>offset + length</tt>.
339      *
340      * @return The new double buffer
341      *
342      * @throws IndexOutOfBoundsException
343      * If the preconditions on the <tt>offset</tt> and <tt>length</tt>
344      * parameters do not hold
345      */

346     public static DoubleBuffer JavaDoc wrap(double[] array,
347                     int offset, int length)
348     {
349     try {
350         return new HeapDoubleBuffer JavaDoc(array, offset, length);
351     } catch (IllegalArgumentException JavaDoc x) {
352         throw new IndexOutOfBoundsException JavaDoc();
353     }
354     }
355
356     /**
357      * Wraps a double array into a buffer.
358      *
359      * <p> The new buffer will be backed by the given double array;
360      * that is, modifications to the buffer will cause the array to be modified
361      * and vice versa. The new buffer's capacity and limit will be
362      * <tt>array.length</tt>, its position will be zero, and its mark will be
363      * undefined. Its {@link #array </code>backing array<code>} will be the
364      * given array, and its {@link #arrayOffset </code>array offset<code>} will
365      * be zero. </p>
366      *
367      * @param array
368      * The array that will back this buffer
369      *
370      * @return The new double buffer
371      */

372     public static DoubleBuffer JavaDoc wrap(double[] array) {
373     return wrap(array, 0, array.length);
374     }
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468     /**
469      * Creates a new double buffer whose content is a shared subsequence of
470      * this buffer's content.
471      *
472      * <p> The content of the new buffer will start at this buffer's current
473      * position. Changes to this buffer's content will be visible in the new
474      * buffer, and vice versa; the two buffers' position, limit, and mark
475      * values will be independent.
476      *
477      * <p> The new buffer's position will be zero, its capacity and its limit
478      * will be the number of doubles remaining in this buffer, and its mark
479      * will be undefined. The new buffer will be direct if, and only if, this
480      * buffer is direct, and it will be read-only if, and only if, this buffer
481      * is read-only. </p>
482      *
483      * @return The new double buffer
484      */

485     public abstract DoubleBuffer JavaDoc slice();
486
487     /**
488      * Creates a new double buffer that shares this buffer's content.
489      *
490      * <p> The content of the new buffer will be that of this buffer. Changes
491      * to this buffer's content will be visible in the new buffer, and vice
492      * versa; the two buffers' position, limit, and mark values will be
493      * independent.
494      *
495      * <p> The new buffer's capacity, limit, position, and mark values will be
496      * identical to those of this buffer. The new buffer will be direct if,
497      * and only if, this buffer is direct, and it will be read-only if, and
498      * only if, this buffer is read-only. </p>
499      *
500      * @return The new double buffer
501      */

502     public abstract DoubleBuffer JavaDoc duplicate();
503
504     /**
505      * Creates a new, read-only double buffer that shares this buffer's
506      * content.
507      *
508      * <p> The content of the new buffer will be that of this buffer. Changes
509      * to this buffer's content will be visible in the new buffer; the new
510      * buffer itself, however, will be read-only and will not allow the shared
511      * content to be modified. The two buffers' position, limit, and mark
512      * values will be independent.
513      *
514      * <p> The new buffer's capacity, limit, position, and mark values will be
515      * identical to those of this buffer.
516      *
517      * <p> If this buffer is itself read-only then this method behaves in
518      * exactly the same way as the {@link #duplicate duplicate} method. </p>
519      *
520      * @return The new, read-only double buffer
521      */

522     public abstract DoubleBuffer JavaDoc asReadOnlyBuffer();
523
524
525     // -- Singleton get/put methods --
526

527     /**
528      * Relative <i>get</i> method. Reads the double at this buffer's
529      * current position, and then increments the position. </p>
530      *
531      * @return The double at the buffer's current position
532      *
533      * @throws BufferUnderflowException
534      * If the buffer's current position is not smaller than its limit
535      */

536     public abstract double get();
537
538     /**
539      * Relative <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
540      *
541      * <p> Writes the given double into this buffer at the current
542      * position, and then increments the position. </p>
543      *
544      * @param d
545      * The double to be written
546      *
547      * @return This buffer
548      *
549      * @throws BufferOverflowException
550      * If this buffer's current position is not smaller than its limit
551      *
552      * @throws ReadOnlyBufferException
553      * If this buffer is read-only
554      */

555     public abstract DoubleBuffer JavaDoc put(double d);
556
557     /**
558      * Absolute <i>get</i> method. Reads the double at the given
559      * index. </p>
560      *
561      * @param index
562      * The index from which the double will be read
563      *
564      * @return The double at the given index
565      *
566      * @throws IndexOutOfBoundsException
567      * If <tt>index</tt> is negative
568      * or not smaller than the buffer's limit
569      */

570     public abstract double get(int index);
571
572     /**
573      * Absolute <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
574      *
575      * <p> Writes the given double into this buffer at the given
576      * index. </p>
577      *
578      * @param index
579      * The index at which the double will be written
580      *
581      * @param d
582      * The double value to be written
583      *
584      * @return This buffer
585      *
586      * @throws IndexOutOfBoundsException
587      * If <tt>index</tt> is negative
588      * or not smaller than the buffer's limit
589      *
590      * @throws ReadOnlyBufferException
591      * If this buffer is read-only
592      */

593     public abstract DoubleBuffer JavaDoc put(int index, double d);
594
595
596     // -- Bulk get operations --
597

598     /**
599      * Relative bulk <i>get</i> method.
600      *
601      * <p> This method transfers doubles from this buffer into the given
602      * destination array. If there are fewer doubles remaining in the
603      * buffer than are required to satisfy the request, that is, if
604      * <tt>length</tt>&nbsp;<tt>&gt;</tt>&nbsp;<tt>remaining()</tt>, then no
605      * doubles are transferred and a {@link BufferUnderflowException} is
606      * thrown.
607      *
608      * <p> Otherwise, this method copies <tt>length</tt> doubles from this
609      * buffer into the given array, starting at the current position of this
610      * buffer and at the given offset in the array. The position of this
611      * buffer is then incremented by <tt>length</tt>.
612      *
613      * <p> In other words, an invocation of this method of the form
614      * <tt>src.get(dst,&nbsp;off,&nbsp;len)</tt> has exactly the same effect as
615      * the loop
616      *
617      * <pre>
618      * for (int i = off; i < off + len; i++)
619      * dst[i] = src.get(); </pre>
620      *
621      * except that it first checks that there are sufficient doubles in
622      * this buffer and it is potentially much more efficient. </p>
623      *
624      * @param dst
625      * The array into which doubles are to be written
626      *
627      * @param offset
628      * The offset within the array of the first double to be
629      * written; must be non-negative and no larger than
630      * <tt>dst.length</tt>
631      *
632      * @param length
633      * The maximum number of doubles to be written to the given
634      * array; must be non-negative and no larger than
635      * <tt>dst.length - offset</tt>
636      *
637      * @return This buffer
638      *
639      * @throws BufferUnderflowException
640      * If there are fewer than <tt>length</tt> doubles
641      * remaining in this buffer
642      *
643      * @throws IndexOutOfBoundsException
644      * If the preconditions on the <tt>offset</tt> and <tt>length</tt>
645      * parameters do not hold
646      */

647     public DoubleBuffer JavaDoc get(double[] dst, int offset, int length) {
648     checkBounds(offset, length, dst.length);
649     if (length > remaining())
650         throw new BufferUnderflowException JavaDoc();
651     int end = offset + length;
652     for (int i = offset; i < end; i++)
653         dst[i] = get();
654     return this;
655     }
656
657     /**
658      * Relative bulk <i>get</i> method.
659      *
660      * <p> This method transfers doubles from this buffer into the given
661      * destination array. An invocation of this method of the form
662      * <tt>src.get(a)</tt> behaves in exactly the same way as the invocation
663      *
664      * <pre>
665      * src.get(a, 0, a.length) </pre>
666      *
667      * @return This buffer
668      *
669      * @throws BufferUnderflowException
670      * If there are fewer than <tt>length</tt> doubles
671      * remaining in this buffer
672      */

673     public DoubleBuffer JavaDoc get(double[] dst) {
674     return get(dst, 0, dst.length);
675     }
676
677
678     // -- Bulk put operations --
679

680     /**
681      * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
682      *
683      * <p> This method transfers the doubles remaining in the given source
684      * buffer into this buffer. If there are more doubles remaining in the
685      * source buffer than in this buffer, that is, if
686      * <tt>src.remaining()</tt>&nbsp;<tt>&gt;</tt>&nbsp;<tt>remaining()</tt>,
687      * then no doubles are transferred and a {@link
688      * BufferOverflowException} is thrown.
689      *
690      * <p> Otherwise, this method copies
691      * <i>n</i>&nbsp;=&nbsp;<tt>src.remaining()</tt> doubles from the given
692      * buffer into this buffer, starting at each buffer's current position.
693      * The positions of both buffers are then incremented by <i>n</i>.
694      *
695      * <p> In other words, an invocation of this method of the form
696      * <tt>dst.put(src)</tt> has exactly the same effect as the loop
697      *
698      * <pre>
699      * while (src.hasRemaining())
700      * dst.put(src.get()); </pre>
701      *
702      * except that it first checks that there is sufficient space in this
703      * buffer and it is potentially much more efficient. </p>
704      *
705      * @param src
706      * The source buffer from which doubles are to be read;
707      * must not be this buffer
708      *
709      * @return This buffer
710      *
711      * @throws BufferOverflowException
712      * If there is insufficient space in this buffer
713      * for the remaining doubles in the source buffer
714      *
715      * @throws IllegalArgumentException
716      * If the source buffer is this buffer
717      *
718      * @throws ReadOnlyBufferException
719      * If this buffer is read-only
720      */

721     public DoubleBuffer JavaDoc put(DoubleBuffer JavaDoc src) {
722     if (src == this)
723         throw new IllegalArgumentException JavaDoc();
724     int n = src.remaining();
725     if (n > remaining())
726         throw new BufferOverflowException JavaDoc();
727     for (int i = 0; i < n; i++)
728         put(src.get());
729     return this;
730     }
731
732     /**
733      * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
734      *
735      * <p> This method transfers doubles into this buffer from the given
736      * source array. If there are more doubles to be copied from the array
737      * than remain in this buffer, that is, if
738      * <tt>length</tt>&nbsp;<tt>&gt;</tt>&nbsp;<tt>remaining()</tt>, then no
739      * doubles are transferred and a {@link BufferOverflowException} is
740      * thrown.
741      *
742      * <p> Otherwise, this method copies <tt>length</tt> doubles from the
743      * given array into this buffer, starting at the given offset in the array
744      * and at the current position of this buffer. The position of this buffer
745      * is then incremented by <tt>length</tt>.
746      *
747      * <p> In other words, an invocation of this method of the form
748      * <tt>dst.put(src,&nbsp;off,&nbsp;len)</tt> has exactly the same effect as
749      * the loop
750      *
751      * <pre>
752      * for (int i = off; i < off + len; i++)
753      * dst.put(a[i]); </pre>
754      *
755      * except that it first checks that there is sufficient space in this
756      * buffer and it is potentially much more efficient. </p>
757      *
758      * @param src
759      * The array from which doubles are to be read
760      *
761      * @param offset
762      * The offset within the array of the first double to be read;
763      * must be non-negative and no larger than <tt>array.length</tt>
764      *
765      * @param length
766      * The number of doubles to be read from the given array;
767      * must be non-negative and no larger than
768      * <tt>array.length - offset</tt>
769      *
770      * @return This buffer
771      *
772      * @throws BufferOverflowException
773      * If there is insufficient space in this buffer
774      *
775      * @throws IndexOutOfBoundsException
776      * If the preconditions on the <tt>offset</tt> and <tt>length</tt>
777      * parameters do not hold
778      *
779      * @throws ReadOnlyBufferException
780      * If this buffer is read-only
781      */

782     public DoubleBuffer JavaDoc put(double[] src, int offset, int length) {
783     checkBounds(offset, length, src.length);
784     if (length > remaining())
785         throw new BufferOverflowException JavaDoc();
786     int end = offset + length;
787     for (int i = offset; i < end; i++)
788         this.put(src[i]);
789     return this;
790     }
791
792     /**
793      * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
794      *
795      * <p> This method transfers the entire content of the given source
796      * double array into this buffer. An invocation of this method of the
797      * form <tt>dst.put(a)</tt> behaves in exactly the same way as the
798      * invocation
799      *
800      * <pre>
801      * dst.put(a, 0, a.length) </pre>
802      *
803      * @return This buffer
804      *
805      * @throws BufferOverflowException
806      * If there is insufficient space in this buffer
807      *
808      * @throws ReadOnlyBufferException
809      * If this buffer is read-only
810      */

811     public final DoubleBuffer JavaDoc put(double[] src) {
812     return put(src, 0, src.length);
813     }
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901     // -- Other stuff --
902

903     /**
904      * Tells whether or not this buffer is backed by an accessible double
905      * array.
906      *
907      * <p> If this method returns <tt>true</tt> then the {@link #array() array}
908      * and {@link #arrayOffset() arrayOffset} methods may safely be invoked.
909      * </p>
910      *
911      * @return <tt>true</tt> if, and only if, this buffer
912      * is backed by an array and is not read-only
913      */

914     public final boolean hasArray() {
915     return (hb != null) && !isReadOnly;
916     }
917
918     /**
919      * Returns the double array that backs this
920      * buffer&nbsp;&nbsp;<i>(optional operation)</i>.
921      *
922      * <p> Modifications to this buffer's content will cause the returned
923      * array's content to be modified, and vice versa.
924      *
925      * <p> Invoke the {@link #hasArray hasArray} method before invoking this
926      * method in order to ensure that this buffer has an accessible backing
927      * array. </p>
928      *
929      * @return The array that backs this buffer
930      *
931      * @throws ReadOnlyBufferException
932      * If this buffer is backed by an array but is read-only
933      *
934      * @throws UnsupportedOperationException
935      * If this buffer is not backed by an accessible array
936      */

937     public final double[] array() {
938     if (hb == null)
939         throw new UnsupportedOperationException JavaDoc();
940     if (isReadOnly)
941         throw new ReadOnlyBufferException JavaDoc();
942     return hb;
943     }
944
945     /**
946      * Returns the offset within this buffer's backing array of the first
947      * element of the buffer&nbsp;&nbsp;<i>(optional operation)</i>.
948      *
949      * <p> If this buffer is backed by an array then buffer position <i>p</i>
950      * corresponds to array index <i>p</i>&nbsp;+&nbsp;<tt>arrayOffset()</tt>.
951      *
952      * <p> Invoke the {@link #hasArray hasArray} method before invoking this
953      * method in order to ensure that this buffer has an accessible backing
954      * array. </p>
955      *
956      * @return The offset within this buffer's array
957      * of the first element of the buffer
958      *
959      * @throws ReadOnlyBufferException
960      * If this buffer is backed by an array but is read-only
961      *
962      * @throws UnsupportedOperationException
963      * If this buffer is not backed by an accessible array
964      */

965     public final int arrayOffset() {
966     if (hb == null)
967         throw new UnsupportedOperationException JavaDoc();
968     if (isReadOnly)
969         throw new ReadOnlyBufferException JavaDoc();
970     return offset;
971     }
972
973     /**
974      * Compacts this buffer&nbsp;&nbsp;<i>(optional operation)</i>.
975      *
976      * <p> The doubles between the buffer's current position and its limit,
977      * if any, are copied to the beginning of the buffer. That is, the
978      * double at index <i>p</i>&nbsp;=&nbsp;<tt>position()</tt> is copied
979      * to index zero, the double at index <i>p</i>&nbsp;+&nbsp;1 is copied
980      * to index one, and so forth until the double at index
981      * <tt>limit()</tt>&nbsp;-&nbsp;1 is copied to index
982      * <i>n</i>&nbsp;=&nbsp;<tt>limit()</tt>&nbsp;-&nbsp;<tt>1</tt>&nbsp;-&nbsp;<i>p</i>.
983      * The buffer's position is then set to <i>n+1</i> and its limit is set to
984      * its capacity. The mark, if defined, is discarded.
985      *
986      * <p> The buffer's position is set to the number of doubles copied,
987      * rather than to zero, so that an invocation of this method can be
988      * followed immediately by an invocation of another relative <i>put</i>
989      * method. </p>
990      *
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008     *
1009     * @return This buffer
1010     *
1011     * @throws ReadOnlyBufferException
1012     * If this buffer is read-only
1013     */

1014    public abstract DoubleBuffer JavaDoc compact();
1015
1016    /**
1017     * Tells whether or not this double buffer is direct. </p>
1018     *
1019     * @return <tt>true</tt> if, and only if, this buffer is direct
1020     */

1021    public abstract boolean isDirect();
1022
1023
1024
1025    /**
1026     * Returns a string summarizing the state of this buffer. </p>
1027     *
1028     * @return A summary string
1029     */

1030    public String JavaDoc toString() {
1031    StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
1032    sb.append(getClass().getName());
1033    sb.append("[pos=");
1034    sb.append(position());
1035    sb.append(" lim=");
1036    sb.append(limit());
1037    sb.append(" cap=");
1038    sb.append(capacity());
1039    sb.append("]");
1040    return sb.toString();
1041    }
1042
1043
1044
1045
1046    
1047
1048    /**
1049     * Returns the current hash code of this buffer.
1050     *
1051     * <p> The hash code of a double buffer depends only upon its remaining
1052     * elements; that is, upon the elements from <tt>position()</tt> up to, and
1053     * including, the element at <tt>limit()</tt>&nbsp;-&nbsp;<tt>1</tt>.
1054     *
1055     * <p> Because buffer hash codes are content-dependent, it is inadvisable
1056     * to use buffers as keys in hash maps or similar data structures unless it
1057     * is known that their contents will not change. </p>
1058     *
1059     * @return The current hash code of this buffer
1060     */

1061    public int hashCode() {
1062    int h = 1;
1063    int p = position();
1064    for (int i = limit() - 1; i >= p; i--)
1065        h = 31 * h + (int)get(i);
1066    return h;
1067    }
1068
1069    /**
1070     * Tells whether or not this buffer is equal to another object.
1071     *
1072     * <p> Two double buffers are equal if, and only if,
1073     *
1074     * <p><ol>
1075     *
1076     * <li><p> They have the same element type, </p></li>
1077     *
1078     * <li><p> They have the same number of remaining elements, and
1079     * </p></li>
1080     *
1081     * <li><p> The two sequences of remaining elements, considered
1082     * independently of their starting positions, are pointwise equal.
1083     * </p></li>
1084     *
1085     * </ol>
1086     *
1087     * <p> A double buffer is not equal to any other type of object. </p>
1088     *
1089     * @param ob The object to which this buffer is to be compared
1090     *
1091     * @return <tt>true</tt> if, and only if, this buffer is equal to the
1092     * given object
1093     */

1094    public boolean equals(Object JavaDoc ob) {
1095    if (!(ob instanceof DoubleBuffer JavaDoc))
1096        return false;
1097    DoubleBuffer JavaDoc that = (DoubleBuffer JavaDoc)ob;
1098    if (this.remaining() != that.remaining())
1099        return false;
1100    int p = this.position();
1101    for (int i = this.limit() - 1, j = that.limit() - 1; i >= p; i--, j--) {
1102        double v1 = this.get(i);
1103        double v2 = that.get(j);
1104        if (v1 != v2) {
1105        if ((v1 != v1) && (v2 != v2)) // For float and double
1106
continue;
1107        return false;
1108        }
1109    }
1110    return true;
1111    }
1112
1113    /**
1114     * Compares this buffer to another.
1115     *
1116     * <p> Two double buffers are compared by comparing their sequences of
1117     * remaining elements lexicographically, without regard to the starting
1118     * position of each sequence within its corresponding buffer.
1119     *
1120     * <p> A double buffer is not comparable to any other type of object.
1121     *
1122     * @return A negative integer, zero, or a positive integer as this buffer
1123     * is less than, equal to, or greater than the given buffer
1124     */

1125    public int compareTo(DoubleBuffer JavaDoc that) {
1126    int n = this.position() + Math.min(this.remaining(), that.remaining());
1127    for (int i = this.position(), j = that.position(); i < n; i++, j++) {
1128        double v1 = this.get(i);
1129        double v2 = that.get(j);
1130        if (v1 == v2)
1131        continue;
1132        if ((v1 != v1) && (v2 != v2)) // For float and double
1133
continue;
1134        if (v1 < v2)
1135        return -1;
1136        return +1;
1137    }
1138    return this.remaining() - that.remaining();
1139    }
1140
1141
1142
1143    // -- Other char stuff --
1144

1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338    // -- Other byte stuff: Access to binary data --
1339

1340
1341
1342    /**
1343     * Retrieves this buffer's byte order.
1344     *
1345     * <p> The byte order of a double buffer created by allocation or by
1346     * wrapping an existing <tt>double</tt> array is the {@link
1347     * ByteOrder#nativeOrder </code>native order<code>} of the underlying
1348     * hardware. The byte order of a double buffer created as a <a
1349     * HREF="ByteBuffer.html#view">view</a> of a byte buffer is that of the
1350     * byte buffer at the moment that the view is created. </p>
1351     *
1352     * @return This buffer's byte order
1353     */

1354    public abstract ByteOrder JavaDoc order();
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408}
1409
Popular Tags