blob: aca91b0291ce504beb4d649cdb48483ac18932f1 [file] [log] [blame]
Alan Viverette3da604b2020-06-10 18:34:39 +00001/* 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
17package java.nio;
18
19import android.compat.annotation.UnsupportedAppUsage;
20
21import java.io.Closeable;
22import java.io.FileDescriptor;
23import java.nio.channels.FileChannel;
24
25import sun.nio.ch.FileChannelImpl;
26
27import static android.system.OsConstants.O_ACCMODE;
28import static android.system.OsConstants.O_APPEND;
29import static android.system.OsConstants.O_RDONLY;
30import static android.system.OsConstants.O_WRONLY;
31
32/**
33 * @hide internal use only
34 */
35@libcore.api.CorePlatformApi
36public final class NioUtils {
37 private NioUtils() {
38 }
39
40 @UnsupportedAppUsage
41 @libcore.api.CorePlatformApi
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 */
59 public static FileDescriptor getFD(FileChannel fc) {
60 return ((FileChannelImpl) fc).fd;
61 }
62
63 /**
64 * Helps bridge between io and nio.
65 */
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
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
89 public static int unsafeArrayOffset(ByteBuffer b) {
90 return b.arrayOffset();
91 }
92}