1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * 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 libcore.io;
18 
19 import android.system.ErrnoException;
20 import android.system.GaiException;
21 import android.system.Int32Ref;
22 import android.system.Int64Ref;
23 import android.system.StructAddrinfo;
24 import android.system.StructCapUserData;
25 import android.system.StructCapUserHeader;
26 import android.system.StructGroupReq;
27 import android.system.StructIfaddrs;
28 import android.system.StructLinger;
29 import android.system.StructPasswd;
30 import android.system.StructPollfd;
31 import android.system.StructRlimit;
32 import android.system.StructStat;
33 import android.system.StructStatVfs;
34 import android.system.StructTimeval;
35 import android.system.StructUcred;
36 import android.system.StructUtsname;
37 
38 import java.io.FileDescriptor;
39 import java.io.InterruptedIOException;
40 import java.net.InetAddress;
41 import java.net.InetSocketAddress;
42 import java.net.SocketAddress;
43 import java.net.SocketException;
44 import java.nio.ByteBuffer;
45 import java.nio.NioUtils;
46 
47 public final class Linux implements Os {
Linux()48     Linux() { }
49 
accept(FileDescriptor fd, SocketAddress peerAddress)50     public native FileDescriptor accept(FileDescriptor fd, SocketAddress peerAddress) throws ErrnoException, SocketException;
access(String path, int mode)51     public native boolean access(String path, int mode) throws ErrnoException;
android_getaddrinfo(String node, StructAddrinfo hints, int netId)52     public native InetAddress[] android_getaddrinfo(String node, StructAddrinfo hints, int netId) throws GaiException;
bind(FileDescriptor fd, InetAddress address, int port)53     public native void bind(FileDescriptor fd, InetAddress address, int port) throws ErrnoException, SocketException;
bind(FileDescriptor fd, SocketAddress address)54     public native void bind(FileDescriptor fd, SocketAddress address) throws ErrnoException, SocketException;
55     @Override
capget(StructCapUserHeader hdr)56     public native StructCapUserData[] capget(StructCapUserHeader hdr) throws ErrnoException;
57     @Override
capset(StructCapUserHeader hdr, StructCapUserData[] data)58     public native void capset(StructCapUserHeader hdr, StructCapUserData[] data)
59             throws ErrnoException;
chmod(String path, int mode)60     public native void chmod(String path, int mode) throws ErrnoException;
chown(String path, int uid, int gid)61     public native void chown(String path, int uid, int gid) throws ErrnoException;
62 
close(FileDescriptor fd)63     public native void close(FileDescriptor fd) throws ErrnoException;
android_fdsan_exchange_owner_tag(FileDescriptor fd, long previousOwnerId, long newOwnerId)64     public native void android_fdsan_exchange_owner_tag(FileDescriptor fd, long previousOwnerId, long newOwnerId);
android_fdsan_get_owner_tag(FileDescriptor fd)65     public native long android_fdsan_get_owner_tag(FileDescriptor fd);
android_fdsan_get_tag_type(long tag)66     public native String android_fdsan_get_tag_type(long tag);
android_fdsan_get_tag_value(long tag)67     public native long android_fdsan_get_tag_value(long tag);
68 
connect(FileDescriptor fd, InetAddress address, int port)69     public native void connect(FileDescriptor fd, InetAddress address, int port) throws ErrnoException, SocketException;
connect(FileDescriptor fd, SocketAddress address)70     public native void connect(FileDescriptor fd, SocketAddress address) throws ErrnoException, SocketException;
dup(FileDescriptor oldFd)71     public native FileDescriptor dup(FileDescriptor oldFd) throws ErrnoException;
dup2(FileDescriptor oldFd, int newFd)72     public native FileDescriptor dup2(FileDescriptor oldFd, int newFd) throws ErrnoException;
environ()73     public native String[] environ();
execv(String filename, String[] argv)74     public native void execv(String filename, String[] argv) throws ErrnoException;
execve(String filename, String[] argv, String[] envp)75     public native void execve(String filename, String[] argv, String[] envp) throws ErrnoException;
fchmod(FileDescriptor fd, int mode)76     public native void fchmod(FileDescriptor fd, int mode) throws ErrnoException;
fchown(FileDescriptor fd, int uid, int gid)77     public native void fchown(FileDescriptor fd, int uid, int gid) throws ErrnoException;
fcntlInt(FileDescriptor fd, int cmd, int arg)78     public native int fcntlInt(FileDescriptor fd, int cmd, int arg) throws ErrnoException;
fcntlVoid(FileDescriptor fd, int cmd)79     public native int fcntlVoid(FileDescriptor fd, int cmd) throws ErrnoException;
fdatasync(FileDescriptor fd)80     public native void fdatasync(FileDescriptor fd) throws ErrnoException;
fstat(FileDescriptor fd)81     public native StructStat fstat(FileDescriptor fd) throws ErrnoException;
fstatvfs(FileDescriptor fd)82     public native StructStatVfs fstatvfs(FileDescriptor fd) throws ErrnoException;
fsync(FileDescriptor fd)83     public native void fsync(FileDescriptor fd) throws ErrnoException;
ftruncate(FileDescriptor fd, long length)84     public native void ftruncate(FileDescriptor fd, long length) throws ErrnoException;
gai_strerror(int error)85     public native String gai_strerror(int error);
getegid()86     public native int getegid();
geteuid()87     public native int geteuid();
getgid()88     public native int getgid();
getenv(String name)89     public native String getenv(String name);
getnameinfo(InetAddress address, int flags)90     public native String getnameinfo(InetAddress address, int flags) throws GaiException;
getpeername(FileDescriptor fd)91     public native SocketAddress getpeername(FileDescriptor fd) throws ErrnoException;
getpgid(int pid)92     public native int getpgid(int pid);
getpid()93     public native int getpid();
getppid()94     public native int getppid();
getpwnam(String name)95     public native StructPasswd getpwnam(String name) throws ErrnoException;
getpwuid(int uid)96     public native StructPasswd getpwuid(int uid) throws ErrnoException;
getrlimit(int resource)97     public native StructRlimit getrlimit(int resource) throws ErrnoException;
getsockname(FileDescriptor fd)98     public native SocketAddress getsockname(FileDescriptor fd) throws ErrnoException;
getsockoptByte(FileDescriptor fd, int level, int option)99     public native int getsockoptByte(FileDescriptor fd, int level, int option) throws ErrnoException;
getsockoptInAddr(FileDescriptor fd, int level, int option)100     public native InetAddress getsockoptInAddr(FileDescriptor fd, int level, int option) throws ErrnoException;
getsockoptInt(FileDescriptor fd, int level, int option)101     public native int getsockoptInt(FileDescriptor fd, int level, int option) throws ErrnoException;
getsockoptLinger(FileDescriptor fd, int level, int option)102     public native StructLinger getsockoptLinger(FileDescriptor fd, int level, int option) throws ErrnoException;
getsockoptTimeval(FileDescriptor fd, int level, int option)103     public native StructTimeval getsockoptTimeval(FileDescriptor fd, int level, int option) throws ErrnoException;
getsockoptUcred(FileDescriptor fd, int level, int option)104     public native StructUcred getsockoptUcred(FileDescriptor fd, int level, int option) throws ErrnoException;
gettid()105     public native int gettid();
getuid()106     public native int getuid();
getxattr(String path, String name)107     public native byte[] getxattr(String path, String name) throws ErrnoException;
getifaddrs()108     public native StructIfaddrs[] getifaddrs() throws ErrnoException;
if_indextoname(int index)109     public native String if_indextoname(int index);
if_nametoindex(String name)110     public native int if_nametoindex(String name);
inet_pton(int family, String address)111     public native InetAddress inet_pton(int family, String address);
ioctlFlags(FileDescriptor fd, String interfaceName)112     public native int ioctlFlags(FileDescriptor fd, String interfaceName) throws ErrnoException;
ioctlInetAddress(FileDescriptor fd, int cmd, String interfaceName)113     public native InetAddress ioctlInetAddress(FileDescriptor fd, int cmd, String interfaceName) throws ErrnoException;
ioctlInt(FileDescriptor fd, int cmd, Int32Ref arg)114     public native int ioctlInt(FileDescriptor fd, int cmd, Int32Ref arg) throws ErrnoException;
ioctlMTU(FileDescriptor fd, String interfaceName)115     public native int ioctlMTU(FileDescriptor fd, String interfaceName) throws ErrnoException;
isatty(FileDescriptor fd)116     public native boolean isatty(FileDescriptor fd);
kill(int pid, int signal)117     public native void kill(int pid, int signal) throws ErrnoException;
lchown(String path, int uid, int gid)118     public native void lchown(String path, int uid, int gid) throws ErrnoException;
link(String oldPath, String newPath)119     public native void link(String oldPath, String newPath) throws ErrnoException;
listen(FileDescriptor fd, int backlog)120     public native void listen(FileDescriptor fd, int backlog) throws ErrnoException;
listxattr(String path)121     public native String[] listxattr(String path) throws ErrnoException;
lseek(FileDescriptor fd, long offset, int whence)122     public native long lseek(FileDescriptor fd, long offset, int whence) throws ErrnoException;
lstat(String path)123     public native StructStat lstat(String path) throws ErrnoException;
memfd_create(String name, int flags)124     public native FileDescriptor memfd_create(String name, int flags) throws ErrnoException;
mincore(long address, long byteCount, byte[] vector)125     public native void mincore(long address, long byteCount, byte[] vector) throws ErrnoException;
mkdir(String path, int mode)126     public native void mkdir(String path, int mode) throws ErrnoException;
mkfifo(String path, int mode)127     public native void mkfifo(String path, int mode) throws ErrnoException;
mlock(long address, long byteCount)128     public native void mlock(long address, long byteCount) throws ErrnoException;
mmap(long address, long byteCount, int prot, int flags, FileDescriptor fd, long offset)129     public native long mmap(long address, long byteCount, int prot, int flags, FileDescriptor fd, long offset) throws ErrnoException;
msync(long address, long byteCount, int flags)130     public native void msync(long address, long byteCount, int flags) throws ErrnoException;
munlock(long address, long byteCount)131     public native void munlock(long address, long byteCount) throws ErrnoException;
munmap(long address, long byteCount)132     public native void munmap(long address, long byteCount) throws ErrnoException;
open(String path, int flags, int mode)133     public native FileDescriptor open(String path, int flags, int mode) throws ErrnoException;
pipe2(int flags)134     public native FileDescriptor[] pipe2(int flags) throws ErrnoException;
poll(StructPollfd[] fds, int timeoutMs)135     public native int poll(StructPollfd[] fds, int timeoutMs) throws ErrnoException;
posix_fallocate(FileDescriptor fd, long offset, long length)136     public native void posix_fallocate(FileDescriptor fd, long offset, long length) throws ErrnoException;
prctl(int option, long arg2, long arg3, long arg4, long arg5)137     public native int prctl(int option, long arg2, long arg3, long arg4, long arg5) throws ErrnoException;
pread(FileDescriptor fd, ByteBuffer buffer, long offset)138     public int pread(FileDescriptor fd, ByteBuffer buffer, long offset) throws ErrnoException, InterruptedIOException {
139         final int bytesRead;
140         final int position = buffer.position();
141 
142         if (buffer.isDirect()) {
143             bytesRead = preadBytes(fd, buffer, position, buffer.remaining(), offset);
144         } else {
145             bytesRead = preadBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining(), offset);
146         }
147 
148         maybeUpdateBufferPosition(buffer, position, bytesRead);
149         return bytesRead;
150     }
pread(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, long offset)151     public int pread(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, long offset) throws ErrnoException, InterruptedIOException {
152         // This indirection isn't strictly necessary, but ensures that our public interface is type safe.
153         return preadBytes(fd, bytes, byteOffset, byteCount, offset);
154     }
preadBytes(FileDescriptor fd, Object buffer, int bufferOffset, int byteCount, long offset)155     private native int preadBytes(FileDescriptor fd, Object buffer, int bufferOffset, int byteCount, long offset) throws ErrnoException, InterruptedIOException;
pwrite(FileDescriptor fd, ByteBuffer buffer, long offset)156     public int pwrite(FileDescriptor fd, ByteBuffer buffer, long offset) throws ErrnoException, InterruptedIOException {
157         final int bytesWritten;
158         final int position = buffer.position();
159 
160         if (buffer.isDirect()) {
161             bytesWritten = pwriteBytes(fd, buffer, position, buffer.remaining(), offset);
162         } else {
163             bytesWritten = pwriteBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining(), offset);
164         }
165 
166         maybeUpdateBufferPosition(buffer, position, bytesWritten);
167         return bytesWritten;
168     }
pwrite(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, long offset)169     public int pwrite(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, long offset) throws ErrnoException, InterruptedIOException {
170         // This indirection isn't strictly necessary, but ensures that our public interface is type safe.
171         return pwriteBytes(fd, bytes, byteOffset, byteCount, offset);
172     }
pwriteBytes(FileDescriptor fd, Object buffer, int bufferOffset, int byteCount, long offset)173     private native int pwriteBytes(FileDescriptor fd, Object buffer, int bufferOffset, int byteCount, long offset) throws ErrnoException, InterruptedIOException;
read(FileDescriptor fd, ByteBuffer buffer)174     public int read(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException, InterruptedIOException {
175         final int bytesRead;
176         final int position = buffer.position();
177 
178         if (buffer.isDirect()) {
179             bytesRead = readBytes(fd, buffer, position, buffer.remaining());
180         } else {
181             bytesRead = readBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining());
182         }
183 
184         maybeUpdateBufferPosition(buffer, position, bytesRead);
185         return bytesRead;
186     }
read(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount)187     public int read(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException, InterruptedIOException {
188         // This indirection isn't strictly necessary, but ensures that our public interface is type safe.
189         return readBytes(fd, bytes, byteOffset, byteCount);
190     }
readBytes(FileDescriptor fd, Object buffer, int offset, int byteCount)191     private native int readBytes(FileDescriptor fd, Object buffer, int offset, int byteCount) throws ErrnoException, InterruptedIOException;
readlink(String path)192     public native String readlink(String path) throws ErrnoException;
realpath(String path)193     public native String realpath(String path) throws ErrnoException;
readv(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts)194     public native int readv(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException, InterruptedIOException;
recvfrom(FileDescriptor fd, ByteBuffer buffer, int flags, InetSocketAddress srcAddress)195     public int recvfrom(FileDescriptor fd, ByteBuffer buffer, int flags, InetSocketAddress srcAddress) throws ErrnoException, SocketException {
196         final int bytesReceived;
197         final int position = buffer.position();
198 
199         if (buffer.isDirect()) {
200             bytesReceived = recvfromBytes(fd, buffer, position, buffer.remaining(), flags, srcAddress);
201         } else {
202             bytesReceived = recvfromBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining(), flags, srcAddress);
203         }
204 
205         maybeUpdateBufferPosition(buffer, position, bytesReceived);
206         return bytesReceived;
207     }
recvfrom(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetSocketAddress srcAddress)208     public int recvfrom(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetSocketAddress srcAddress) throws ErrnoException, SocketException {
209         // This indirection isn't strictly necessary, but ensures that our public interface is type safe.
210         return recvfromBytes(fd, bytes, byteOffset, byteCount, flags, srcAddress);
211     }
recvfromBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, InetSocketAddress srcAddress)212     private native int recvfromBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, InetSocketAddress srcAddress) throws ErrnoException, SocketException;
remove(String path)213     public native void remove(String path) throws ErrnoException;
removexattr(String path, String name)214     public native void removexattr(String path, String name) throws ErrnoException;
rename(String oldPath, String newPath)215     public native void rename(String oldPath, String newPath) throws ErrnoException;
sendfile(FileDescriptor outFd, FileDescriptor inFd, Int64Ref offset, long byteCount)216     public native long sendfile(FileDescriptor outFd, FileDescriptor inFd, Int64Ref offset, long byteCount) throws ErrnoException;
sendto(FileDescriptor fd, ByteBuffer buffer, int flags, InetAddress inetAddress, int port)217     public int sendto(FileDescriptor fd, ByteBuffer buffer, int flags, InetAddress inetAddress, int port) throws ErrnoException, SocketException {
218         final int bytesSent;
219         final int position = buffer.position();
220 
221         if (buffer.isDirect()) {
222             bytesSent = sendtoBytes(fd, buffer, position, buffer.remaining(), flags, inetAddress, port);
223         } else {
224             bytesSent = sendtoBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining(), flags, inetAddress, port);
225         }
226 
227         maybeUpdateBufferPosition(buffer, position, bytesSent);
228         return bytesSent;
229     }
sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port)230     public int sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port) throws ErrnoException, SocketException {
231         // This indirection isn't strictly necessary, but ensures that our public interface is type safe.
232         return sendtoBytes(fd, bytes, byteOffset, byteCount, flags, inetAddress, port);
233     }
sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, SocketAddress address)234     public int sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, SocketAddress address) throws ErrnoException, SocketException {
235         return sendtoBytes(fd, bytes, byteOffset, byteCount, flags, address);
236     }
sendtoBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port)237     private native int sendtoBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port) throws ErrnoException, SocketException;
sendtoBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, SocketAddress address)238     private native int sendtoBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, SocketAddress address) throws ErrnoException, SocketException;
setegid(int egid)239     public native void setegid(int egid) throws ErrnoException;
setenv(String name, String value, boolean overwrite)240     public native void setenv(String name, String value, boolean overwrite) throws ErrnoException;
seteuid(int euid)241     public native void seteuid(int euid) throws ErrnoException;
setgid(int gid)242     public native void setgid(int gid) throws ErrnoException;
setpgid(int pid, int pgid)243     public native void setpgid(int pid, int pgid) throws ErrnoException;
setregid(int rgid, int egid)244     public native void setregid(int rgid, int egid) throws ErrnoException;
setreuid(int ruid, int euid)245     public native void setreuid(int ruid, int euid) throws ErrnoException;
setsid()246     public native int setsid() throws ErrnoException;
setsockoptByte(FileDescriptor fd, int level, int option, int value)247     public native void setsockoptByte(FileDescriptor fd, int level, int option, int value) throws ErrnoException;
setsockoptIfreq(FileDescriptor fd, int level, int option, String value)248     public native void setsockoptIfreq(FileDescriptor fd, int level, int option, String value) throws ErrnoException;
setsockoptInt(FileDescriptor fd, int level, int option, int value)249     public native void setsockoptInt(FileDescriptor fd, int level, int option, int value) throws ErrnoException;
setsockoptIpMreqn(FileDescriptor fd, int level, int option, int value)250     public native void setsockoptIpMreqn(FileDescriptor fd, int level, int option, int value) throws ErrnoException;
setsockoptGroupReq(FileDescriptor fd, int level, int option, StructGroupReq value)251     public native void setsockoptGroupReq(FileDescriptor fd, int level, int option, StructGroupReq value) throws ErrnoException;
setsockoptLinger(FileDescriptor fd, int level, int option, StructLinger value)252     public native void setsockoptLinger(FileDescriptor fd, int level, int option, StructLinger value) throws ErrnoException;
setsockoptTimeval(FileDescriptor fd, int level, int option, StructTimeval value)253     public native void setsockoptTimeval(FileDescriptor fd, int level, int option, StructTimeval value) throws ErrnoException;
setuid(int uid)254     public native void setuid(int uid) throws ErrnoException;
setxattr(String path, String name, byte[] value, int flags)255     public native void setxattr(String path, String name, byte[] value, int flags) throws ErrnoException;
shutdown(FileDescriptor fd, int how)256     public native void shutdown(FileDescriptor fd, int how) throws ErrnoException;
socket(int domain, int type, int protocol)257     public native FileDescriptor socket(int domain, int type, int protocol) throws ErrnoException;
socketpair(int domain, int type, int protocol, FileDescriptor fd1, FileDescriptor fd2)258     public native void socketpair(int domain, int type, int protocol, FileDescriptor fd1, FileDescriptor fd2) throws ErrnoException;
splice(FileDescriptor fdIn, Int64Ref offIn, FileDescriptor fdOut, Int64Ref offOut, long len, int flags)259     public native long splice(FileDescriptor fdIn, Int64Ref offIn, FileDescriptor fdOut, Int64Ref offOut, long len, int flags) throws ErrnoException;
stat(String path)260     public native StructStat stat(String path) throws ErrnoException;
statvfs(String path)261     public native StructStatVfs statvfs(String path) throws ErrnoException;
strerror(int errno)262     public native String strerror(int errno);
strsignal(int signal)263     public native String strsignal(int signal);
symlink(String oldPath, String newPath)264     public native void symlink(String oldPath, String newPath) throws ErrnoException;
sysconf(int name)265     public native long sysconf(int name);
tcdrain(FileDescriptor fd)266     public native void tcdrain(FileDescriptor fd) throws ErrnoException;
tcsendbreak(FileDescriptor fd, int duration)267     public native void tcsendbreak(FileDescriptor fd, int duration) throws ErrnoException;
umask(int mask)268     public int umask(int mask) {
269         if ((mask & 0777) != mask) {
270             throw new IllegalArgumentException("Invalid umask: " + mask);
271         }
272         return umaskImpl(mask);
273     }
umaskImpl(int mask)274     private native int umaskImpl(int mask);
uname()275     public native StructUtsname uname();
unlink(String pathname)276     public native void unlink(String pathname) throws ErrnoException;
unsetenv(String name)277     public native void unsetenv(String name) throws ErrnoException;
waitpid(int pid, Int32Ref status, int options)278     public native int waitpid(int pid, Int32Ref status, int options) throws ErrnoException;
write(FileDescriptor fd, ByteBuffer buffer)279     public int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException, InterruptedIOException {
280         final int bytesWritten;
281         final int position = buffer.position();
282         if (buffer.isDirect()) {
283             bytesWritten = writeBytes(fd, buffer, position, buffer.remaining());
284         } else {
285             bytesWritten = writeBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining());
286         }
287 
288         maybeUpdateBufferPosition(buffer, position, bytesWritten);
289         return bytesWritten;
290     }
write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount)291     public int write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException, InterruptedIOException {
292         // This indirection isn't strictly necessary, but ensures that our public interface is type safe.
293         return writeBytes(fd, bytes, byteOffset, byteCount);
294     }
writeBytes(FileDescriptor fd, Object buffer, int offset, int byteCount)295     private native int writeBytes(FileDescriptor fd, Object buffer, int offset, int byteCount) throws ErrnoException, InterruptedIOException;
writev(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts)296     public native int writev(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException, InterruptedIOException;
297 
maybeUpdateBufferPosition(ByteBuffer buffer, int originalPosition, int bytesReadOrWritten)298     private static void maybeUpdateBufferPosition(ByteBuffer buffer, int originalPosition, int bytesReadOrWritten) {
299         if (bytesReadOrWritten > 0) {
300             buffer.position(bytesReadOrWritten + originalPosition);
301         }
302     }
303 }
304