1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.tests.java.nio;
19
20import java.io.RandomAccessFile;
21import java.nio.BufferOverflowException;
22import java.nio.BufferUnderflowException;
23import java.nio.ByteBuffer;
24import java.nio.ByteOrder;
25import java.nio.CharBuffer;
26import java.nio.DoubleBuffer;
27import java.nio.FloatBuffer;
28import java.nio.IntBuffer;
29import java.nio.InvalidMarkException;
30import java.nio.LongBuffer;
31import java.nio.ReadOnlyBufferException;
32import java.nio.ShortBuffer;
33import java.nio.channels.FileChannel;
34import java.nio.file.Files;
35import java.nio.file.Path;
36import java.util.Arrays;
37
38/**
39 * Tests java.nio.ByteBuffer
40 *
41 */
42public class ByteBufferTest extends AbstractBufferTest {
43    protected static final int SMALL_TEST_LENGTH = 5;
44    protected static final int BUFFER_LENGTH = 250;
45
46    protected ByteBuffer buf;
47
48    protected void setUp() throws Exception {
49        buf = ByteBuffer.allocate(10);
50        loadTestData1(buf);
51        baseBuf = buf;
52    }
53
54    protected void tearDown() throws Exception {
55        super.tearDown();
56    }
57
58    public void testArray() {
59        if (buf.hasArray()) {
60            byte array[] = buf.array();
61            assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
62
63            loadTestData1(array, buf.arrayOffset(), buf.capacity());
64            assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
65
66            loadTestData2(array, buf.arrayOffset(), buf.capacity());
67            assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
68
69            loadTestData1(buf);
70            assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
71
72            loadTestData2(buf);
73            assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
74        } else {
75            if (buf.isReadOnly()) {
76                try {
77                    buf.array();
78                    fail("Should throw Exception");
79                } catch (UnsupportedOperationException e) {
80                    // expected
81                    // Note:can not tell when to throw
82                    // UnsupportedOperationException
83                    // or ReadOnlyBufferException, so catch all.
84                }
85            } else {
86                try {
87                    buf.array();
88                    fail("Should throw Exception");
89                } catch (UnsupportedOperationException e) {
90                    // expected
91                }
92            }
93        }
94    }
95
96    public void testArrayOffset() {
97        if (buf.hasArray()) {
98            byte array[] = buf.array();
99            assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
100
101            loadTestData1(array, buf.arrayOffset(), buf.capacity());
102            assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
103
104            loadTestData2(array, buf.arrayOffset(), buf.capacity());
105            assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
106
107            loadTestData1(buf);
108            assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
109
110            loadTestData2(buf);
111            assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
112        } else {
113            if (buf.isReadOnly()) {
114                try {
115                    buf.arrayOffset();
116                    fail("Should throw Exception");
117                } catch (UnsupportedOperationException e) {
118                    // expected
119                    // Note:can not tell when to throw
120                    // UnsupportedOperationException
121                    // or ReadOnlyBufferException, so catch all.
122                }
123            } else {
124                try {
125                    buf.arrayOffset();
126                    fail("Should throw Exception");
127                } catch (UnsupportedOperationException e) {
128                    // expected
129                }
130            }
131        }
132    }
133
134    public void testAsReadOnlyBuffer() {
135        buf.clear();
136        buf.mark();
137        buf.position(buf.limit());
138
139        // readonly's contents should be the same as buf
140        ByteBuffer readonly = buf.asReadOnlyBuffer();
141        assertNotSame(buf, readonly);
142        assertTrue(readonly.isReadOnly());
143        assertEquals(buf.position(), readonly.position());
144        assertEquals(buf.limit(), readonly.limit());
145        assertEquals(buf.isDirect(), readonly.isDirect());
146        assertEquals(buf.order(), readonly.order());
147        assertContentEquals(buf, readonly);
148
149        // readonly's position, mark, and limit should be independent to buf
150        readonly.reset();
151        assertEquals(readonly.position(), 0);
152        readonly.clear();
153        assertEquals(buf.position(), buf.limit());
154        buf.reset();
155        assertEquals(buf.position(), 0);
156    }
157
158    public void testCompact() {
159        if (buf.isReadOnly()) {
160            try {
161                buf.compact();
162                fail("Should throw Exception");
163            } catch (ReadOnlyBufferException e) {
164                // expected
165            }
166            return;
167        }
168
169        // case: buffer is full
170        buf.clear();
171        buf.mark();
172        loadTestData1(buf);
173        ByteBuffer ret = buf.compact();
174        assertSame(ret, buf);
175        assertEquals(buf.position(), buf.capacity());
176        assertEquals(buf.limit(), buf.capacity());
177        assertContentLikeTestData1(buf, 0, (byte) 0, buf.capacity());
178        try {
179            buf.reset();
180            fail("Should throw Exception");
181        } catch (InvalidMarkException e) {
182            // expected
183        }
184
185        // case: buffer is empty
186        buf.position(0);
187        buf.limit(0);
188        buf.mark();
189        ret = buf.compact();
190        assertSame(ret, buf);
191        assertEquals(buf.position(), 0);
192        assertEquals(buf.limit(), buf.capacity());
193        assertContentLikeTestData1(buf, 0, (byte) 0, buf.capacity());
194        try {
195            buf.reset();
196            fail("Should throw Exception");
197        } catch (InvalidMarkException e) {
198            // expected
199        }
200
201        // case: normal
202        assertTrue(buf.capacity() > SMALL_TEST_LENGTH);
203        buf.position(1);
204        buf.limit(SMALL_TEST_LENGTH);
205        buf.mark();
206        ret = buf.compact();
207        assertSame(ret, buf);
208        assertEquals(buf.position(), 4);
209        assertEquals(buf.limit(), buf.capacity());
210        assertContentLikeTestData1(buf, 0, (byte) 1, 4);
211        try {
212            buf.reset();
213            fail("Should throw Exception");
214        } catch (InvalidMarkException e) {
215            // expected
216        }
217    }
218
219    public void testCompareTo() {
220        // compare to self
221        assertEquals(0, buf.compareTo(buf));
222
223        // normal cases
224        if (!buf.isReadOnly()) {
225            assertTrue(buf.capacity() > SMALL_TEST_LENGTH);
226            buf.clear();
227            ByteBuffer other = ByteBuffer.allocate(buf.capacity());
228            loadTestData1(buf);
229            loadTestData1(other);
230            assertEquals(0, buf.compareTo(other));
231            assertEquals(0, other.compareTo(buf));
232            buf.position(1);
233            assertTrue(buf.compareTo(other) > 0);
234            assertTrue(other.compareTo(buf) < 0);
235            other.position(2);
236            assertTrue(buf.compareTo(other) < 0);
237            assertTrue(other.compareTo(buf) > 0);
238            buf.position(2);
239            other.limit(SMALL_TEST_LENGTH);
240            assertTrue(buf.compareTo(other) > 0);
241            assertTrue(other.compareTo(buf) < 0);
242        }
243
244        assertTrue(ByteBuffer.wrap(new byte[21]).compareTo(ByteBuffer.allocateDirect(21)) == 0);
245    }
246
247    public void testDuplicate() {
248        buf.clear();
249        buf.mark();
250        buf.position(buf.limit());
251
252        // duplicate's contents should be the same as buf
253        ByteBuffer duplicate = buf.duplicate();
254        assertNotSame(buf, duplicate);
255        assertEquals(buf.position(), duplicate.position());
256        assertEquals(buf.limit(), duplicate.limit());
257        assertEquals(buf.isReadOnly(), duplicate.isReadOnly());
258        assertEquals(buf.isDirect(), duplicate.isDirect());
259        assertEquals(buf.order(), duplicate.order());
260        assertContentEquals(buf, duplicate);
261
262        // duplicate's position, mark, and limit should be independent to buf
263        duplicate.reset();
264        assertEquals(duplicate.position(), 0);
265        duplicate.clear();
266        assertEquals(buf.position(), buf.limit());
267        buf.reset();
268        assertEquals(buf.position(), 0);
269
270        // duplicate share the same content with buf
271        if (!duplicate.isReadOnly()) {
272            loadTestData1(buf);
273            assertContentEquals(buf, duplicate);
274            loadTestData2(duplicate);
275            assertContentEquals(buf, duplicate);
276        }
277    }
278
279    public void testEquals() {
280        // equal to self
281        assertTrue(buf.equals(buf));
282        ByteBuffer readonly = buf.asReadOnlyBuffer();
283        assertTrue(buf.equals(readonly));
284        ByteBuffer duplicate = buf.duplicate();
285        assertTrue(buf.equals(duplicate));
286
287        // always false, if type mismatch
288        assertFalse(buf.equals(Boolean.TRUE));
289
290        assertTrue(buf.capacity() > SMALL_TEST_LENGTH);
291
292        buf.limit(buf.capacity()).position(0);
293        readonly.limit(readonly.capacity()).position(1);
294        assertFalse(buf.equals(readonly));
295
296        buf.limit(buf.capacity() - 1).position(0);
297        duplicate.limit(duplicate.capacity()).position(0);
298        assertFalse(buf.equals(duplicate));
299    }
300
301    /*
302     * Class under test for byte get()
303     */
304    public void testGet() {
305        buf.clear();
306        for (int i = 0; i < buf.capacity(); i++) {
307            assertEquals(buf.position(), i);
308            assertEquals(buf.get(), buf.get(i));
309        }
310        try {
311            buf.get();
312            fail("Should throw Exception");
313        } catch (BufferUnderflowException e) {
314            // expected
315        }
316    }
317
318    /*
319     * Class under test for java.nio.ByteBuffer get(byte[])
320     */
321    public void testGetbyteArray() {
322        byte array[] = new byte[1];
323        buf.clear();
324        for (int i = 0; i < buf.capacity(); i++) {
325            assertEquals(buf.position(), i);
326            ByteBuffer ret = buf.get(array);
327            assertEquals(array[0], buf.get(i));
328            assertSame(ret, buf);
329        }
330        try {
331            buf.get(array);
332            fail("Should throw Exception");
333        } catch (BufferUnderflowException e) {
334            // expected
335        }
336        try {
337            buf.get((byte[])null);
338            fail("Should throw Exception");
339        } catch (NullPointerException e) {
340            // expected
341        }
342    }
343
344    /*
345     * Class under test for java.nio.ByteBuffer get(byte[], int, int)
346     */
347    public void testGetbyteArrayintint() {
348        buf.clear();
349        byte array[] = new byte[buf.capacity()];
350
351        try {
352            buf.get(new byte[buf.capacity() + 1], 0, buf.capacity() + 1);
353            fail("Should throw Exception");
354        } catch (BufferUnderflowException e) {
355            // expected
356        }
357        assertEquals(buf.position(), 0);
358        try {
359            buf.get(array, -1, array.length);
360            fail("Should throw Exception");
361        } catch (IndexOutOfBoundsException e) {
362            // expected
363        }
364        buf.get(array, array.length, 0);
365        try {
366            buf.get(array, array.length + 1, 1);
367            fail("Should throw Exception");
368        } catch (IndexOutOfBoundsException e) {
369            // expected
370        }
371        assertEquals(buf.position(), 0);
372        try {
373            buf.get(array, 2, -1);
374            fail("Should throw Exception");
375        } catch (IndexOutOfBoundsException e) {
376            // expected
377        }
378        try {
379            buf.get(array, 2, array.length);
380            fail("Should throw Exception");
381        } catch (IndexOutOfBoundsException e) {
382            // expected
383        }
384        try {
385            buf.get((byte[])null, -1, 0);
386            fail("Should throw Exception");
387        } catch (NullPointerException e) {
388            // expected
389        }
390        try {
391            buf.get(array, 1, Integer.MAX_VALUE);
392            fail("Should throw Exception");
393        } catch (IndexOutOfBoundsException e) {
394            // expected
395        }
396        try {
397            buf.get(array, Integer.MAX_VALUE, 1);
398            fail("Should throw Exception");
399        } catch (IndexOutOfBoundsException e) {
400            // expected
401        }
402        assertEquals(buf.position(), 0);
403
404        buf.clear();
405        ByteBuffer ret = buf.get(array, 0, array.length);
406        assertEquals(buf.position(), buf.capacity());
407        assertContentEquals(buf, array, 0, array.length);
408        assertSame(ret, buf);
409    }
410
411    /*
412     * Class under test for byte get(int)
413     */
414    public void testGetint() {
415        buf.clear();
416        for (int i = 0; i < buf.capacity(); i++) {
417            assertEquals(buf.position(), i);
418            assertEquals(buf.get(), buf.get(i));
419        }
420        try {
421            buf.get(-1);
422            fail("Should throw Exception");
423        } catch (IndexOutOfBoundsException e) {
424            // expected
425        }
426        try {
427            buf.get(buf.limit());
428            fail("Should throw Exception");
429        } catch (IndexOutOfBoundsException e) {
430            // expected
431        }
432    }
433
434    public void testHasArray() {
435        if (buf.hasArray()) {
436            assertNotNull(buf.array());
437        } else {
438            if (buf.isReadOnly()) {
439                try {
440                    buf.array();
441                    fail("Should throw Exception");
442                } catch (UnsupportedOperationException e) {
443                    // expected
444                    // Note:can not tell when to throw
445                    // UnsupportedOperationException
446                    // or ReadOnlyBufferException, so catch all.
447                }
448            } else {
449                try {
450                    buf.array();
451                    fail("Should throw Exception");
452                } catch (UnsupportedOperationException e) {
453                    // expected
454                }
455            }
456        }
457    }
458
459    public void testHashCode() {
460        buf.clear();
461        loadTestData1(buf);
462        ByteBuffer readonly = buf.asReadOnlyBuffer();
463        ByteBuffer duplicate = buf.duplicate();
464        assertTrue(buf.hashCode() == readonly.hashCode());
465        assertTrue(buf.capacity() > SMALL_TEST_LENGTH);
466        duplicate.position(buf.capacity()/2);
467        assertTrue(buf.hashCode()!= duplicate.hashCode());
468    }
469
470    //for the testHashCode() method of readonly subclasses
471    protected void readOnlyHashCode() {
472        //create a new buffer initiated with some data
473        ByteBuffer buf = ByteBuffer.allocate(BUFFER_LENGTH);
474        loadTestData1(buf);
475        buf = buf.asReadOnlyBuffer();
476        buf.clear();
477        ByteBuffer readonly = buf.asReadOnlyBuffer();
478        ByteBuffer duplicate = buf.duplicate();
479        assertEquals(buf.hashCode(),readonly.hashCode());
480        duplicate.position(buf.capacity()/2);
481        assertTrue(buf.hashCode()!= duplicate.hashCode());
482    }
483
484    public void testIsDirect() {
485        buf.isDirect();
486    }
487
488    public void testOrder() {
489        // BIG_ENDIAN is the default byte order
490        assertEquals(ByteOrder.BIG_ENDIAN, buf.order());
491
492        buf.order(ByteOrder.LITTLE_ENDIAN);
493        assertEquals(ByteOrder.LITTLE_ENDIAN, buf.order());
494
495        buf.order(ByteOrder.BIG_ENDIAN);
496        assertEquals(ByteOrder.BIG_ENDIAN, buf.order());
497
498        // Regression test for HARMONY-798
499        buf.order((ByteOrder)null);
500        assertEquals(ByteOrder.LITTLE_ENDIAN, buf.order());
501
502        buf.order(ByteOrder.BIG_ENDIAN);
503    }
504
505    /*
506     * Class under test for java.nio.ByteBuffer put(byte)
507     */
508    public void testPutbyte() {
509        if (buf.isReadOnly()) {
510            try {
511                buf.clear();
512                buf.put((byte) 0);
513                fail("Should throw Exception");
514            } catch (ReadOnlyBufferException e) {
515                // expected
516            }
517            return;
518        }
519
520        buf.clear();
521        for (int i = 0; i < buf.capacity(); i++) {
522            assertEquals(buf.position(), i);
523            ByteBuffer ret = buf.put((byte) i);
524            assertEquals(buf.get(i), (byte) i);
525            assertSame(ret, buf);
526        }
527        try {
528            buf.put((byte) 0);
529            fail("Should throw Exception");
530        } catch (BufferOverflowException e) {
531            // expected
532        }
533    }
534
535    /*
536     * Class under test for java.nio.ByteBuffer put(byte[])
537     */
538    public void testPutbyteArray() {
539        byte array[] = new byte[1];
540        if (buf.isReadOnly()) {
541            try {
542                buf.put(array);
543                fail("Should throw Exception");
544            } catch (ReadOnlyBufferException e) {
545                // expected
546            }
547            return;
548        }
549
550        buf.clear();
551        for (int i = 0; i < buf.capacity(); i++) {
552            assertEquals(buf.position(), i);
553            array[0] = (byte) i;
554            ByteBuffer ret = buf.put(array);
555            assertEquals(buf.get(i), (byte) i);
556            assertSame(ret, buf);
557        }
558        try {
559            buf.put(array);
560            fail("Should throw Exception");
561        } catch (BufferOverflowException e) {
562            // expected
563        }
564        try {
565            buf.put((byte[])null);
566            fail("Should throw Exception");
567        } catch (NullPointerException e) {
568            // expected
569        }
570    }
571
572    /*
573     * Class under test for java.nio.ByteBuffer put(byte[], int, int)
574     */
575    public void testPutbyteArrayintint() {
576        buf.clear();
577        byte array[] = new byte[buf.capacity()];
578        if (buf.isReadOnly()) {
579            try {
580                buf.put(array, 0, array.length);
581                fail("Should throw Exception");
582            } catch (ReadOnlyBufferException e) {
583                // expected
584            }
585            return;
586        }
587
588        try {
589            buf.put(new byte[buf.capacity() + 1], 0, buf.capacity() + 1);
590            fail("Should throw Exception");
591        } catch (BufferOverflowException e) {
592            // expected
593        }
594        assertEquals(buf.position(), 0);
595        try {
596            buf.put(array, -1, array.length);
597            fail("Should throw Exception");
598        } catch (IndexOutOfBoundsException e) {
599            // expected
600        }
601        try {
602            buf.put(array, array.length + 1, 0);
603            fail("Should throw Exception");
604        } catch (IndexOutOfBoundsException e) {
605            // expected
606        }
607        buf.put(array, array.length, 0);
608        assertEquals(buf.position(), 0);
609        try {
610            buf.put(array, 0, -1);
611            fail("Should throw Exception");
612        } catch (IndexOutOfBoundsException e) {
613            // expected
614        }
615        try {
616            buf.put(array, 2, array.length);
617            fail("Should throw Exception");
618        } catch (IndexOutOfBoundsException e) {
619            // expected
620        }
621
622        try {
623            buf.put(array, 2, Integer.MAX_VALUE);
624            fail("Should throw Exception");
625        } catch (IndexOutOfBoundsException e) {
626            // expected
627        }
628        try {
629            buf.put(array, Integer.MAX_VALUE, 1);
630            fail("Should throw Exception");
631        } catch (IndexOutOfBoundsException e) {
632            // expected
633        }
634        try {
635            buf.put((byte[])null, 2, Integer.MAX_VALUE);
636            fail("Should throw Exception");
637        } catch (NullPointerException e) {
638            // expected
639        }
640
641        assertEquals(buf.position(), 0);
642
643        loadTestData2(array, 0, array.length);
644        ByteBuffer ret = buf.put(array, 0, array.length);
645        assertEquals(buf.position(), buf.capacity());
646        assertContentEquals(buf, array, 0, array.length);
647        assertSame(ret, buf);
648    }
649
650    /*
651     * Class under test for java.nio.ByteBuffer put(java.nio.ByteBuffer)
652     */
653    public void testPutByteBuffer() {
654        ByteBuffer other = ByteBuffer.allocate(buf.capacity());
655        if (buf.isReadOnly()) {
656            try {
657                buf.clear();
658                buf.put(other);
659                fail("Should throw Exception");
660            } catch (ReadOnlyBufferException e) {
661                // expected
662            }
663            try {
664                buf.clear();
665                buf.put((ByteBuffer)null);
666                fail("Should throw Exception");
667            } catch (ReadOnlyBufferException e) {
668                // expected
669            }
670        } else {
671            try {
672                buf.put(buf);
673                fail("Should throw Exception");
674            } catch (IllegalArgumentException e) {
675                // expected
676            }
677            try {
678                buf.put(ByteBuffer.allocate(buf.capacity() + 1));
679                fail("Should throw Exception");
680            } catch (BufferOverflowException e) {
681                // expected
682            }
683
684            try {
685                buf.put((ByteBuffer)null);
686                fail("Should throw Exception");
687            } catch (NullPointerException e) {
688                // expected
689            }
690            loadTestData2(other);
691            other.clear();
692            buf.clear();
693            ByteBuffer ret = buf.put(other);
694            assertEquals(other.position(), other.capacity());
695            assertEquals(buf.position(), buf.capacity());
696            assertContentEquals(other, buf);
697            assertSame(ret, buf);
698        }
699
700        other = ByteBuffer.allocateDirect(buf.capacity());
701        if (buf.isReadOnly()) {
702            try {
703                buf.clear();
704                buf.put(other);
705                fail("Should throw Exception");
706            } catch (ReadOnlyBufferException e) {
707                // expected
708            }
709            try {
710                buf.clear();
711                buf.put((ByteBuffer)null);
712                fail("Should throw Exception");
713            } catch (ReadOnlyBufferException e) {
714                // expected
715            }
716        } else {
717            try {
718                buf.put(buf);
719                fail("Should throw Exception");
720            } catch (IllegalArgumentException e) {
721                // expected
722            }
723            try {
724                buf.put(ByteBuffer.allocate(buf.capacity() + 1));
725                fail("Should throw Exception");
726            } catch (BufferOverflowException e) {
727                // expected
728            }
729
730            try {
731                buf.put((ByteBuffer)null);
732                fail("Should throw Exception");
733            } catch (NullPointerException e) {
734                // expected
735            }
736            loadTestData2(other);
737            other.clear();
738            buf.clear();
739            ByteBuffer ret = buf.put(other);
740            assertEquals(other.position(), other.capacity());
741            assertEquals(buf.position(), buf.capacity());
742            assertContentEquals(other, buf);
743            assertSame(ret, buf);
744        }
745    }
746
747    /*
748     * Class under test for java.nio.ByteBuffer put(int, byte)
749     */
750    public void testPutintbyte() {
751        if (buf.isReadOnly()) {
752            try {
753                buf.put(0, (byte) 0);
754                fail("Should throw Exception");
755            } catch (ReadOnlyBufferException e) {
756                // expected
757            }
758            return;
759        }
760
761        buf.clear();
762        for (int i = 0; i < buf.capacity(); i++) {
763            assertEquals(buf.position(), 0);
764            ByteBuffer ret = buf.put(i, (byte) i);
765            assertEquals(buf.get(i), (byte) i);
766            assertSame(ret, buf);
767        }
768        try {
769            buf.put(-1, (byte) 0);
770            fail("Should throw Exception");
771        } catch (IndexOutOfBoundsException e) {
772            // expected
773        }
774        try {
775            buf.put(buf.limit(), (byte) 0);
776            fail("Should throw Exception");
777        } catch (IndexOutOfBoundsException e) {
778            // expected
779        }
780    }
781
782    public void testSlice() {
783        assertTrue(buf.capacity() > SMALL_TEST_LENGTH);
784        buf.position(1);
785        buf.limit(buf.capacity() - 1);
786
787        ByteBuffer slice = buf.slice();
788        assertEquals(buf.isReadOnly(), slice.isReadOnly());
789        assertEquals(buf.isDirect(), slice.isDirect());
790        assertEquals(buf.order(), slice.order());
791        assertEquals(slice.position(), 0);
792        assertEquals(slice.limit(), buf.remaining());
793        assertEquals(slice.capacity(), buf.remaining());
794        try {
795            slice.reset();
796            fail("Should throw Exception");
797        } catch (InvalidMarkException e) {
798            // expected
799        }
800
801        // slice share the same content with buf
802        if (!slice.isReadOnly()) {
803            loadTestData1(slice);
804            assertContentLikeTestData1(buf, 1, (byte) 0, slice.capacity());
805            buf.put(2, (byte) 100);
806            assertEquals(slice.get(1), 100);
807        }
808    }
809
810    public void testToString() {
811        String str = buf.toString();
812        assertTrue(str.indexOf("Byte") >= 0 || str.indexOf("byte") >= 0);
813        assertTrue(str.indexOf("" + buf.position()) >= 0);
814        assertTrue(str.indexOf("" + buf.limit()) >= 0);
815        assertTrue(str.indexOf("" + buf.capacity()) >= 0);
816    }
817
818    public void testAsCharBuffer() {
819        CharBuffer charBuffer;
820        byte bytes[] = new byte[2];
821        char value;
822
823        // test BIG_ENDIAN char buffer, read
824        buf.clear();
825        buf.order(ByteOrder.BIG_ENDIAN);
826        charBuffer = buf.asCharBuffer();
827        assertSame(ByteOrder.BIG_ENDIAN, charBuffer.order());
828        while (charBuffer.remaining() > 0) {
829            buf.get(bytes);
830            value = charBuffer.get();
831            assertEquals(bytes2char(bytes, buf.order()), value);
832        }
833
834        // test LITTLE_ENDIAN char buffer, read
835        buf.clear();
836        buf.order(ByteOrder.LITTLE_ENDIAN);
837        charBuffer = buf.asCharBuffer();
838        assertSame(ByteOrder.LITTLE_ENDIAN, charBuffer.order());
839        while (charBuffer.remaining() > 0) {
840            buf.get(bytes);
841            value = charBuffer.get();
842            assertEquals(bytes2char(bytes, buf.order()), value);
843        }
844
845        if (!buf.isReadOnly()) {
846            // test BIG_ENDIAN char buffer, write
847            buf.clear();
848            buf.order(ByteOrder.BIG_ENDIAN);
849            charBuffer = buf.asCharBuffer();
850            assertSame(ByteOrder.BIG_ENDIAN, charBuffer.order());
851            while (charBuffer.remaining() > 0) {
852                value = (char) charBuffer.remaining();
853                charBuffer.put(value);
854                buf.get(bytes);
855                assertTrue(Arrays.equals(bytes, char2bytes(value, buf.order())));
856            }
857
858            // test LITTLE_ENDIAN char buffer, write
859            buf.clear();
860            buf.order(ByteOrder.LITTLE_ENDIAN);
861            charBuffer = buf.asCharBuffer();
862            assertSame(ByteOrder.LITTLE_ENDIAN, charBuffer.order());
863            while (charBuffer.remaining() > 0) {
864                value = (char) charBuffer.remaining();
865                charBuffer.put(value);
866                buf.get(bytes);
867                assertTrue(Arrays.equals(bytes, char2bytes(value, buf.order())));
868            }
869        }
870        buf.clear();
871        buf.order(ByteOrder.BIG_ENDIAN);
872    }
873
874    public void testAsDoubleBuffer() {
875        DoubleBuffer doubleBuffer;
876        byte bytes[] = new byte[8];
877        double value;
878
879        // test BIG_ENDIAN double buffer, read
880        buf.clear();
881        buf.order(ByteOrder.BIG_ENDIAN);
882        doubleBuffer = buf.asDoubleBuffer();
883        assertSame(ByteOrder.BIG_ENDIAN, doubleBuffer.order());
884        while (doubleBuffer.remaining() > 0) {
885            buf.get(bytes);
886            value = doubleBuffer.get();
887            if (!(Double.isNaN(bytes2double(bytes, buf.order())) && Double
888                    .isNaN(value))) {
889                assertEquals(bytes2double(bytes, buf.order()), value, 0.00);
890            }
891        }
892
893        // test LITTLE_ENDIAN double buffer, read
894        buf.clear();
895        buf.order(ByteOrder.LITTLE_ENDIAN);
896        doubleBuffer = buf.asDoubleBuffer();
897        assertSame(ByteOrder.LITTLE_ENDIAN, doubleBuffer.order());
898        while (doubleBuffer.remaining() > 0) {
899            buf.get(bytes);
900            value = doubleBuffer.get();
901            if (!(Double.isNaN(bytes2double(bytes, buf.order())) && Double
902                    .isNaN(value))) {
903                assertEquals(bytes2double(bytes, buf.order()), value, 0.00);
904            }
905        }
906
907        if (!buf.isReadOnly()) {
908            // test BIG_ENDIAN double buffer, write
909            buf.clear();
910            buf.order(ByteOrder.BIG_ENDIAN);
911            doubleBuffer = buf.asDoubleBuffer();
912            assertSame(ByteOrder.BIG_ENDIAN, doubleBuffer.order());
913            while (doubleBuffer.remaining() > 0) {
914                value = (double) doubleBuffer.remaining();
915                doubleBuffer.put(value);
916                buf.get(bytes);
917                assertTrue(Arrays.equals(bytes, double2bytes(value, buf.order())));
918            }
919
920            // test LITTLE_ENDIAN double buffer, write
921            buf.clear();
922            buf.order(ByteOrder.LITTLE_ENDIAN);
923            doubleBuffer = buf.asDoubleBuffer();
924            assertSame(ByteOrder.LITTLE_ENDIAN, doubleBuffer.order());
925            while (doubleBuffer.remaining() > 0) {
926                value = (double) doubleBuffer.remaining();
927                doubleBuffer.put(value);
928                buf.get(bytes);
929                assertTrue(Arrays.equals(bytes, double2bytes(value, buf.order())));
930            }
931        }
932
933        buf.clear();
934        buf.order(ByteOrder.BIG_ENDIAN);
935    }
936
937    public void testAsFloatBuffer() {
938        FloatBuffer floatBuffer;
939        byte bytes[] = new byte[4];
940        float value;
941
942        // test BIG_ENDIAN float buffer, read
943        buf.clear();
944        buf.order(ByteOrder.BIG_ENDIAN);
945        floatBuffer = buf.asFloatBuffer();
946        assertSame(ByteOrder.BIG_ENDIAN, floatBuffer.order());
947        while (floatBuffer.remaining() > 0) {
948            buf.get(bytes);
949            value = floatBuffer.get();
950            if (!(Float.isNaN(bytes2float(bytes, buf.order())) && Float
951                    .isNaN(value))) {
952                assertEquals(bytes2float(bytes, buf.order()), value, 0.00);
953            }
954        }
955
956        // test LITTLE_ENDIAN float buffer, read
957        buf.clear();
958        buf.order(ByteOrder.LITTLE_ENDIAN);
959        floatBuffer = buf.asFloatBuffer();
960        assertSame(ByteOrder.LITTLE_ENDIAN, floatBuffer.order());
961        while (floatBuffer.remaining() > 0) {
962            buf.get(bytes);
963            value = floatBuffer.get();
964            if (!(Float.isNaN(bytes2float(bytes, buf.order())) && Float
965                    .isNaN(value))) {
966                assertEquals(bytes2float(bytes, buf.order()), value, 0.00);
967            }
968        }
969
970        if (!buf.isReadOnly()) {
971            // test BIG_ENDIAN float buffer, write
972            buf.clear();
973            buf.order(ByteOrder.BIG_ENDIAN);
974            floatBuffer = buf.asFloatBuffer();
975            assertSame(ByteOrder.BIG_ENDIAN, floatBuffer.order());
976            while (floatBuffer.remaining() > 0) {
977                value = (float) floatBuffer.remaining();
978                floatBuffer.put(value);
979                buf.get(bytes);
980                assertTrue(Arrays.equals(bytes, float2bytes(value, buf.order())));
981            }
982
983            // test LITTLE_ENDIAN float buffer, write
984            buf.clear();
985            buf.order(ByteOrder.LITTLE_ENDIAN);
986            floatBuffer = buf.asFloatBuffer();
987            assertSame(ByteOrder.LITTLE_ENDIAN, floatBuffer.order());
988            while (floatBuffer.remaining() > 0) {
989                value = (float) floatBuffer.remaining();
990                floatBuffer.put(value);
991                buf.get(bytes);
992                assertTrue(Arrays.equals(bytes, float2bytes(value, buf.order())));
993            }
994        }
995
996        buf.clear();
997        buf.order(ByteOrder.BIG_ENDIAN);
998    }
999
1000    public void testAsIntBuffer() {
1001        IntBuffer intBuffer;
1002        byte bytes[] = new byte[4];
1003        int value;
1004
1005        // test BIG_ENDIAN int buffer, read
1006        buf.clear();
1007        buf.order(ByteOrder.BIG_ENDIAN);
1008        intBuffer = buf.asIntBuffer();
1009        assertSame(ByteOrder.BIG_ENDIAN, intBuffer.order());
1010        while (intBuffer.remaining() > 0) {
1011            buf.get(bytes);
1012            value = intBuffer.get();
1013            assertEquals(bytes2int(bytes, buf.order()), value);
1014        }
1015
1016        // test LITTLE_ENDIAN int buffer, read
1017        buf.clear();
1018        buf.order(ByteOrder.LITTLE_ENDIAN);
1019        intBuffer = buf.asIntBuffer();
1020        assertSame(ByteOrder.LITTLE_ENDIAN, intBuffer.order());
1021        while (intBuffer.remaining() > 0) {
1022            buf.get(bytes);
1023            value = intBuffer.get();
1024            assertEquals(bytes2int(bytes, buf.order()), value);
1025        }
1026
1027        if (!buf.isReadOnly()) {
1028            // test BIG_ENDIAN int buffer, write
1029            buf.clear();
1030            buf.order(ByteOrder.BIG_ENDIAN);
1031            intBuffer = buf.asIntBuffer();
1032            assertSame(ByteOrder.BIG_ENDIAN, intBuffer.order());
1033            while (intBuffer.remaining() > 0) {
1034                value = (int) intBuffer.remaining();
1035                intBuffer.put(value);
1036                buf.get(bytes);
1037                assertTrue(Arrays.equals(bytes, int2bytes(value, buf.order())));
1038            }
1039
1040            // test LITTLE_ENDIAN int buffer, write
1041            buf.clear();
1042            buf.order(ByteOrder.LITTLE_ENDIAN);
1043            intBuffer = buf.asIntBuffer();
1044            assertSame(ByteOrder.LITTLE_ENDIAN, intBuffer.order());
1045            while (intBuffer.remaining() > 0) {
1046                value = (int) intBuffer.remaining();
1047                intBuffer.put(value);
1048                buf.get(bytes);
1049                assertTrue(Arrays.equals(bytes, int2bytes(value, buf.order())));
1050            }
1051        }
1052
1053        buf.clear();
1054        buf.order(ByteOrder.BIG_ENDIAN);
1055    }
1056
1057    public void testAsLongBuffer() {
1058        LongBuffer longBuffer;
1059        byte bytes[] = new byte[8];
1060        long value;
1061
1062        // test BIG_ENDIAN long buffer, read
1063        buf.clear();
1064        buf.order(ByteOrder.BIG_ENDIAN);
1065        longBuffer = buf.asLongBuffer();
1066        assertSame(ByteOrder.BIG_ENDIAN, longBuffer.order());
1067        while (longBuffer.remaining() > 0) {
1068            buf.get(bytes);
1069            value = longBuffer.get();
1070            assertEquals(bytes2long(bytes, buf.order()), value);
1071        }
1072
1073        // test LITTLE_ENDIAN long buffer, read
1074        buf.clear();
1075        buf.order(ByteOrder.LITTLE_ENDIAN);
1076        longBuffer = buf.asLongBuffer();
1077        assertSame(ByteOrder.LITTLE_ENDIAN, longBuffer.order());
1078        while (longBuffer.remaining() > 0) {
1079            buf.get(bytes);
1080            value = longBuffer.get();
1081            assertEquals(bytes2long(bytes, buf.order()), value);
1082        }
1083
1084        if (!buf.isReadOnly()) {
1085            // test BIG_ENDIAN long buffer, write
1086            buf.clear();
1087            buf.order(ByteOrder.BIG_ENDIAN);
1088            longBuffer = buf.asLongBuffer();
1089            assertSame(ByteOrder.BIG_ENDIAN, longBuffer.order());
1090            while (longBuffer.remaining() > 0) {
1091                value = (long) longBuffer.remaining();
1092                longBuffer.put(value);
1093                buf.get(bytes);
1094                assertTrue(Arrays.equals(bytes, long2bytes(value, buf.order())));
1095            }
1096
1097            // test LITTLE_ENDIAN long buffer, write
1098            buf.clear();
1099            buf.order(ByteOrder.LITTLE_ENDIAN);
1100            longBuffer = buf.asLongBuffer();
1101            assertSame(ByteOrder.LITTLE_ENDIAN, longBuffer.order());
1102            while (longBuffer.remaining() > 0) {
1103                value = (long) longBuffer.remaining();
1104                longBuffer.put(value);
1105                buf.get(bytes);
1106                assertTrue(Arrays.equals(bytes, long2bytes(value, buf.order())));
1107            }
1108        }
1109
1110        buf.clear();
1111        buf.order(ByteOrder.BIG_ENDIAN);
1112    }
1113
1114    public void testAsShortBuffer() {
1115        ShortBuffer shortBuffer;
1116        byte bytes[] = new byte[2];
1117        short value;
1118
1119        // test BIG_ENDIAN short buffer, read
1120        buf.clear();
1121        buf.order(ByteOrder.BIG_ENDIAN);
1122        shortBuffer = buf.asShortBuffer();
1123        assertSame(ByteOrder.BIG_ENDIAN, shortBuffer.order());
1124        while (shortBuffer.remaining() > 0) {
1125            buf.get(bytes);
1126            value = shortBuffer.get();
1127            assertEquals(bytes2short(bytes, buf.order()), value);
1128        }
1129
1130        // test LITTLE_ENDIAN short buffer, read
1131        buf.clear();
1132        buf.order(ByteOrder.LITTLE_ENDIAN);
1133        shortBuffer = buf.asShortBuffer();
1134        assertSame(ByteOrder.LITTLE_ENDIAN, shortBuffer.order());
1135        while (shortBuffer.remaining() > 0) {
1136            buf.get(bytes);
1137            value = shortBuffer.get();
1138            assertEquals(bytes2short(bytes, buf.order()), value);
1139        }
1140
1141        if (!buf.isReadOnly()) {
1142            // test BIG_ENDIAN short buffer, write
1143            buf.clear();
1144            buf.order(ByteOrder.BIG_ENDIAN);
1145            shortBuffer = buf.asShortBuffer();
1146            assertSame(ByteOrder.BIG_ENDIAN, shortBuffer.order());
1147            while (shortBuffer.remaining() > 0) {
1148                value = (short) shortBuffer.remaining();
1149                shortBuffer.put(value);
1150                buf.get(bytes);
1151                assertTrue(Arrays.equals(bytes, short2bytes(value, buf.order())));
1152            }
1153
1154            // test LITTLE_ENDIAN short buffer, write
1155            buf.clear();
1156            buf.order(ByteOrder.LITTLE_ENDIAN);
1157            shortBuffer = buf.asShortBuffer();
1158            assertSame(ByteOrder.LITTLE_ENDIAN, shortBuffer.order());
1159            while (shortBuffer.remaining() > 0) {
1160                value = (short) shortBuffer.remaining();
1161                shortBuffer.put(value);
1162                buf.get(bytes);
1163                assertTrue(Arrays.equals(bytes, short2bytes(value, buf.order())));
1164            }
1165        }
1166
1167        buf.clear();
1168        buf.order(ByteOrder.BIG_ENDIAN);
1169    }
1170
1171    public void testGetChar() {
1172        int nbytes = 2;
1173        byte bytes[] = new byte[nbytes];
1174        char value;
1175        buf.clear();
1176        for (int i = 0; buf.remaining() >= nbytes; i++) {
1177            buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1178                    : ByteOrder.LITTLE_ENDIAN);
1179            assertEquals(i * nbytes, buf.position());
1180            buf.mark();
1181            buf.get(bytes);
1182            buf.reset();
1183            value = buf.getChar();
1184            assertEquals(bytes2char(bytes, buf.order()), value);
1185        }
1186
1187        try {
1188            buf.getChar();
1189            fail("Should throw Exception");
1190        } catch (BufferUnderflowException e) {
1191            // expected
1192        }
1193
1194        buf.order(ByteOrder.BIG_ENDIAN);
1195    }
1196
1197    public void testGetCharint() {
1198        int nbytes = 2;
1199        byte bytes[] = new byte[nbytes];
1200        char value;
1201        buf.clear();
1202        for (int i = 0; i <= buf.limit() - nbytes; i++) {
1203            buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1204                    : ByteOrder.LITTLE_ENDIAN);
1205            buf.position(i);
1206            value = buf.getChar(i);
1207            assertEquals(i, buf.position());
1208            buf.get(bytes);
1209            assertEquals(bytes2char(bytes, buf.order()), value);
1210        }
1211
1212        try {
1213            buf.getChar(-1);
1214            fail("Should throw Exception");
1215        } catch (IndexOutOfBoundsException e) {
1216            // expected
1217        }
1218        try {
1219            buf.getChar(buf.limit() - nbytes + 1);
1220            fail("Should throw Exception");
1221        } catch (IndexOutOfBoundsException e) {
1222            // expected
1223        }
1224
1225        buf.order(ByteOrder.BIG_ENDIAN);
1226    }
1227
1228    public void testPutChar() {
1229        if (buf.isReadOnly()) {
1230            try {
1231                buf.clear();
1232                buf.putChar((char) 1);
1233                fail("Should throw Exception");
1234            } catch (ReadOnlyBufferException e) {
1235                // expected
1236            }
1237            return;
1238        }
1239
1240        int nbytes = 2;
1241        byte bytes[] = new byte[nbytes];
1242        char value = 0;
1243        buf.clear();
1244        for (int i = 0; buf.remaining() >= nbytes; i++) {
1245            buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1246                    : ByteOrder.LITTLE_ENDIAN);
1247            value = (char) i;
1248            buf.mark();
1249            buf.putChar(value);
1250            assertEquals((i + 1) * nbytes, buf.position());
1251            buf.reset();
1252            buf.get(bytes);
1253            assertTrue(Arrays.equals(char2bytes(value, buf.order()), bytes));
1254        }
1255
1256        try {
1257            buf.putChar(value);
1258            fail("Should throw Exception");
1259        } catch (BufferOverflowException e) {
1260            // expected
1261        }
1262
1263        buf.order(ByteOrder.BIG_ENDIAN);
1264    }
1265
1266    public void testPutCharint() {
1267        if (buf.isReadOnly()) {
1268            try {
1269                buf.putChar(0, (char) 1);
1270                fail("Should throw Exception");
1271            } catch (ReadOnlyBufferException e) {
1272                // expected
1273            }
1274            return;
1275        }
1276
1277        int nbytes = 2;
1278        byte bytes[] = new byte[nbytes];
1279        char value = 0;
1280        buf.clear();
1281        for (int i = 0; i <= buf.limit() - nbytes; i++) {
1282            buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1283                    : ByteOrder.LITTLE_ENDIAN);
1284            value = (char) i;
1285            buf.position(i);
1286            buf.putChar(i, value);
1287            assertEquals(i, buf.position());
1288            buf.get(bytes);
1289            assertTrue(Arrays.equals(char2bytes(value, buf.order()), bytes));
1290        }
1291
1292        try {
1293            buf.putChar(-1, value);
1294            fail("Should throw Exception");
1295        } catch (IndexOutOfBoundsException e) {
1296            // expected
1297        }
1298        try {
1299            buf.putChar(buf.limit() - nbytes + 1, value);
1300            fail("Should throw Exception");
1301        } catch (IndexOutOfBoundsException e) {
1302            // expected
1303        }
1304
1305        buf.order(ByteOrder.BIG_ENDIAN);
1306
1307        try {
1308        	ByteBuffer.allocateDirect(16).putChar(Integer.MAX_VALUE, 'h');
1309        } catch (IndexOutOfBoundsException e) {
1310        	//expected
1311        }
1312    }
1313
1314    public void testGetDouble() {
1315        int nbytes = 8;
1316        byte bytes[] = new byte[nbytes];
1317        double value;
1318        buf.clear();
1319        for (int i = 0; buf.remaining() >= nbytes; i++) {
1320            buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1321                    : ByteOrder.LITTLE_ENDIAN);
1322            assertEquals(i * nbytes, buf.position());
1323            buf.mark();
1324            buf.get(bytes);
1325            buf.reset();
1326            value = buf.getDouble();
1327            if (!(Double.isNaN(bytes2double(bytes, buf.order())) && Double
1328                    .isNaN(value))) {
1329                assertEquals(bytes2double(bytes, buf.order()), value, 0.00);
1330            }
1331        }
1332
1333        try {
1334            buf.getDouble();
1335            fail("Should throw Exception");
1336        } catch (BufferUnderflowException e) {
1337            // expected
1338        }
1339
1340        buf.order(ByteOrder.BIG_ENDIAN);
1341    }
1342
1343    public void testGetDoubleint() {
1344        int nbytes = 8;
1345        byte bytes[] = new byte[nbytes];
1346        double value;
1347        buf.clear();
1348        for (int i = 0; i <= buf.limit() - nbytes; i++) {
1349            buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1350                    : ByteOrder.LITTLE_ENDIAN);
1351            buf.position(i);
1352            value = buf.getDouble(i);
1353            assertEquals(i, buf.position());
1354            buf.get(bytes);
1355            if (!(Double.isNaN(bytes2double(bytes, buf.order())) && Double
1356                    .isNaN(value))) {
1357                assertEquals(bytes2double(bytes, buf.order()), value, 0.00);
1358            }
1359        }
1360
1361        try {
1362            buf.getDouble(-1);
1363            fail("Should throw Exception");
1364        } catch (IndexOutOfBoundsException e) {
1365            // expected
1366        }
1367        try {
1368            buf.getDouble(buf.limit() - nbytes + 1);
1369            fail("Should throw Exception");
1370        } catch (IndexOutOfBoundsException e) {
1371            // expected
1372        }
1373
1374        buf.order(ByteOrder.BIG_ENDIAN);
1375
1376        try {
1377        	ByteBuffer.allocateDirect(16).getDouble(Integer.MAX_VALUE);
1378        } catch (IndexOutOfBoundsException e) {
1379        	//expected
1380        }
1381    }
1382
1383    public void testPutDouble() {
1384        if (buf.isReadOnly()) {
1385            try {
1386                buf.clear();
1387                buf.putDouble((double) 1);
1388                fail("Should throw Exception");
1389            } catch (ReadOnlyBufferException e) {
1390                // expected
1391            }
1392            return;
1393        }
1394
1395        int nbytes = 8;
1396        byte bytes[] = new byte[nbytes];
1397        double value = 0;
1398        buf.clear();
1399        for (int i = 0; buf.remaining() >= nbytes; i++) {
1400            buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1401                    : ByteOrder.LITTLE_ENDIAN);
1402            value = (double) i;
1403            buf.mark();
1404            buf.putDouble(value);
1405            assertEquals((i + 1) * nbytes, buf.position());
1406            buf.reset();
1407            buf.get(bytes);
1408            assertTrue(Arrays.equals(double2bytes(value, buf.order()), bytes));
1409        }
1410
1411        try {
1412            buf.putDouble(value);
1413            fail("Should throw Exception");
1414        } catch (BufferOverflowException e) {
1415            // expected
1416        }
1417
1418        buf.order(ByteOrder.BIG_ENDIAN);
1419    }
1420
1421    public void testPutDoubleint() {
1422        if (buf.isReadOnly()) {
1423            try {
1424                buf.putDouble(0, (double) 1);
1425                fail("Should throw Exception");
1426            } catch (ReadOnlyBufferException e) {
1427                // expected
1428            }
1429            return;
1430        }
1431
1432        int nbytes = 8;
1433        byte bytes[] = new byte[nbytes];
1434        double value = 0;
1435        buf.clear();
1436        for (int i = 0; i <= buf.limit() - nbytes; i++) {
1437            buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1438                    : ByteOrder.LITTLE_ENDIAN);
1439            value = (double) i;
1440            buf.position(i);
1441            buf.putDouble(i, value);
1442            assertEquals(i, buf.position());
1443            buf.get(bytes);
1444            assertTrue(Arrays.equals(double2bytes(value, buf.order()), bytes));
1445        }
1446
1447        try {
1448            buf.putDouble(-1, value);
1449            fail("Should throw Exception");
1450        } catch (IndexOutOfBoundsException e) {
1451            // expected
1452        }
1453        try {
1454            buf.putDouble(buf.limit() - nbytes + 1, value);
1455            fail("Should throw Exception");
1456        } catch (IndexOutOfBoundsException e) {
1457            // expected
1458        }
1459
1460        buf.order(ByteOrder.BIG_ENDIAN);
1461    }
1462
1463    public void testGetFloat() {
1464        int nbytes = 4;
1465        byte bytes[] = new byte[nbytes];
1466        float value;
1467        buf.clear();
1468        for (int i = 0; buf.remaining() >= nbytes; i++) {
1469            buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1470                    : ByteOrder.LITTLE_ENDIAN);
1471            assertEquals(i * nbytes, buf.position());
1472            buf.mark();
1473            buf.get(bytes);
1474            buf.reset();
1475            value = buf.getFloat();
1476            if (!(Float.isNaN(bytes2float(bytes, buf.order())) && Float
1477                    .isNaN(value))) {
1478                assertEquals(bytes2float(bytes, buf.order()), value, 0.00);
1479            }
1480        }
1481
1482        try {
1483            buf.getFloat();
1484            fail("Should throw Exception");
1485        } catch (BufferUnderflowException e) {
1486            // expected
1487        }
1488
1489        buf.order(ByteOrder.BIG_ENDIAN);
1490    }
1491
1492    public void testGetFloatint() {
1493        int nbytes = 4;
1494        byte bytes[] = new byte[nbytes];
1495        float value;
1496        buf.clear();
1497        for (int i = 0; i <= buf.limit() - nbytes; i++) {
1498            buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1499                    : ByteOrder.LITTLE_ENDIAN);
1500            buf.position(i);
1501            value = buf.getFloat(i);
1502            assertEquals(i, buf.position());
1503            buf.get(bytes);
1504            if (!(Float.isNaN(bytes2float(bytes, buf.order())) && Float
1505                    .isNaN(value))) {
1506                assertEquals(bytes2float(bytes, buf.order()), value, 0.00);
1507            }
1508        }
1509
1510        try {
1511            buf.getFloat(-1);
1512            fail("Should throw Exception");
1513        } catch (IndexOutOfBoundsException e) {
1514            // expected
1515        }
1516        try {
1517            buf.getFloat(buf.limit() - nbytes + 1);
1518            fail("Should throw Exception");
1519        } catch (IndexOutOfBoundsException e) {
1520            // expected
1521        }
1522
1523        buf.order(ByteOrder.BIG_ENDIAN);
1524    }
1525
1526    public void testPutFloat() {
1527        if (buf.isReadOnly()) {
1528            try {
1529                buf.clear();
1530                buf.putFloat((float) 1);
1531                fail("Should throw Exception");
1532            } catch (ReadOnlyBufferException e) {
1533                // expected
1534            }
1535            return;
1536        }
1537
1538        int nbytes = 4;
1539        byte bytes[] = new byte[nbytes];
1540        float value = 0;
1541        buf.clear();
1542        for (int i = 0; buf.remaining() >= nbytes; i++) {
1543            buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1544                    : ByteOrder.LITTLE_ENDIAN);
1545            value = (float) i;
1546            buf.mark();
1547            buf.putFloat(value);
1548            assertEquals((i + 1) * nbytes, buf.position());
1549            buf.reset();
1550            buf.get(bytes);
1551            assertTrue(Arrays.equals(float2bytes(value, buf.order()), bytes));
1552        }
1553
1554        try {
1555            buf.putFloat(value);
1556            fail("Should throw Exception");
1557        } catch (BufferOverflowException e) {
1558            // expected
1559        }
1560
1561        buf.order(ByteOrder.BIG_ENDIAN);
1562    }
1563
1564    public void testPutFloatint() {
1565        if (buf.isReadOnly()) {
1566            try {
1567                buf.putFloat(0, (float) 1);
1568                fail("Should throw Exception");
1569            } catch (ReadOnlyBufferException e) {
1570                // expected
1571            }
1572            return;
1573        }
1574
1575        int nbytes = 4;
1576        byte bytes[] = new byte[nbytes];
1577        float value = 0;
1578        buf.clear();
1579        for (int i = 0; i <= buf.limit() - nbytes; i++) {
1580            buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1581                    : ByteOrder.LITTLE_ENDIAN);
1582            value = (float) i;
1583            buf.position(i);
1584            buf.putFloat(i, value);
1585            assertEquals(i, buf.position());
1586            buf.get(bytes);
1587            assertTrue(Arrays.equals(float2bytes(value, buf.order()), bytes));
1588        }
1589
1590        try {
1591            buf.putFloat(-1, value);
1592            fail("Should throw Exception");
1593        } catch (IndexOutOfBoundsException e) {
1594            // expected
1595        }
1596        try {
1597            buf.putFloat(buf.limit() - nbytes + 1, value);
1598            fail("Should throw Exception");
1599        } catch (IndexOutOfBoundsException e) {
1600            // expected
1601        }
1602
1603        buf.order(ByteOrder.BIG_ENDIAN);
1604    }
1605
1606    public void testGetInt() {
1607        int nbytes = 4;
1608        byte bytes[] = new byte[nbytes];
1609        int value;
1610        buf.clear();
1611        for (int i = 0; buf.remaining() >= nbytes; i++) {
1612            buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1613                    : ByteOrder.LITTLE_ENDIAN);
1614            assertEquals(i * nbytes, buf.position());
1615            buf.mark();
1616            buf.get(bytes);
1617            buf.reset();
1618            value = buf.getInt();
1619            assertEquals(bytes2int(bytes, buf.order()), value);
1620        }
1621
1622        try {
1623            buf.getInt();
1624            fail("Should throw Exception");
1625        } catch (BufferUnderflowException e) {
1626            // expected
1627        }
1628
1629        buf.order(ByteOrder.BIG_ENDIAN);
1630    }
1631
1632    public void testGetIntint() {
1633        int nbytes = 4;
1634        byte bytes[] = new byte[nbytes];
1635        int value;
1636        buf.clear();
1637        for (int i = 0; i <= buf.limit() - nbytes; i++) {
1638            buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1639                    : ByteOrder.LITTLE_ENDIAN);
1640            buf.position(i);
1641            value = buf.getInt(i);
1642            assertEquals(i, buf.position());
1643            buf.get(bytes);
1644            assertEquals(bytes2int(bytes, buf.order()), value);
1645        }
1646
1647        try {
1648            buf.getInt(-1);
1649            fail("Should throw Exception");
1650        } catch (IndexOutOfBoundsException e) {
1651            // expected
1652        }
1653        try {
1654            buf.getInt(buf.limit() - nbytes + 1);
1655            fail("Should throw Exception");
1656        } catch (IndexOutOfBoundsException e) {
1657            // expected
1658        }
1659
1660        buf.order(ByteOrder.BIG_ENDIAN);
1661        try {
1662        	ByteBuffer.allocateDirect(16).getInt(Integer.MAX_VALUE);
1663        } catch (IndexOutOfBoundsException e) {
1664        	//expected
1665        }
1666    }
1667
1668    public void testPutInt() {
1669        if (buf.isReadOnly()) {
1670            try {
1671                buf.clear();
1672                buf.putInt((int) 1);
1673                fail("Should throw Exception");
1674            } catch (ReadOnlyBufferException e) {
1675                // expected
1676            }
1677            return;
1678        }
1679
1680        int nbytes = 4;
1681        byte bytes[] = new byte[nbytes];
1682        int value = 0;
1683        buf.clear();
1684        for (int i = 0; buf.remaining() >= nbytes; i++) {
1685            buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1686                    : ByteOrder.LITTLE_ENDIAN);
1687            value = (int) i;
1688            buf.mark();
1689            buf.putInt(value);
1690            assertEquals((i + 1) * nbytes, buf.position());
1691            buf.reset();
1692            buf.get(bytes);
1693            assertTrue(Arrays.equals(int2bytes(value, buf.order()), bytes));
1694        }
1695
1696        try {
1697            buf.putInt(value);
1698            fail("Should throw Exception");
1699        } catch (BufferOverflowException e) {
1700            // expected
1701        }
1702
1703        buf.order(ByteOrder.BIG_ENDIAN);
1704    }
1705
1706    public void testPutIntint() {
1707        if (buf.isReadOnly()) {
1708            try {
1709                buf.putInt(0, (int) 1);
1710                fail("Should throw Exception");
1711            } catch (ReadOnlyBufferException e) {
1712                // expected
1713            }
1714            return;
1715        }
1716
1717        int nbytes = 4;
1718        byte bytes[] = new byte[nbytes];
1719        int value = 0;
1720        buf.clear();
1721        for (int i = 0; i <= buf.limit() - nbytes; i++) {
1722            buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1723                    : ByteOrder.LITTLE_ENDIAN);
1724            value = (int) i;
1725            buf.position(i);
1726            buf.putInt(i, value);
1727            assertEquals(i, buf.position());
1728            buf.get(bytes);
1729            assertTrue(Arrays.equals(int2bytes(value, buf.order()), bytes));
1730        }
1731
1732        try {
1733            buf.putInt(-1, value);
1734            fail("Should throw Exception");
1735        } catch (IndexOutOfBoundsException e) {
1736            // expected
1737        }
1738        try {
1739            buf.putInt(buf.limit() - nbytes + 1, value);
1740            fail("Should throw Exception");
1741        } catch (IndexOutOfBoundsException e) {
1742            // expected
1743        }
1744
1745        buf.order(ByteOrder.BIG_ENDIAN);
1746    }
1747
1748    public void testGetLong() {
1749        int nbytes = 8;
1750        byte bytes[] = new byte[nbytes];
1751        long value;
1752        buf.clear();
1753        for (int i = 0; buf.remaining() >= nbytes; i++) {
1754            buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1755                    : ByteOrder.LITTLE_ENDIAN);
1756            assertEquals(i * nbytes, buf.position());
1757            buf.mark();
1758            buf.get(bytes);
1759            buf.reset();
1760            value = buf.getLong();
1761            assertEquals(bytes2long(bytes, buf.order()), value);
1762        }
1763
1764        try {
1765            buf.getLong();
1766            fail("Should throw Exception");
1767        } catch (BufferUnderflowException e) {
1768            // expected
1769        }
1770
1771        buf.order(ByteOrder.BIG_ENDIAN);
1772    }
1773
1774    public void testGetLongint() {
1775        int nbytes = 8;
1776        byte bytes[] = new byte[nbytes];
1777        long value;
1778        buf.clear();
1779        for (int i = 0; i <= buf.limit() - nbytes; i++) {
1780            buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1781                    : ByteOrder.LITTLE_ENDIAN);
1782            buf.position(i);
1783            value = buf.getLong(i);
1784            assertEquals(i, buf.position());
1785            buf.get(bytes);
1786            assertEquals(bytes2long(bytes, buf.order()), value);
1787        }
1788
1789        try {
1790            buf.getLong(-1);
1791            fail("Should throw Exception");
1792        } catch (IndexOutOfBoundsException e) {
1793            // expected
1794        }
1795        try {
1796            buf.getLong(buf.limit() - nbytes + 1);
1797            fail("Should throw Exception");
1798        } catch (IndexOutOfBoundsException e) {
1799            // expected
1800        }
1801
1802        buf.order(ByteOrder.BIG_ENDIAN);
1803    }
1804
1805    public void testPutLong() {
1806        if (buf.isReadOnly()) {
1807            try {
1808                buf.clear();
1809                buf.putLong((long) 1);
1810                fail("Should throw Exception");
1811            } catch (ReadOnlyBufferException e) {
1812                // expected
1813            }
1814            return;
1815        }
1816
1817        int nbytes = 8;
1818        byte bytes[] = new byte[nbytes];
1819        long value = 0;
1820        buf.clear();
1821        for (int i = 0; buf.remaining() >= nbytes; i++) {
1822            buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1823                    : ByteOrder.LITTLE_ENDIAN);
1824            value = (long) i;
1825            buf.mark();
1826            buf.putLong(value);
1827            assertEquals((i + 1) * nbytes, buf.position());
1828            buf.reset();
1829            buf.get(bytes);
1830            assertTrue(Arrays.equals(long2bytes(value, buf.order()), bytes));
1831        }
1832
1833        try {
1834            buf.putLong(value);
1835            fail("Should throw Exception");
1836        } catch (BufferOverflowException e) {
1837            // expected
1838        }
1839
1840        buf.order(ByteOrder.BIG_ENDIAN);
1841    }
1842
1843    public void testPutLongint() {
1844        if (buf.isReadOnly()) {
1845            try {
1846                buf.putLong(0, (long) 1);
1847                fail("Should throw Exception");
1848            } catch (ReadOnlyBufferException e) {
1849                // expected
1850            }
1851            return;
1852        }
1853
1854        int nbytes = 8;
1855        byte bytes[] = new byte[nbytes];
1856        long value = 0;
1857        buf.clear();
1858        for (int i = 0; i <= buf.limit() - nbytes; i++) {
1859            buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1860                    : ByteOrder.LITTLE_ENDIAN);
1861            value = (long) i;
1862            buf.position(i);
1863            buf.putLong(i, value);
1864            assertEquals(i, buf.position());
1865            buf.get(bytes);
1866            assertTrue(Arrays.equals(long2bytes(value, buf.order()), bytes));
1867        }
1868
1869        try {
1870            buf.putLong(-1, value);
1871            fail("Should throw Exception");
1872        } catch (IndexOutOfBoundsException e) {
1873            // expected
1874        }
1875        try {
1876            buf.putLong(buf.limit() - nbytes + 1, value);
1877            fail("Should throw Exception");
1878        } catch (IndexOutOfBoundsException e) {
1879            // expected
1880        }
1881
1882        buf.order(ByteOrder.BIG_ENDIAN);
1883    }
1884
1885    public void testGetShort() {
1886        int nbytes = 2;
1887        byte bytes[] = new byte[nbytes];
1888        short value;
1889        buf.clear();
1890        for (int i = 0; buf.remaining() >= nbytes; i++) {
1891            buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1892                    : ByteOrder.LITTLE_ENDIAN);
1893            assertEquals(i * nbytes, buf.position());
1894            buf.mark();
1895            buf.get(bytes);
1896            buf.reset();
1897            value = buf.getShort();
1898            assertEquals(bytes2short(bytes, buf.order()), value);
1899        }
1900
1901        try {
1902            buf.getShort();
1903            fail("Should throw Exception");
1904        } catch (BufferUnderflowException e) {
1905            // expected
1906        }
1907
1908        buf.order(ByteOrder.BIG_ENDIAN);
1909    }
1910
1911    public void testGetShortint() {
1912        int nbytes = 2;
1913        byte bytes[] = new byte[nbytes];
1914        short value;
1915        buf.clear();
1916        for (int i = 0; i <= buf.limit() - nbytes; i++) {
1917            buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1918                    : ByteOrder.LITTLE_ENDIAN);
1919            buf.position(i);
1920            value = buf.getShort(i);
1921            assertEquals(i, buf.position());
1922            buf.get(bytes);
1923            assertEquals(bytes2short(bytes, buf.order()), value);
1924        }
1925
1926        try {
1927            buf.getShort(-1);
1928            fail("Should throw Exception");
1929        } catch (IndexOutOfBoundsException e) {
1930            // expected
1931        }
1932        try {
1933            buf.getShort(buf.limit() - nbytes + 1);
1934            fail("Should throw Exception");
1935        } catch (IndexOutOfBoundsException e) {
1936            // expected
1937        }
1938
1939        buf.order(ByteOrder.BIG_ENDIAN);
1940    }
1941
1942    public void testPutShort() {
1943        if (buf.isReadOnly()) {
1944            try {
1945                buf.clear();
1946                buf.putShort((short) 1);
1947                fail("Should throw Exception");
1948            } catch (ReadOnlyBufferException e) {
1949                // expected
1950            }
1951            return;
1952        }
1953
1954        int nbytes = 2;
1955        byte bytes[] = new byte[nbytes];
1956        short value = 0;
1957        buf.clear();
1958        for (int i = 0; buf.remaining() >= nbytes; i++) {
1959            buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1960                    : ByteOrder.LITTLE_ENDIAN);
1961            value = (short) i;
1962            buf.mark();
1963            buf.putShort(value);
1964            assertEquals((i + 1) * nbytes, buf.position());
1965            buf.reset();
1966            buf.get(bytes);
1967            assertTrue(Arrays.equals(short2bytes(value, buf.order()), bytes));
1968        }
1969
1970        try {
1971            buf.putShort(value);
1972            fail("Should throw Exception");
1973        } catch (BufferOverflowException e) {
1974            // expected
1975        }
1976
1977        buf.order(ByteOrder.BIG_ENDIAN);
1978    }
1979
1980    public void testPutShortint() {
1981        if (buf.isReadOnly()) {
1982            try {
1983                buf.putShort(0, (short) 1);
1984                fail("Should throw Exception");
1985            } catch (ReadOnlyBufferException e) {
1986                // expected
1987            }
1988            return;
1989        }
1990
1991        int nbytes = 2;
1992        byte bytes[] = new byte[nbytes];
1993        short value = 0;
1994        buf.clear();
1995        for (int i = 0; i <= buf.limit() - nbytes; i++) {
1996            buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1997                    : ByteOrder.LITTLE_ENDIAN);
1998            value = (short) i;
1999            buf.position(i);
2000            buf.putShort(i, value);
2001            assertEquals(i, buf.position());
2002            buf.get(bytes);
2003            assertTrue(Arrays.equals(short2bytes(value, buf.order()), bytes));
2004        }
2005
2006        try {
2007            buf.putShort(-1, value);
2008            fail("Should throw Exception");
2009        } catch (IndexOutOfBoundsException e) {
2010            // expected
2011        }
2012        try {
2013            buf.putShort(buf.limit() - nbytes + 1, value);
2014            fail("Should throw Exception");
2015        } catch (IndexOutOfBoundsException e) {
2016            // expected
2017        }
2018
2019        buf.order(ByteOrder.BIG_ENDIAN);
2020    }
2021
2022    /**
2023     * @tests java.nio.ByteBuffer.wrap(byte[],int,int)
2024     */
2025    public void testWrappedByteBuffer_null_array() {
2026        // Regression for HARMONY-264
2027        byte array[] = null;
2028        try {
2029            ByteBuffer.wrap(array, -1, 0);
2030            fail("Should throw NPE");
2031        } catch (NullPointerException e) {
2032        }
2033        try {
2034            ByteBuffer.wrap(new byte[10], Integer.MAX_VALUE, 2);
2035            fail("Should throw IndexOutOfBoundsException");
2036        } catch (IndexOutOfBoundsException e) {
2037        }
2038    }
2039
2040    // http://b/34045479
2041    public void testMappedByteBuffer_Put_ReadOnlyHeapByteBuffer() throws Exception {
2042        // Create a temp file
2043        byte[] data = new byte[] {1, 2, 3, 4};
2044        Path tempFile = Files.createTempFile("mmap", "test");
2045        Files.write(tempFile, data);
2046
2047        // Create a read-only heap buffer
2048        ByteBuffer readOnlySource = ByteBuffer.allocate(4).asReadOnlyBuffer();
2049        try (RandomAccessFile tempRAF = new RandomAccessFile(tempFile.toFile(), "rw")) {
2050            FileChannel tempFileChannel = tempRAF.getChannel();
2051            ByteBuffer mappedByteBuffer =
2052                tempFileChannel.map(FileChannel.MapMode.READ_WRITE, 0, tempFileChannel.size());
2053
2054            // Try to put a non-empty, read-only heap byte buffer into a mapped byte buffer.
2055            mappedByteBuffer.put(readOnlySource);
2056            tempFileChannel.close();
2057        } finally {
2058            Files.delete(tempFile);
2059        }
2060    }
2061
2062    private void loadTestData1(byte array[], int offset, int length) {
2063        for (int i = 0; i < length; i++) {
2064            array[offset + i] = (byte) i;
2065        }
2066    }
2067
2068    private void loadTestData2(byte array[], int offset, int length) {
2069        for (int i = 0; i < length; i++) {
2070            array[offset + i] = (byte) (length - i);
2071        }
2072    }
2073
2074    private void loadTestData1(ByteBuffer buf) {
2075        buf.clear();
2076        for (int i = 0; i < buf.capacity(); i++) {
2077            buf.put(i, (byte) i);
2078        }
2079    }
2080
2081    private void loadTestData2(ByteBuffer buf) {
2082        buf.clear();
2083        for (int i = 0; i < buf.capacity(); i++) {
2084            buf.put(i, (byte) (buf.capacity() - i));
2085        }
2086    }
2087
2088    private void assertContentEquals(ByteBuffer buf, byte array[],
2089            int offset, int length) {
2090        for (int i = 0; i < length; i++) {
2091            assertEquals(buf.get(i), array[offset + i]);
2092        }
2093    }
2094
2095    private void assertContentEquals(ByteBuffer buf, ByteBuffer other) {
2096        assertEquals(buf.capacity(), other.capacity());
2097        for (int i = 0; i < buf.capacity(); i++) {
2098            assertEquals(buf.get(i), other.get(i));
2099        }
2100    }
2101
2102    private void assertContentLikeTestData1(ByteBuffer buf,
2103            int startIndex, byte startValue, int length) {
2104        byte value = startValue;
2105        for (int i = 0; i < length; i++) {
2106            assertEquals(buf.get(startIndex + i), value);
2107            value = (byte) (value + 1);
2108        }
2109    }
2110
2111    private int bytes2int(byte bytes[], ByteOrder order) {
2112        int nbytes = 4, bigHead, step;
2113        if (order == ByteOrder.BIG_ENDIAN) {
2114            bigHead = 0;
2115            step = 1;
2116        } else {
2117            bigHead = nbytes - 1;
2118            step = -1;
2119        }
2120        int result = 0;
2121        int p = bigHead;
2122        for (int i = 0; i < nbytes; i++) {
2123            result = result << 8;
2124            result = result | (bytes[p] & 0xff);
2125            p += step;
2126        }
2127        return result;
2128    }
2129
2130    private long bytes2long(byte bytes[], ByteOrder order) {
2131        int nbytes = 8, bigHead, step;
2132        if (order == ByteOrder.BIG_ENDIAN) {
2133            bigHead = 0;
2134            step = 1;
2135        } else {
2136            bigHead = nbytes - 1;
2137            step = -1;
2138        }
2139        long result = 0;
2140        int p = bigHead;
2141        for (int i = 0; i < nbytes; i++) {
2142            result = result << 8;
2143            result = result | (bytes[p] & 0xff);
2144            p += step;
2145        }
2146        return result;
2147    }
2148
2149    private short bytes2short(byte bytes[], ByteOrder order) {
2150        int nbytes = 2, bigHead, step;
2151        if (order == ByteOrder.BIG_ENDIAN) {
2152            bigHead = 0;
2153            step = 1;
2154        } else {
2155            bigHead = nbytes - 1;
2156            step = -1;
2157        }
2158        short result = 0;
2159        int p = bigHead;
2160        for (int i = 0; i < nbytes; i++) {
2161            result = (short) (result << 8);
2162            result = (short) (result | (bytes[p] & 0xff));
2163            p += step;
2164        }
2165        return result;
2166    }
2167
2168    private char bytes2char(byte bytes[], ByteOrder order) {
2169        return (char) bytes2short(bytes, order);
2170    }
2171
2172    private float bytes2float(byte bytes[], ByteOrder order) {
2173        return Float.intBitsToFloat(bytes2int(bytes, order));
2174    }
2175
2176    private double bytes2double(byte bytes[], ByteOrder order) {
2177        return Double.longBitsToDouble(bytes2long(bytes, order));
2178    }
2179
2180    private byte[] int2bytes(int value, ByteOrder order) {
2181        int nbytes = 4, smallHead, step;
2182        if (order == ByteOrder.BIG_ENDIAN) {
2183            smallHead = nbytes - 1;
2184            step = -1;
2185        } else {
2186            smallHead = 0;
2187            step = 1;
2188        }
2189        byte bytes[] = new byte[nbytes];
2190        int p = smallHead;
2191        for (int i = 0; i < nbytes; i++) {
2192            bytes[p] = (byte) (value & 0xff);
2193            value = value >> 8;
2194            p += step;
2195        }
2196        return bytes;
2197    }
2198
2199    private byte[] long2bytes(long value, ByteOrder order) {
2200        int nbytes = 8, smallHead, step;
2201        if (order == ByteOrder.BIG_ENDIAN) {
2202            smallHead = nbytes - 1;
2203            step = -1;
2204        } else {
2205            smallHead = 0;
2206            step = 1;
2207        }
2208        byte bytes[] = new byte[nbytes];
2209        int p = smallHead;
2210        for (int i = 0; i < nbytes; i++) {
2211            bytes[p] = (byte) (value & 0xff);
2212            value = value >> 8;
2213            p += step;
2214        }
2215        return bytes;
2216    }
2217
2218    private byte[] short2bytes(short value, ByteOrder order) {
2219        int nbytes = 2, smallHead, step;
2220        if (order == ByteOrder.BIG_ENDIAN) {
2221            smallHead = nbytes - 1;
2222            step = -1;
2223        } else {
2224            smallHead = 0;
2225            step = 1;
2226        }
2227        byte bytes[] = new byte[nbytes];
2228        int p = smallHead;
2229        for (int i = 0; i < nbytes; i++) {
2230            bytes[p] = (byte) (value & 0xff);
2231            value = (short) (value >> 8);
2232            p += step;
2233        }
2234        return bytes;
2235    }
2236
2237    private byte[] char2bytes(char value, ByteOrder order) {
2238        return short2bytes((short) value, order);
2239    }
2240
2241    private byte[] float2bytes(float value, ByteOrder order) {
2242        return int2bytes(Float.floatToRawIntBits(value), order);
2243    }
2244
2245    private byte[] double2bytes(double value, ByteOrder order) {
2246        return long2bytes(Double.doubleToRawLongBits(value), order);
2247    }
2248}
2249