1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package libcore.java.nio;
18 
19 import java.nio.Buffer;
20 import java.nio.BufferOverflowException;
21 import java.nio.BufferUnderflowException;
22 import java.nio.ByteBuffer;
23 import java.nio.ByteOrder;
24 import java.nio.FloatBuffer;
25 import java.nio.IntBuffer;
26 import java.nio.ShortBuffer;
27 import junit.framework.TestCase;
28 
29 /**
30  * Tests for some buffers from the java.nio package.
31  */
32 public class OldAndroidNIOTest extends TestCase {
33 
checkBuffer(Buffer b)34     void checkBuffer(Buffer b) {
35         assertTrue(0 <= b.position());
36         assertTrue(b.position() <= b.limit());
37         assertTrue(b.limit() <= b.capacity());
38     }
39 
testNIO_byte_array()40     public void testNIO_byte_array() throws Exception {
41         // Test byte array-based buffer
42         byteBufferTest(ByteBuffer.allocate(12));
43     }
44 
testNIO_direct()45     public void testNIO_direct() throws Exception {
46         // Test native heap-allocated buffer
47         byteBufferTest(ByteBuffer.allocateDirect(12));
48     }
49 
testNIO_short_array()50     public void testNIO_short_array() throws Exception {
51         // Test short array-based buffer
52         short[] shortArray = new short[8];
53         ShortBuffer sb = ShortBuffer.wrap(shortArray);
54         shortBufferTest(sb);
55     }
56 
testNIO_int_array()57     public void testNIO_int_array() throws Exception {
58         // Test int array-based buffer
59         int[] intArray = new int[8];
60         IntBuffer ib = IntBuffer.wrap(intArray);
61         intBufferTest(ib);
62     }
63 
testNIO_float_array()64     public void testNIO_float_array() throws Exception {
65         // Test float array-based buffer
66         float[] floatArray = new float[8];
67         FloatBuffer fb = FloatBuffer.wrap(floatArray);
68         floatBufferTest(fb);
69     }
70 
byteBufferTest(ByteBuffer b)71     private void byteBufferTest(ByteBuffer b) {
72         checkBuffer(b);
73 
74         // Duplicate buffers revert to big-endian.
75         b.order(ByteOrder.LITTLE_ENDIAN);
76         ByteBuffer dupe = b.duplicate();
77         assertEquals(ByteOrder.BIG_ENDIAN, dupe.order());
78         b.order(ByteOrder.BIG_ENDIAN);
79 
80         // Bounds checks
81         try {
82             b.put(-1, (byte) 0);
83             fail("expected exception not thrown");
84         } catch (IndexOutOfBoundsException e) {
85             // expected
86         }
87 
88         try {
89             b.put(b.limit(), (byte) 0);
90             fail("expected exception not thrown");
91         } catch (IndexOutOfBoundsException e) {
92             // expected
93         }
94 
95         // IndexOutOfBoundsException: offset < 0
96         try {
97             byte[] data = new byte[8];
98             b.position(0);
99             b.put(data, -1, 2);
100             fail("expected exception not thrown");
101         } catch (IndexOutOfBoundsException e) {
102             // expected
103         }
104 
105         // IndexOutOfBoundsException: length > array.length - offset
106         try {
107             byte[] data = new byte[8];
108             b.position(0);
109             b.put(data, 1, 8);
110             fail("expected exception not thrown");
111         } catch (IndexOutOfBoundsException e) {
112             // expected
113         }
114 
115         // BufferOverflowException: length > remaining()
116         try {
117             byte[] data = new byte[8];
118             b.position(b.limit() - 2);
119             b.put(data, 0, 3);
120             fail("expected exception not thrown");
121         } catch (BufferOverflowException e) {
122             // expected
123         }
124 
125         // Fill buffer with bytes A0 A1 A2 A3 ...
126         b.position(0);
127         for (int i = 0; i < b.capacity(); i++) {
128             b.put((byte) (0xA0 + i));
129         }
130         try {
131             b.put((byte) 0xFF);
132             fail("expected exception not thrown");
133         } catch (BufferOverflowException e) {
134             // expected
135         }
136 
137         b.position(0);
138         assertEquals((byte) 0xA7, b.get(7));
139         try {
140             b.get(12);
141             fail("expected exception not thrown");
142         } catch (IndexOutOfBoundsException e) {
143             // expected
144         }
145         try {
146             b.get(-10);
147             fail("expected exception not thrown");
148         } catch (IndexOutOfBoundsException e) {
149             // expected
150         }
151 
152         b.position(0);
153         b.order(ByteOrder.LITTLE_ENDIAN);
154         assertEquals((byte) 0xA0, b.get());
155         assertEquals((byte) 0xA1, b.get());
156         assertEquals((byte) 0xA2, b.get());
157         assertEquals((byte) 0xA3, b.get());
158         assertEquals((byte) 0xA4, b.get());
159         assertEquals((byte) 0xA5, b.get());
160         assertEquals((byte) 0xA6, b.get());
161         assertEquals((byte) 0xA7, b.get());
162         assertEquals((byte) 0xA8, b.get());
163         assertEquals((byte) 0xA9, b.get());
164         assertEquals((byte) 0xAA, b.get());
165         assertEquals((byte) 0xAB, b.get());
166         try {
167             b.get();
168             fail("expected exception not thrown");
169         } catch (BufferUnderflowException e) {
170             // expected
171         }
172 
173         b.position(0);
174         b.order(ByteOrder.BIG_ENDIAN);
175         assertEquals((byte) 0xA0, b.get());
176         assertEquals((byte) 0xA1, b.get());
177         assertEquals((byte) 0xA2, b.get());
178         assertEquals((byte) 0xA3, b.get());
179         assertEquals((byte) 0xA4, b.get());
180         assertEquals((byte) 0xA5, b.get());
181         assertEquals((byte) 0xA6, b.get());
182         assertEquals((byte) 0xA7, b.get());
183         assertEquals((byte) 0xA8, b.get());
184         assertEquals((byte) 0xA9, b.get());
185         assertEquals((byte) 0xAA, b.get());
186         assertEquals((byte) 0xAB, b.get());
187         try {
188             b.get();
189             fail("expected exception not thrown");
190         } catch (BufferUnderflowException e) {
191             // expected
192         }
193 
194         b.position(6);
195         b.limit(10);
196         assertEquals((byte) 0xA6, b.get());
197 
198         // Check sliced buffer
199         b.position(6);
200 
201         ByteBuffer bb = b.slice();
202         checkBuffer(bb);
203 
204         assertEquals(0, bb.position());
205         assertEquals(4, bb.limit());
206         assertEquals(4, bb.capacity());
207 
208         assertEquals((byte) 0xA6, bb.get());
209         assertEquals((byte) 0xA7, bb.get());
210         assertEquals((byte) 0xA8, bb.get());
211         assertEquals((byte) 0xA9, bb.get());
212         try {
213             bb.get();
214             fail("expected exception not thrown");
215         } catch (BufferUnderflowException e) {
216             // expected
217         }
218 
219         // Reset position and limit
220         b.position(0);
221         b.limit(b.capacity());
222 
223         // Check 'getShort'
224         b.order(ByteOrder.LITTLE_ENDIAN);
225         b.position(0);
226         assertEquals((short) 0xA1A0, b.getShort());
227         assertEquals((short) 0xA3A2, b.getShort());
228         assertEquals((short) 0xA5A4, b.getShort());
229         assertEquals((short) 0xA7A6, b.getShort());
230         assertEquals((short) 0xA9A8, b.getShort());
231         assertEquals((short) 0xABAA, b.getShort());
232         try {
233             bb.getShort();
234             fail("expected exception not thrown");
235         } catch (BufferUnderflowException e) {
236             // expected
237         }
238 
239         b.order(ByteOrder.BIG_ENDIAN);
240         b.position(0);
241         assertEquals((short) 0xA0A1, b.getShort());
242         assertEquals((short) 0xA2A3, b.getShort());
243         assertEquals((short) 0xA4A5, b.getShort());
244         assertEquals((short) 0xA6A7, b.getShort());
245         assertEquals((short) 0xA8A9, b.getShort());
246         assertEquals((short) 0xAAAB, b.getShort());
247         try {
248             bb.getShort();
249            fail("expected exception not thrown");
250         } catch (BufferUnderflowException e) {
251             // expected
252         }
253 
254         // Check 'getInt'
255         b.order(ByteOrder.LITTLE_ENDIAN);
256         b.position(0);
257         assertEquals(0xA3A2A1A0, b.getInt());
258         assertEquals(0xA7A6A5A4, b.getInt());
259         assertEquals(0xABAAA9A8, b.getInt());
260         try {
261             bb.getInt();
262             fail("expected exception not thrown");
263         } catch (BufferUnderflowException e) {
264             // expected
265         }
266 
267         b.order(ByteOrder.BIG_ENDIAN);
268         b.position(0);
269         assertEquals(0xA0A1A2A3, b.getInt());
270         assertEquals(0xA4A5A6A7, b.getInt());
271         assertEquals(0xA8A9AAAB, b.getInt());
272         try {
273             bb.getInt();
274             fail("expected exception not thrown");
275         } catch (BufferUnderflowException e) {
276             // expected
277         }
278 
279         // Check 'getFloat'
280         b.order(ByteOrder.LITTLE_ENDIAN);
281         b.position(0);
282         assertEquals(0xA3A2A1A0, Float.floatToRawIntBits(b.getFloat()));
283         assertEquals(0xA7A6A5A4, Float.floatToRawIntBits(b.getFloat()));
284         assertEquals(0xABAAA9A8, Float.floatToRawIntBits(b.getFloat()));
285         try {
286             b.getFloat();
287             fail("expected exception not thrown");
288         } catch (BufferUnderflowException e) {
289             // expected
290         }
291 
292         b.order(ByteOrder.BIG_ENDIAN);
293         b.position(0);
294         assertEquals(0xA0A1A2A3, Float.floatToRawIntBits(b.getFloat()));
295         assertEquals(0xA4A5A6A7, Float.floatToRawIntBits(b.getFloat()));
296         assertEquals(0xA8A9AAAB, Float.floatToRawIntBits(b.getFloat()));
297         try {
298             b.getFloat();
299             fail("expected exception not thrown");
300         } catch (BufferUnderflowException e) {
301             // expected
302         }
303 
304         // Check 'getDouble(int position)'
305         b.order(ByteOrder.LITTLE_ENDIAN);
306         assertEquals(0xA7A6A5A4A3A2A1A0L, Double.doubleToRawLongBits(b.getDouble(0)));
307         assertEquals(0xA8A7A6A5A4A3A2A1L, Double.doubleToRawLongBits(b.getDouble(1)));
308         try {
309             b.getDouble(-1);
310             fail("expected exception not thrown");
311         } catch (IndexOutOfBoundsException e) {
312             // expected
313         }
314         try {
315             b.getDouble(5);
316             fail("expected exception not thrown");
317         } catch (IndexOutOfBoundsException e) {
318             // expected
319         }
320 
321         b.order(ByteOrder.BIG_ENDIAN);
322         assertEquals(0xA0A1A2A3A4A5A6A7L, Double.doubleToRawLongBits(b.getDouble(0)));
323         assertEquals(0xA1A2A3A4A5A6A7A8L, Double.doubleToRawLongBits(b.getDouble(1)));
324         try {
325             b.getDouble(-1);
326             fail("expected exception not thrown");
327         } catch (IndexOutOfBoundsException e) {
328             // expected
329         }
330         try {
331             b.getDouble(5);
332             fail("expected exception not thrown");
333         } catch (IndexOutOfBoundsException e) {
334             // expected
335         }
336 
337         // Slice and check 'getInt'
338         b.position(1);
339         b.limit(5);
340         b.order(ByteOrder.LITTLE_ENDIAN);
341         bb = b.slice();
342         assertEquals(4, bb.capacity());
343         assertEquals(ByteOrder.BIG_ENDIAN, bb.order());
344         assertEquals(0xA1A2A3A4, bb.getInt(0));
345         bb.order(ByteOrder.LITTLE_ENDIAN);
346         assertEquals(0xA4A3A2A1, bb.getInt(0));
347 
348         bb.order(ByteOrder.LITTLE_ENDIAN);
349         ShortBuffer sb = bb.asShortBuffer();
350 
351         checkBuffer(sb);
352         assertEquals(2, sb.capacity());
353         assertEquals((short) 0xA2A1, sb.get());
354         assertEquals((short) 0xA4A3, sb.get());
355 
356         bb.order(ByteOrder.BIG_ENDIAN);
357         sb = bb.asShortBuffer();
358 
359         checkBuffer(sb);
360         assertEquals(2, sb.capacity());
361         assertEquals((short) 0xA1A2, sb.get());
362         assertEquals((short) 0xA3A4, sb.get());
363 
364         bb.order(ByteOrder.LITTLE_ENDIAN);
365         IntBuffer ib = bb.asIntBuffer();
366 
367         checkBuffer(ib);
368         assertEquals(1, ib.capacity());
369         assertEquals(0xA4A3A2A1, ib.get());
370 
371         bb.order(ByteOrder.BIG_ENDIAN);
372         ib = bb.asIntBuffer();
373 
374         checkBuffer(ib);
375         assertEquals(1, ib.capacity());
376         assertEquals(0xA1A2A3A4, ib.get());
377 
378         bb.order(ByteOrder.LITTLE_ENDIAN);
379         FloatBuffer fb = bb.asFloatBuffer();
380 
381         checkBuffer(fb);
382         assertEquals(1, fb.capacity());
383         assertEquals(0xA4A3A2A1, Float.floatToRawIntBits(fb.get()));
384 
385         bb.order(ByteOrder.BIG_ENDIAN);
386         fb = bb.asFloatBuffer();
387 
388         checkBuffer(fb);
389         assertEquals(1, fb.capacity());
390         assertEquals(0xA1A2A3A4, Float.floatToRawIntBits(fb.get()));
391     }
392 
shortBufferTest(ShortBuffer sb)393     private void shortBufferTest(ShortBuffer sb) {
394         checkBuffer(sb);
395 
396         try {
397             sb.put(-1, (short) 0);
398             fail("expected exception not thrown");
399         } catch (IndexOutOfBoundsException e) {
400             // expected
401         }
402 
403         try {
404             sb.put(sb.limit(), (short) 0);
405             fail("expected exception not thrown");
406         } catch (IndexOutOfBoundsException e) {
407             // expected
408         }
409 
410         // IndexOutOfBoundsException: offset < 0
411         try {
412             short[] data = new short[8];
413             sb.position(0);
414             sb.put(data, -1, 2);
415             fail("expected exception not thrown");
416         } catch (IndexOutOfBoundsException e) {
417             // expected
418         }
419 
420         // IndexOutOfBoundsException: length > array.length - offset
421         try {
422             short[] data = new short[8];
423             sb.position(0);
424             sb.put(data, 1, 8);
425             fail("expected exception not thrown");
426         } catch (IndexOutOfBoundsException e) {
427             // expected
428         }
429 
430         // BufferOverflowException: length > remaining()
431         try {
432             short[] data = new short[8];
433             sb.position(sb.limit() - 2);
434             sb.put(data, 0, 3);
435             fail("expected exception not thrown");
436         } catch (BufferOverflowException e) {
437             // expected
438         }
439 
440         short[] data = {0, 10, 20, 30, 40, 50, 60, 70};
441         sb.position(0);
442         sb.put(data);
443 
444         try {
445             sb.get();
446             fail("expected exception not thrown");
447         } catch (BufferUnderflowException e) {
448             // expected
449         }
450 
451         sb.position(0);
452         assertEquals((short) 0, sb.get());
453         assertEquals((short) 10, sb.get());
454         assertEquals((short) 20, sb.get());
455         assertEquals((short) 30, sb.get());
456         assertEquals((short) 40, sb.get());
457         assertEquals((short) 50, sb.get());
458         assertEquals((short) 60, sb.get());
459         assertEquals((short) 70, sb.get());
460         try {
461             sb.get();
462             fail("expected exception not thrown");
463         } catch (BufferUnderflowException e) {
464             // expected
465         }
466         sb.position(1);
467         sb.put((short) 11);
468         assertEquals((short) 11, sb.get(1));
469 
470         short[] ss1 = {33, 44, 55, 66};
471         sb.position(3);
472         sb.put(ss1);
473         sb.position(0);
474         assertEquals((short) 0, sb.get());
475         assertEquals((short) 11, sb.get());
476         assertEquals((short) 20, sb.get());
477         assertEquals((short) 33, sb.get());
478         assertEquals((short) 44, sb.get());
479         assertEquals((short) 55, sb.get());
480         assertEquals((short) 66, sb.get());
481         assertEquals((short) 70, sb.get());
482 
483         short[] ss2 = {10, 22, 30};
484         sb.position(2);
485         sb.put(ss2, 1, 1);
486         sb.position(0);
487         assertEquals((short) 0, sb.get());
488         assertEquals((short) 11, sb.get());
489         assertEquals((short) 22, sb.get());
490         assertEquals((short) 33, sb.get());
491         assertEquals((short) 44, sb.get());
492         assertEquals((short) 55, sb.get());
493         assertEquals((short) 66, sb.get());
494         assertEquals((short) 70, sb.get());
495     }
496 
intBufferTest(IntBuffer ib)497     private void intBufferTest(IntBuffer ib) {
498         checkBuffer(ib);
499 
500         try {
501             ib.put(-1, (int) 0);
502             fail("expected exception not thrown");
503         } catch (IndexOutOfBoundsException e) {
504             // expected
505         }
506 
507         try {
508             ib.put(ib.limit(), (int) 0);
509             fail("expected exception not thrown");
510         } catch (IndexOutOfBoundsException e) {
511             // expected
512         }
513 
514         // IndexOutOfBoundsException: offset < 0
515         try {
516             int[] data = new int[8];
517             ib.position(0);
518             ib.put(data, -1, 2);
519             fail("expected exception not thrown");
520         } catch (IndexOutOfBoundsException e) {
521             // expected
522         }
523 
524         // IndexOutOfBoundsException: length > array.length - offset
525         try {
526             int[] data = new int[8];
527             ib.position(0);
528             ib.put(data, 1, 8);
529             fail("expected exception not thrown");
530         } catch (IndexOutOfBoundsException e) {
531             // expected
532         }
533 
534         // BufferOverflowException: length > remaining()
535         try {
536             int[] data = new int[8];
537             ib.position(ib.limit() - 2);
538             ib.put(data, 0, 3);
539             fail("expected exception not thrown");
540         } catch (BufferOverflowException e) {
541             // expected
542         }
543 
544         int[] data = {0, 10, 20, 30, 40, 50, 60, 70};
545         ib.position(0);
546         ib.put(data);
547 
548         try {
549             ib.get();
550             fail("expected exception not thrown");
551         } catch (BufferUnderflowException e) {
552             // expected
553         }
554 
555         ib.position(0);
556         assertEquals((int) 0, ib.get());
557         assertEquals((int) 10, ib.get());
558         assertEquals((int) 20, ib.get());
559         assertEquals((int) 30, ib.get());
560         assertEquals((int) 40, ib.get());
561         assertEquals((int) 50, ib.get());
562         assertEquals((int) 60, ib.get());
563         assertEquals((int) 70, ib.get());
564         try {
565             ib.get();
566             fail("expected exception not thrown");
567         } catch (BufferUnderflowException e) {
568             // expected
569         }
570         ib.position(1);
571         ib.put((int) 11);
572         assertEquals((int) 11, ib.get(1));
573 
574         int[] ss1 = {33, 44, 55, 66};
575         ib.position(3);
576         ib.put(ss1);
577         ib.position(0);
578         assertEquals((int) 0, ib.get());
579         assertEquals((int) 11, ib.get());
580         assertEquals((int) 20, ib.get());
581         assertEquals((int) 33, ib.get());
582         assertEquals((int) 44, ib.get());
583         assertEquals((int) 55, ib.get());
584         assertEquals((int) 66, ib.get());
585         assertEquals((int) 70, ib.get());
586 
587         int[] ss2 = {10, 22, 30};
588         ib.position(2);
589         ib.put(ss2, 1, 1);
590         ib.position(0);
591         assertEquals((int) 0, ib.get());
592         assertEquals((int) 11, ib.get());
593         assertEquals((int) 22, ib.get());
594         assertEquals((int) 33, ib.get());
595         assertEquals((int) 44, ib.get());
596         assertEquals((int) 55, ib.get());
597         assertEquals((int) 66, ib.get());
598         assertEquals((int) 70, ib.get());
599     }
600 
floatBufferTest(FloatBuffer fb)601     void floatBufferTest(FloatBuffer fb) {
602         checkBuffer(fb);
603 
604         try {
605             fb.put(-1, (float) 0);
606             fail("expected exception not thrown");
607         } catch (IndexOutOfBoundsException e) {
608             // expected
609         }
610 
611         try {
612             fb.put(fb.limit(), (float) 0);
613             fail("expected exception not thrown");
614         } catch (IndexOutOfBoundsException e) {
615             // expected
616         }
617 
618         // IndexOutOfBoundsException: offset < 0
619         try {
620             float[] data = new float[8];
621             fb.position(0);
622             fb.put(data, -1, 2);
623             fail("expected exception not thrown");
624         } catch (IndexOutOfBoundsException e) {
625             // expected
626         }
627 
628         // IndexOutOfBoundsException: length > array.length - offset
629         try {
630             float[] data = new float[8];
631             fb.position(0);
632             fb.put(data, 1, 8);
633             fail("expected exception not thrown");
634         } catch (IndexOutOfBoundsException e) {
635             // expected
636         }
637 
638         // BufferOverflowException: length > remaining()
639         try {
640             float[] data = new float[8];
641             fb.position(fb.limit() - 2);
642             fb.put(data, 0, 3);
643             fail("expected exception not thrown");
644         } catch (BufferOverflowException e) {
645             // expected
646         }
647 
648         float[] data = {0, 10, 20, 30, 40, 50, 60, 70};
649         fb.position(0);
650         fb.put(data);
651 
652         try {
653             fb.get();
654             fail("expected exception not thrown");
655         } catch (BufferUnderflowException e) {
656             // expected
657         }
658 
659         fb.position(0);
660         assertEquals((float) 0, fb.get());
661         assertEquals((float) 10, fb.get());
662         assertEquals((float) 20, fb.get());
663         assertEquals((float) 30, fb.get());
664         assertEquals((float) 40, fb.get());
665         assertEquals((float) 50, fb.get());
666         assertEquals((float) 60, fb.get());
667         assertEquals((float) 70, fb.get());
668         try {
669             fb.get();
670             fail("expected exception not thrown");
671         } catch (BufferUnderflowException e) {
672             // expected
673         }
674         fb.position(1);
675         fb.put((float) 11);
676         assertEquals((float) 11, fb.get(1));
677 
678         float[] ss1 = {33, 44, 55, 66};
679         fb.position(3);
680         fb.put(ss1);
681         fb.position(0);
682         assertEquals((float) 0, fb.get());
683         assertEquals((float) 11, fb.get());
684         assertEquals((float) 20, fb.get());
685         assertEquals((float) 33, fb.get());
686         assertEquals((float) 44, fb.get());
687         assertEquals((float) 55, fb.get());
688         assertEquals((float) 66, fb.get());
689         assertEquals((float) 70, fb.get());
690 
691         float[] ss2 = {10, 22, 30};
692         fb.position(2);
693         fb.put(ss2, 1, 1);
694         fb.position(0);
695         assertEquals((float) 0, fb.get());
696         assertEquals((float) 11, fb.get());
697         assertEquals((float) 22, fb.get());
698         assertEquals((float) 33, fb.get());
699         assertEquals((float) 44, fb.get());
700         assertEquals((float) 55, fb.get());
701         assertEquals((float) 66, fb.get());
702         assertEquals((float) 70, fb.get());
703     }
704 }
705