jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame^] | 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 | import java.nio.BufferOverflowException; |
| 18 | import java.nio.ByteBuffer; |
| 19 | import java.nio.ByteOrder; |
| 20 | import java.nio.FloatBuffer; |
| 21 | import java.nio.IntBuffer; |
| 22 | import java.nio.ShortBuffer; |
| 23 | |
| 24 | public 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 | } |