181550e2d43bbf16be983722877519362799e7d90Jesse Wilson/* Licensed to the Apache Software Foundation (ASF) under one or more 281550e2d43bbf16be983722877519362799e7d90Jesse Wilson * contributor license agreements. See the NOTICE file distributed with 381550e2d43bbf16be983722877519362799e7d90Jesse Wilson * this work for additional information regarding copyright ownership. 481550e2d43bbf16be983722877519362799e7d90Jesse Wilson * The ASF licenses this file to You under the Apache License, Version 2.0 581550e2d43bbf16be983722877519362799e7d90Jesse Wilson * (the "License"); you may not use this file except in compliance with 681550e2d43bbf16be983722877519362799e7d90Jesse Wilson * the License. You may obtain a copy of the License at 781550e2d43bbf16be983722877519362799e7d90Jesse Wilson * 881550e2d43bbf16be983722877519362799e7d90Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0 981550e2d43bbf16be983722877519362799e7d90Jesse Wilson * 1081550e2d43bbf16be983722877519362799e7d90Jesse Wilson * Unless required by applicable law or agreed to in writing, software 1181550e2d43bbf16be983722877519362799e7d90Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS, 1281550e2d43bbf16be983722877519362799e7d90Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1381550e2d43bbf16be983722877519362799e7d90Jesse Wilson * See the License for the specific language governing permissions and 1481550e2d43bbf16be983722877519362799e7d90Jesse Wilson * limitations under the License. 1581550e2d43bbf16be983722877519362799e7d90Jesse Wilson */ 1681550e2d43bbf16be983722877519362799e7d90Jesse Wilson 1781550e2d43bbf16be983722877519362799e7d90Jesse Wilsonpackage libcore.java.nio; 1881550e2d43bbf16be983722877519362799e7d90Jesse Wilson 1981550e2d43bbf16be983722877519362799e7d90Jesse Wilsonimport java.nio.ByteBuffer; 2081550e2d43bbf16be983722877519362799e7d90Jesse Wilsonimport java.nio.ReadOnlyBufferException; 2181550e2d43bbf16be983722877519362799e7d90Jesse Wilsonimport junit.framework.TestCase; 2281550e2d43bbf16be983722877519362799e7d90Jesse Wilson 2381550e2d43bbf16be983722877519362799e7d90Jesse Wilsonpublic final class NoArrayTest extends TestCase { 24bc01e68730f04cb8dbbf7f3b7da82006c2151ebfElliott Hughes public void testReadOnly() { 25bc01e68730f04cb8dbbf7f3b7da82006c2151ebfElliott Hughes // A read-only buffer must not expose its array. 2681550e2d43bbf16be983722877519362799e7d90Jesse Wilson assertNoArray(ByteBuffer.wrap(new byte[32]).asReadOnlyBuffer()); 2781550e2d43bbf16be983722877519362799e7d90Jesse Wilson assertNoArray(ByteBuffer.allocate(32).asReadOnlyBuffer()); 28bc01e68730f04cb8dbbf7f3b7da82006c2151ebfElliott Hughes assertNoArray(ByteBuffer.allocateDirect(32).asReadOnlyBuffer()); 2981550e2d43bbf16be983722877519362799e7d90Jesse Wilson } 3081550e2d43bbf16be983722877519362799e7d90Jesse Wilson 3181550e2d43bbf16be983722877519362799e7d90Jesse Wilson private void assertNoArray(ByteBuffer buf) { 32bc01e68730f04cb8dbbf7f3b7da82006c2151ebfElliott Hughes assertFalse(buf.hasArray()); 3381550e2d43bbf16be983722877519362799e7d90Jesse Wilson try { 34bc01e68730f04cb8dbbf7f3b7da82006c2151ebfElliott Hughes buf.array(); 3581550e2d43bbf16be983722877519362799e7d90Jesse Wilson fail(); 3681550e2d43bbf16be983722877519362799e7d90Jesse Wilson } catch (ReadOnlyBufferException expected) { 3781550e2d43bbf16be983722877519362799e7d90Jesse Wilson } catch (UnsupportedOperationException expected) { 3881550e2d43bbf16be983722877519362799e7d90Jesse Wilson } 3981550e2d43bbf16be983722877519362799e7d90Jesse Wilson try { 4081550e2d43bbf16be983722877519362799e7d90Jesse Wilson buf.arrayOffset(); 4181550e2d43bbf16be983722877519362799e7d90Jesse Wilson fail(); 4281550e2d43bbf16be983722877519362799e7d90Jesse Wilson } catch (ReadOnlyBufferException expected) { 4381550e2d43bbf16be983722877519362799e7d90Jesse Wilson } catch (UnsupportedOperationException expected) { 4481550e2d43bbf16be983722877519362799e7d90Jesse Wilson } 4581550e2d43bbf16be983722877519362799e7d90Jesse Wilson } 4681550e2d43bbf16be983722877519362799e7d90Jesse Wilson} 47