Main.java revision 5d1ac920fdaef5d4ec8f66bb734488cd9660b024
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
17import java.nio.BufferOverflowException;
18import java.nio.ByteBuffer;
19import java.nio.ByteOrder;
20import java.nio.FloatBuffer;
21import java.nio.IntBuffer;
22import java.nio.ShortBuffer;
23
24public class Main {
25    public static void main(String[] args) {
26         intFloatTest();
27         basicShortTest();
28    }
29
30    /*
31     * Create a buffer and fiddle with it.
32     */
33    public static void basicShortTest() {
34        ByteBuffer directBuf = ByteBuffer.allocateDirect(64);
35        //ByteBuffer directBuf = ByteBuffer.allocateDirect(65);
36
37        ShortBuffer shortBuf = directBuf.asShortBuffer();
38
39        short[] myShorts = {
40            1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007,
41            1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015,
42            1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023,
43            1024, 1025, 1026, 1027, 1028, 1029, 1030, 1031
44        };
45
46        shortBuf.position(0);
47        shortBuf.put(myShorts, 0, 32);      // should work
48        shortBuf.position(0);
49        shortBuf.put(myShorts, 16, 16);     // should work
50        shortBuf.put(myShorts, 16, 16);     // advance to end
51
52        try {
53            shortBuf.put(myShorts, 0, 1);     // should fail
54            System.err.println("ERROR: out-of-bounds put succeeded\n");
55        } catch (BufferOverflowException boe) {
56            System.out.println("Got expected buffer overflow exception");
57        }
58
59        try {
60            shortBuf.position(0);
61            shortBuf.put(myShorts, 0, 33);     // should fail
62            System.err.println("ERROR: out-of-bounds put succeeded\n");
63        } catch (IndexOutOfBoundsException ioobe) {
64            System.out.println("Got expected out-of-bounds exception");
65        }
66
67        try {
68            shortBuf.position(16);
69            shortBuf.put(myShorts, 0, 17);     // should fail
70            System.err.println("ERROR: out-of-bounds put succeeded\n");
71        } catch (BufferOverflowException boe) {
72            System.out.println("Got expected buffer overflow exception");
73        }
74    }
75
76    /*
77     * Try this with either floats or ints; ints fail with
78     * BufferOverflowException, floats work.
79     *
80     * From http://code.google.com/p/android/issues/detail?id=1585 .
81     */
82    public static void intFloatTest() {
83        ByteBuffer direct = ByteBuffer.allocateDirect(100);
84        direct.order(ByteOrder.nativeOrder());
85        IntBuffer int1 = direct.asIntBuffer();
86        int data[] = new int[25];
87        //FloatBuffer int1 = direct.asFloatBuffer();
88        //float data[] = new float[25];
89        int1.clear ();
90        int1.put (data);
91        int1.position (0);
92
93        int1.clear ();
94        int1.put (data);
95        int1.position (0);
96    }
97}
98