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