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 public final class NioUtils { NioUtils()40 private NioUtils() { 41 } 42 43 /** 44 * Frees {@link DirectByteBuffer} running associated {@link sun.misc.Cleaner Cleaner}. 45 * 46 * @param buffer to free with associated {@code Cleaner} 47 * 48 * @hide 49 */ 50 @UnsupportedAppUsage 51 @SystemApi(client = MODULE_LIBRARIES) freeDirectBuffer(ByteBuffer buffer)52 public static void freeDirectBuffer(ByteBuffer buffer) { 53 if (buffer == null) { 54 return; 55 } 56 57 DirectByteBuffer dbb = (DirectByteBuffer) buffer; 58 // Run the cleaner early, if one is defined. 59 if (dbb.cleaner != null) { 60 dbb.cleaner.clean(); 61 } 62 63 dbb.memoryRef.free(); 64 } 65 66 /** 67 * Returns the int file descriptor from within the given FileChannel 'fc'. 68 * 69 * @hide 70 */ getFD(FileChannel fc)71 public static FileDescriptor getFD(FileChannel fc) { 72 return ((FileChannelImpl) fc).fd; 73 } 74 75 /** 76 * Helps bridge between io and nio. This method is kept only for the hidden API access. 77 * art/build/boot/hiddenapi/hiddenapi-max-target-o-low-priority.txt 78 * 79 * @hide 80 */ newFileChannel(Closeable ioObject, FileDescriptor fd, int mode)81 public static FileChannel newFileChannel(Closeable ioObject, FileDescriptor fd, int mode) { 82 boolean readable = (mode & O_ACCMODE) != O_WRONLY; 83 boolean writable = (mode & O_ACCMODE) != O_RDONLY; 84 return FileChannelImpl.open(fd, null, readable, writable, ioObject); 85 } 86 87 /** 88 * Exposes the array backing a non-direct {@link java.nio.ByteBuffer ByteBuffer}, even if 89 * the {@link java.nio.ByteBuffer ByteBuffer} is read-only. 90 * Normally, attempting to access the array backing a read-only buffer throws. 91 * 92 * @param b {@link java.nio.ByteBuffer ByteBuffer} to access its backing array. 93 * @return buffer's underlying array. 94 * 95 * @hide 96 */ 97 @UnsupportedAppUsage 98 @SystemApi(client = MODULE_LIBRARIES) unsafeArray(ByteBuffer b)99 public static byte[] unsafeArray(ByteBuffer b) { 100 return b.array(); 101 } 102 103 /** 104 * Exposes the array offset for the array backing a non-direct {@link java.nio.ByteBuffer ByteBuffer}, 105 * even if the {@link java.nio.ByteBuffer ByteBuffer} is read-only. 106 * 107 * @param b {@link java.nio.ByteBuffer ByteBuffer} to access its backing array offset. 108 * @return buffer's underlying array data offset. 109 * 110 * @hide 111 */ 112 @UnsupportedAppUsage 113 @SystemApi(client = MODULE_LIBRARIES) unsafeArrayOffset(ByteBuffer b)114 public static int unsafeArrayOffset(ByteBuffer b) { 115 return b.arrayOffset(); 116 } 117 } 118