DirectByteBufferTest.java revision 0f153ca202c986addbb6f481a6f922aa5158c797
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  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 */
16package org.apache.harmony.tests.java.nio;
17
18import java.nio.ByteBuffer;
19import java.nio.ByteOrder;
20import java.nio.IntBuffer;
21import java.nio.LongBuffer;
22import java.nio.ShortBuffer;
23
24public class DirectByteBufferTest extends ByteBufferTest {
25
26    protected void setUp() throws Exception {
27        super.setUp();
28        buf = ByteBuffer.allocateDirect(BUFFER_LENGTH);
29        baseBuf = buf;
30    }
31
32    protected void tearDown() throws Exception {
33        super.tearDown();
34        buf = null;
35        baseBuf = null;
36    }
37
38    /**
39     * @tests java.nio.ByteBuffer#allocateDirect(int)
40     */
41    public void testAllocatedByteBuffer_IllegalArg() {
42        try {
43            ByteBuffer.allocateDirect(-1);
44            fail("Should throw Exception"); //$NON-NLS-1$
45        } catch (IllegalArgumentException e) {
46            // expected
47        }
48    }
49
50    public void testIsDirect() {
51        assertTrue(buf.isDirect());
52    }
53
54    public void testHasArray() {
55        // Android direct byte buffers have backing arrays.
56        assertTrue(buf.hasArray());
57        // assertFalse(buf.hasArray());
58    }
59
60    public void testIsReadOnly() {
61        assertFalse(buf.isReadOnly());
62    }
63
64    // http://b/19692084
65    // http://b/21491780
66    public void testUnalignedReadsAndWrites() {
67        // We guarantee that the first byte of the buffer is 8 byte aligned.
68        ByteBuffer buf = ByteBuffer.allocateDirect(23);
69        // Native order is always little endian, so this forces swaps.
70        buf.order(ByteOrder.BIG_ENDIAN);
71
72        for (int i = 0; i < 8; ++i) {
73            buf.position(i);
74
75            // 2 byte swaps.
76            ShortBuffer shortBuf = buf.asShortBuffer();
77            short[] shortArray = new short[]{42, 24};
78
79            // Write.
80            shortBuf.put(shortArray);
81            // Read
82            shortBuf.flip();
83            shortBuf.get(shortArray);
84            // Assert Equality
85            assertEquals(42, shortArray[0]);
86            assertEquals(24, shortArray[1]);
87
88            buf.position(i);
89            // 4 byte swaps.
90            IntBuffer intBuf = buf.asIntBuffer();
91            int[] intArray = new int[]{967, 1983};
92            // Write.
93            intBuf.put(intArray);
94            // Read
95            intBuf.flip();
96            intBuf.get(intArray);
97            // Assert Equality
98            assertEquals(967, intArray[0]);
99            assertEquals(1983, intArray[1]);
100
101
102            buf.position(i);
103            // 8 byte swaps.
104            LongBuffer longBuf = buf.asLongBuffer();
105            long[] longArray = new long[]{2147484614L, 2147485823L};
106            // Write.
107            longBuf.put(longArray);
108            // Read
109            longBuf.flip();
110            longBuf.get(longArray);
111            // Assert Equality
112            assertEquals(2147484614L, longArray[0]);
113            assertEquals(2147485823L, longArray[1]);
114        }
115    }
116
117    public void testIsAccessible() {
118        buf.clear();
119        assertTrue(buf.isAccessible());
120        buf.get(0);
121        buf.setAccessible(false);
122        try {
123            buf.get(0);
124            fail("should throw exception");
125        } catch (IllegalStateException e) {
126            // expected
127        }
128        buf.setAccessible(true);
129        buf.get(0);
130    }
131}
132