1eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson/* Licensed to the Apache Software Foundation (ASF) under one or more 2adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * contributor license agreements. See the NOTICE file distributed with 3adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * this work for additional information regarding copyright ownership. 4adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The ASF licenses this file to You under the Apache License, Version 2.0 5adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * (the "License"); you may not use this file except in compliance with 6adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the License. You may obtain a copy of the License at 7f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 8adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 9f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 10adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Unless required by applicable law or agreed to in writing, software 11adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 12adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * See the License for the specific language governing permissions and 14adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * limitations under the License. 15adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 16adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 17c73a5be50cdd804ff3c12e7b43da08c33cdd6f21Elliott Hughespackage java.nio; 18adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 1952724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesimport java.io.FileDescriptor; 20ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughesimport java.nio.channels.FileChannel; 21ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes 22c73a5be50cdd804ff3c12e7b43da08c33cdd6f21Elliott Hughes/** 23c73a5be50cdd804ff3c12e7b43da08c33cdd6f21Elliott Hughes * @hide internal use only 24c73a5be50cdd804ff3c12e7b43da08c33cdd6f21Elliott Hughes */ 25c73a5be50cdd804ff3c12e7b43da08c33cdd6f21Elliott Hughespublic final class NioUtils { 26ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes private NioUtils() { 27ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes } 28ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes 29adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project /** 30adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Gets the start address of a direct buffer. 31adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p> 32adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * This method corresponds to the JNI function: 33f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 34adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <pre> 35adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * void* GetDirectBufferAddress(JNIEnv* env, jobject buf); 36adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </pre> 37f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 38adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @param buf 39adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the direct buffer whose address shall be returned must not be 40adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <code>null</code>. 41adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @return the address of the buffer given, or zero if the buffer is not a 42adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * direct Buffer. 43adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 44f46d2ce4d4c92d16e2574f57f70fc5477dc12697Elliott Hughes public static int getDirectBufferAddress(Buffer buffer) { 450f335c4ed1a0cb419c8ce82183fabb241d5dd032Elliott Hughes return buffer.effectiveDirectAddress; 46adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 47c73a5be50cdd804ff3c12e7b43da08c33cdd6f21Elliott Hughes 48c73a5be50cdd804ff3c12e7b43da08c33cdd6f21Elliott Hughes public static void freeDirectBuffer(ByteBuffer buffer) { 49c73a5be50cdd804ff3c12e7b43da08c33cdd6f21Elliott Hughes if (buffer == null) { 50c73a5be50cdd804ff3c12e7b43da08c33cdd6f21Elliott Hughes return; 51c73a5be50cdd804ff3c12e7b43da08c33cdd6f21Elliott Hughes } 52c73a5be50cdd804ff3c12e7b43da08c33cdd6f21Elliott Hughes if (buffer instanceof DirectByteBuffer) { 53c73a5be50cdd804ff3c12e7b43da08c33cdd6f21Elliott Hughes ((DirectByteBuffer) buffer).free(); 54c73a5be50cdd804ff3c12e7b43da08c33cdd6f21Elliott Hughes } else if (buffer instanceof MappedByteBuffer) { 55c73a5be50cdd804ff3c12e7b43da08c33cdd6f21Elliott Hughes ((MappedByteBufferAdapter) buffer).free(); 56c73a5be50cdd804ff3c12e7b43da08c33cdd6f21Elliott Hughes } else { 57c73a5be50cdd804ff3c12e7b43da08c33cdd6f21Elliott Hughes throw new AssertionError(); 58c73a5be50cdd804ff3c12e7b43da08c33cdd6f21Elliott Hughes } 59c73a5be50cdd804ff3c12e7b43da08c33cdd6f21Elliott Hughes } 60ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes 61ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes /** 62ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes * Returns the int file descriptor from within the given FileChannel 'fc'. 63ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes */ 6452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes public static FileDescriptor getFD(FileChannel fc) { 6552724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes return ((FileChannelImpl) fc).getFD(); 66ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes } 67ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes 68ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes /** 69ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes * Helps bridge between io and nio. 70ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes */ 7152724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes public static FileChannel newFileChannel(Object stream, FileDescriptor fd, int mode) { 726ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes return new FileChannelImpl(stream, fd, mode); 73ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes } 744af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes 754af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes /** 764af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes * Exposes the array backing a non-direct ByteBuffer, even if the ByteBuffer is read-only. 774af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes * Normally, attempting to access the array backing a read-only buffer throws. 784af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes */ 794af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes public static byte[] unsafeArray(ByteBuffer b) { 804af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes return ((HeapByteBuffer) b).backingArray; 814af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes } 824af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes 834af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes /** 844af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes * Exposes the array offset for the array backing a non-direct ByteBuffer, 854af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes * even if the ByteBuffer is read-only. 864af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes */ 874af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes public static int unsafeArrayOffset(ByteBuffer b) { 884af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes return ((HeapByteBuffer) b).offset; 894af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes } 9092787771b917bd9ca546e83840ed03a7e7b293c5Elliott Hughes} 91