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 */ 16 17 package java.nio; 18 19 import android.compat.annotation.UnsupportedAppUsage; 20 21 import java.io.Closeable; 22 import java.io.FileDescriptor; 23 import java.nio.channels.FileChannel; 24 25 import sun.nio.ch.FileChannelImpl; 26 27 import static android.system.OsConstants.O_ACCMODE; 28 import static android.system.OsConstants.O_APPEND; 29 import static android.system.OsConstants.O_RDONLY; 30 import static android.system.OsConstants.O_WRONLY; 31 32 /** 33 * @hide internal use only 34 */ 35 @libcore.api.CorePlatformApi 36 public final class NioUtils { NioUtils()37 private NioUtils() { 38 } 39 40 @UnsupportedAppUsage 41 @libcore.api.CorePlatformApi freeDirectBuffer(ByteBuffer buffer)42 public static void freeDirectBuffer(ByteBuffer buffer) { 43 if (buffer == null) { 44 return; 45 } 46 47 DirectByteBuffer dbb = (DirectByteBuffer) buffer; 48 // Run the cleaner early, if one is defined. 49 if (dbb.cleaner != null) { 50 dbb.cleaner.clean(); 51 } 52 53 dbb.memoryRef.free(); 54 } 55 56 /** 57 * Returns the int file descriptor from within the given FileChannel 'fc'. 58 */ getFD(FileChannel fc)59 public static FileDescriptor getFD(FileChannel fc) { 60 return ((FileChannelImpl) fc).fd; 61 } 62 63 /** 64 * Helps bridge between io and nio. 65 */ newFileChannel(Closeable ioObject, FileDescriptor fd, int mode)66 public static FileChannel newFileChannel(Closeable ioObject, FileDescriptor fd, int mode) { 67 boolean readable = (mode & O_ACCMODE) != O_WRONLY; 68 boolean writable = (mode & O_ACCMODE) != O_RDONLY; 69 boolean append = (mode & O_APPEND) != 0; 70 return FileChannelImpl.open(fd, null, readable, writable, append, ioObject); 71 } 72 73 /** 74 * Exposes the array backing a non-direct ByteBuffer, even if the ByteBuffer is read-only. 75 * Normally, attempting to access the array backing a read-only buffer throws. 76 */ 77 @UnsupportedAppUsage 78 @libcore.api.CorePlatformApi unsafeArray(ByteBuffer b)79 public static byte[] unsafeArray(ByteBuffer b) { 80 return b.array(); 81 } 82 83 /** 84 * Exposes the array offset for the array backing a non-direct ByteBuffer, 85 * even if the ByteBuffer is read-only. 86 */ 87 @UnsupportedAppUsage 88 @libcore.api.CorePlatformApi unsafeArrayOffset(ByteBuffer b)89 public static int unsafeArrayOffset(ByteBuffer b) { 90 return b.arrayOffset(); 91 } 92 } 93