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.StructMsghdr; 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; fcntlInt(FileDescriptor fd, int cmd, int arg)79 public native int fcntlInt(FileDescriptor fd, int cmd, int arg) throws ErrnoException; fcntlVoid(FileDescriptor fd, int cmd)80 public native int fcntlVoid(FileDescriptor fd, int cmd) throws ErrnoException; fdatasync(FileDescriptor fd)81 public native void fdatasync(FileDescriptor fd) throws ErrnoException; fstat(FileDescriptor fd)82 public native StructStat fstat(FileDescriptor fd) throws ErrnoException; fstatvfs(FileDescriptor fd)83 public native StructStatVfs fstatvfs(FileDescriptor fd) throws ErrnoException; fsync(FileDescriptor fd)84 public native void fsync(FileDescriptor fd) throws ErrnoException; ftruncate(FileDescriptor fd, long length)85 public native void ftruncate(FileDescriptor fd, long length) throws ErrnoException; gai_strerror(int error)86 public native String gai_strerror(int error); getegid()87 public native int getegid(); geteuid()88 public native int geteuid(); getgid()89 public native int getgid(); getenv(String name)90 public native String getenv(String name); getnameinfo(InetAddress address, int flags)91 public native String getnameinfo(InetAddress address, int flags) throws GaiException; getpeername(FileDescriptor fd)92 public native SocketAddress getpeername(FileDescriptor fd) throws ErrnoException; getpgid(int pid)93 public native int getpgid(int pid); getpid()94 public native int getpid(); getppid()95 public native int getppid(); getpwnam(String name)96 public native StructPasswd getpwnam(String name) throws ErrnoException; getpwuid(int uid)97 public native StructPasswd getpwuid(int uid) throws ErrnoException; getrlimit(int resource)98 public native StructRlimit getrlimit(int resource) throws ErrnoException; getsockname(FileDescriptor fd)99 public native SocketAddress getsockname(FileDescriptor fd) throws ErrnoException; getsockoptByte(FileDescriptor fd, int level, int option)100 public native int getsockoptByte(FileDescriptor fd, int level, int option) throws ErrnoException; getsockoptInAddr(FileDescriptor fd, int level, int option)101 public native InetAddress getsockoptInAddr(FileDescriptor fd, int level, int option) throws ErrnoException; getsockoptInt(FileDescriptor fd, int level, int option)102 public native int getsockoptInt(FileDescriptor fd, int level, int option) throws ErrnoException; getsockoptLinger(FileDescriptor fd, int level, int option)103 public native StructLinger getsockoptLinger(FileDescriptor fd, int level, int option) throws ErrnoException; getsockoptTimeval(FileDescriptor fd, int level, int option)104 public native StructTimeval getsockoptTimeval(FileDescriptor fd, int level, int option) throws ErrnoException; getsockoptUcred(FileDescriptor fd, int level, int option)105 public native StructUcred getsockoptUcred(FileDescriptor fd, int level, int option) throws ErrnoException; gettid()106 public native int gettid(); getuid()107 public native int getuid(); getxattr(String path, String name)108 public native byte[] getxattr(String path, String name) throws ErrnoException; getifaddrs()109 public native StructIfaddrs[] getifaddrs() throws ErrnoException; if_indextoname(int index)110 public native String if_indextoname(int index); if_nametoindex(String name)111 public native int if_nametoindex(String name); inet_pton(int family, String address)112 public native InetAddress inet_pton(int family, String address); ioctlFlags(FileDescriptor fd, String interfaceName)113 public native int ioctlFlags(FileDescriptor fd, String interfaceName) throws ErrnoException; ioctlInetAddress(FileDescriptor fd, int cmd, String interfaceName)114 public native InetAddress ioctlInetAddress(FileDescriptor fd, int cmd, String interfaceName) throws ErrnoException; ioctlInt(FileDescriptor fd, int cmd)115 public native int ioctlInt(FileDescriptor fd, int cmd) throws ErrnoException; ioctlMTU(FileDescriptor fd, String interfaceName)116 public native int ioctlMTU(FileDescriptor fd, String interfaceName) throws ErrnoException; isatty(FileDescriptor fd)117 public native boolean isatty(FileDescriptor fd); kill(int pid, int signal)118 public native void kill(int pid, int signal) throws ErrnoException; lchown(String path, int uid, int gid)119 public native void lchown(String path, int uid, int gid) throws ErrnoException; link(String oldPath, String newPath)120 public native void link(String oldPath, String newPath) throws ErrnoException; listen(FileDescriptor fd, int backlog)121 public native void listen(FileDescriptor fd, int backlog) throws ErrnoException; listxattr(String path)122 public native String[] listxattr(String path) throws ErrnoException; lseek(FileDescriptor fd, long offset, int whence)123 public native long lseek(FileDescriptor fd, long offset, int whence) throws ErrnoException; lstat(String path)124 public native StructStat lstat(String path) throws ErrnoException; memfd_create(String name, int flags)125 public native FileDescriptor memfd_create(String name, int flags) 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; recvmsg(FileDescriptor fd, StructMsghdr msg, int flags)214 public native int recvmsg(FileDescriptor fd, StructMsghdr msg, int flags) throws ErrnoException, SocketException; remove(String path)215 public native void remove(String path) throws ErrnoException; removexattr(String path, String name)216 public native void removexattr(String path, String name) throws ErrnoException; rename(String oldPath, String newPath)217 public native void rename(String oldPath, String newPath) throws ErrnoException; sendfile(FileDescriptor outFd, FileDescriptor inFd, Int64Ref offset, long byteCount)218 public native long sendfile(FileDescriptor outFd, FileDescriptor inFd, Int64Ref offset, long byteCount) throws ErrnoException; sendmsg(FileDescriptor fd, StructMsghdr msg, int flags)219 public native int sendmsg(FileDescriptor fd, StructMsghdr msg, int flags) throws ErrnoException, SocketException; sendto(FileDescriptor fd, ByteBuffer buffer, int flags, InetAddress inetAddress, int port)220 public int sendto(FileDescriptor fd, ByteBuffer buffer, int flags, InetAddress inetAddress, int port) throws ErrnoException, SocketException { 221 final int bytesSent; 222 final int position = buffer.position(); 223 224 if (buffer.isDirect()) { 225 bytesSent = sendtoBytes(fd, buffer, position, buffer.remaining(), flags, inetAddress, port); 226 } else { 227 bytesSent = sendtoBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining(), flags, inetAddress, port); 228 } 229 230 maybeUpdateBufferPosition(buffer, position, bytesSent); 231 return bytesSent; 232 } sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port)233 public int sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port) throws ErrnoException, SocketException { 234 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 235 return sendtoBytes(fd, bytes, byteOffset, byteCount, flags, inetAddress, port); 236 } sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, SocketAddress address)237 public int sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, SocketAddress address) throws ErrnoException, SocketException { 238 return sendtoBytes(fd, bytes, byteOffset, byteCount, flags, address); 239 } sendtoBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port)240 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)241 private native int sendtoBytes(FileDescriptor fd, Object buffer, int byteOffset, int byteCount, int flags, SocketAddress address) throws ErrnoException, SocketException; setegid(int egid)242 public native void setegid(int egid) throws ErrnoException; setenv(String name, String value, boolean overwrite)243 public native void setenv(String name, String value, boolean overwrite) throws ErrnoException; seteuid(int euid)244 public native void seteuid(int euid) throws ErrnoException; setgid(int gid)245 public native void setgid(int gid) throws ErrnoException; setpgid(int pid, int pgid)246 public native void setpgid(int pid, int pgid) throws ErrnoException; setregid(int rgid, int egid)247 public native void setregid(int rgid, int egid) throws ErrnoException; setreuid(int ruid, int euid)248 public native void setreuid(int ruid, int euid) throws ErrnoException; setsid()249 public native int setsid() throws ErrnoException; setsockoptByte(FileDescriptor fd, int level, int option, int value)250 public native void setsockoptByte(FileDescriptor fd, int level, int option, int value) throws ErrnoException; setsockoptIfreq(FileDescriptor fd, int level, int option, String value)251 public native void setsockoptIfreq(FileDescriptor fd, int level, int option, String value) throws ErrnoException; setsockoptInt(FileDescriptor fd, int level, int option, int value)252 public native void setsockoptInt(FileDescriptor fd, int level, int option, int value) throws ErrnoException; setsockoptIpMreqn(FileDescriptor fd, int level, int option, int value)253 public native void setsockoptIpMreqn(FileDescriptor fd, int level, int option, int value) throws ErrnoException; setsockoptGroupReq(FileDescriptor fd, int level, int option, StructGroupReq value)254 public native void setsockoptGroupReq(FileDescriptor fd, int level, int option, StructGroupReq value) throws ErrnoException; setsockoptLinger(FileDescriptor fd, int level, int option, StructLinger value)255 public native void setsockoptLinger(FileDescriptor fd, int level, int option, StructLinger value) throws ErrnoException; setsockoptTimeval(FileDescriptor fd, int level, int option, StructTimeval value)256 public native void setsockoptTimeval(FileDescriptor fd, int level, int option, StructTimeval value) throws ErrnoException; setuid(int uid)257 public native void setuid(int uid) throws ErrnoException; setxattr(String path, String name, byte[] value, int flags)258 public native void setxattr(String path, String name, byte[] value, int flags) throws ErrnoException; shutdown(FileDescriptor fd, int how)259 public native void shutdown(FileDescriptor fd, int how) throws ErrnoException; socket(int domain, int type, int protocol)260 public native FileDescriptor socket(int domain, int type, int protocol) throws ErrnoException; socketpair(int domain, int type, int protocol, FileDescriptor fd1, FileDescriptor fd2)261 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)262 public native long splice(FileDescriptor fdIn, Int64Ref offIn, FileDescriptor fdOut, Int64Ref offOut, long len, int flags) throws ErrnoException; stat(String path)263 public native StructStat stat(String path) throws ErrnoException; statvfs(String path)264 public native StructStatVfs statvfs(String path) throws ErrnoException; strerror(int errno)265 public native String strerror(int errno); strsignal(int signal)266 public native String strsignal(int signal); symlink(String oldPath, String newPath)267 public native void symlink(String oldPath, String newPath) throws ErrnoException; sysconf(int name)268 public native long sysconf(int name); tcdrain(FileDescriptor fd)269 public native void tcdrain(FileDescriptor fd) throws ErrnoException; tcsendbreak(FileDescriptor fd, int duration)270 public native void tcsendbreak(FileDescriptor fd, int duration) throws ErrnoException; umask(int mask)271 public int umask(int mask) { 272 if ((mask & 0777) != mask) { 273 throw new IllegalArgumentException("Invalid umask: " + mask); 274 } 275 return umaskImpl(mask); 276 } umaskImpl(int mask)277 private native int umaskImpl(int mask); uname()278 public native StructUtsname uname(); unlink(String pathname)279 public native void unlink(String pathname) throws ErrnoException; unsetenv(String name)280 public native void unsetenv(String name) throws ErrnoException; waitpid(int pid, Int32Ref status, int options)281 public native int waitpid(int pid, Int32Ref status, int options) throws ErrnoException; write(FileDescriptor fd, ByteBuffer buffer)282 public int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException, InterruptedIOException { 283 final int bytesWritten; 284 final int position = buffer.position(); 285 if (buffer.isDirect()) { 286 bytesWritten = writeBytes(fd, buffer, position, buffer.remaining()); 287 } else { 288 bytesWritten = writeBytes(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + position, buffer.remaining()); 289 } 290 291 maybeUpdateBufferPosition(buffer, position, bytesWritten); 292 return bytesWritten; 293 } write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount)294 public int write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException, InterruptedIOException { 295 // This indirection isn't strictly necessary, but ensures that our public interface is type safe. 296 return writeBytes(fd, bytes, byteOffset, byteCount); 297 } writeBytes(FileDescriptor fd, Object buffer, int offset, int byteCount)298 private native int writeBytes(FileDescriptor fd, Object buffer, int offset, int byteCount) throws ErrnoException, InterruptedIOException; writev(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts)299 public native int writev(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException, InterruptedIOException; 300 maybeUpdateBufferPosition(ByteBuffer buffer, int originalPosition, int bytesReadOrWritten)301 private static void maybeUpdateBufferPosition(ByteBuffer buffer, int originalPosition, int bytesReadOrWritten) { 302 if (bytesReadOrWritten > 0) { 303 buffer.position(bytesReadOrWritten + originalPosition); 304 } 305 } 306 } 307