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 static android.annotation.SystemApi.Client.MODULE_LIBRARIES; 20 21 import android.annotation.SystemApi; 22 import android.compat.annotation.UnsupportedAppUsage; 23 24 import java.io.Closeable; 25 import java.io.FileDescriptor; 26 import java.nio.channels.FileChannel; 27 28 import sun.nio.ch.FileChannelImpl; 29 30 import static android.system.OsConstants.O_ACCMODE; 31 import static android.system.OsConstants.O_APPEND; 32 import static android.system.OsConstants.O_RDONLY; 33 import static android.system.OsConstants.O_WRONLY; 34 35 /** 36 * @hide internal use only 37 */ 38 @SystemApi(client = MODULE_LIBRARIES) 39 @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE) 40 public final class NioUtils { NioUtils()41 private NioUtils() { 42 } 43 44 /** 45 * Frees {@link DirectByteBuffer} running associated {@link sun.misc.Cleaner Cleaner}. 46 * 47 * @param buffer to free with associated {@code Cleaner} 48 * 49 * @hide 50 */ 51 @UnsupportedAppUsage 52 @SystemApi(client = MODULE_LIBRARIES) 53 @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE) freeDirectBuffer(ByteBuffer buffer)54 public static void freeDirectBuffer(ByteBuffer buffer) { 55 if (buffer == null) { 56 return; 57 } 58 59 DirectByteBuffer dbb = (DirectByteBuffer) buffer; 60 // Run the cleaner early, if one is defined. 61 if (dbb.cleaner != null) { 62 dbb.cleaner.clean(); 63 } 64 65 dbb.memoryRef.free(); 66 } 67 68 /** 69 * Returns the int file descriptor from within the given FileChannel 'fc'. 70 * 71 * @hide 72 */ getFD(FileChannel fc)73 public static FileDescriptor getFD(FileChannel fc) { 74 return ((FileChannelImpl) fc).fd; 75 } 76 77 /** 78 * Helps bridge between io and nio. 79 * 80 * @hide 81 */ newFileChannel(Closeable ioObject, FileDescriptor fd, int mode)82 public static FileChannel newFileChannel(Closeable ioObject, FileDescriptor fd, int mode) { 83 boolean readable = (mode & O_ACCMODE) != O_WRONLY; 84 boolean writable = (mode & O_ACCMODE) != O_RDONLY; 85 boolean append = (mode & O_APPEND) != 0; 86 return FileChannelImpl.open(fd, null, readable, writable, append, ioObject); 87 } 88 89 /** 90 * Exposes the array backing a non-direct {@link java.nio.ByteBuffer ByteBuffer}, even if 91 * the {@link java.nio.ByteBuffer ByteBuffer} is read-only. 92 * Normally, attempting to access the array backing a read-only buffer throws. 93 * 94 * @param b {@link java.nio.ByteBuffer ByteBuffer} to access its backing array. 95 * @return buffer's underlying array. 96 * 97 * @hide 98 */ 99 @UnsupportedAppUsage 100 @SystemApi(client = MODULE_LIBRARIES) 101 @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE) unsafeArray(ByteBuffer b)102 public static byte[] unsafeArray(ByteBuffer b) { 103 return b.array(); 104 } 105 106 /** 107 * Exposes the array offset for the array backing a non-direct {@link java.nio.ByteBuffer ByteBuffer}, 108 * even if the {@link java.nio.ByteBuffer ByteBuffer} is read-only. 109 * 110 * @param b {@link java.nio.ByteBuffer ByteBuffer} to access its backing array offset. 111 * @return buffer's underlying array data offset. 112 * 113 * @hide 114 */ 115 @UnsupportedAppUsage 116 @SystemApi(client = MODULE_LIBRARIES) 117 @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE) unsafeArrayOffset(ByteBuffer b)118 public static int unsafeArrayOffset(ByteBuffer b) { 119 return b.arrayOffset(); 120 } 121 } 122