Posix.java revision 462bdac45c10f43d88d8f07f6994e272a27c14a2
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 17package libcore.io; 18 19import java.io.FileDescriptor; 20import java.nio.ByteBuffer; 21import java.nio.NioUtils; 22import libcore.util.MutableInt; 23import libcore.util.MutableLong; 24 25public final class Posix implements Os { 26 Posix() { } 27 28 public native boolean access(String path, int mode) throws ErrnoException; 29 public native void chmod(String path, int mode) throws ErrnoException; 30 public native void close(FileDescriptor fd) throws ErrnoException; 31 public native String[] environ(); 32 public native int fcntlVoid(FileDescriptor fd, int cmd) throws ErrnoException; 33 public native int fcntlLong(FileDescriptor fd, int cmd, long arg) throws ErrnoException; 34 public native int fcntlFlock(FileDescriptor fd, int cmd, StructFlock arg) throws ErrnoException; 35 public native void fdatasync(FileDescriptor fd) throws ErrnoException; 36 public native StructStat fstat(FileDescriptor fd) throws ErrnoException; 37 public native StructStatFs fstatfs(FileDescriptor fd) throws ErrnoException; 38 public native void fsync(FileDescriptor fd) throws ErrnoException; 39 public native void ftruncate(FileDescriptor fd, long length) throws ErrnoException; 40 public native String getenv(String name); 41 public native int ioctlInt(FileDescriptor fd, int cmd, MutableInt arg) throws ErrnoException; 42 public native boolean isatty(FileDescriptor fd); 43 public native void listen(FileDescriptor fd, int backlog) throws ErrnoException; 44 public native long lseek(FileDescriptor fd, long offset, int whence) throws ErrnoException; 45 public native void mincore(long address, long byteCount, byte[] vector) throws ErrnoException; 46 public native void mkdir(String path, int mode) throws ErrnoException; 47 public native void mlock(long address, long byteCount) throws ErrnoException; 48 public native long mmap(long address, long byteCount, int prot, int flags, FileDescriptor fd, long offset) throws ErrnoException; 49 public native void msync(long address, long byteCount, int flags) throws ErrnoException; 50 public native void munlock(long address, long byteCount) throws ErrnoException; 51 public native void munmap(long address, long byteCount) throws ErrnoException; 52 public native FileDescriptor open(String path, int flags, int mode) throws ErrnoException; 53 public native FileDescriptor[] pipe() throws ErrnoException; 54 public native StructStat lstat(String path) throws ErrnoException; 55 public int read(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException { 56 if (buffer.isDirect()) { 57 return readDirectBuffer(fd, buffer, buffer.position(), buffer.remaining()); 58 } 59 return read(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + buffer.position(), buffer.remaining()); 60 } 61 private native int readDirectBuffer(FileDescriptor fd, ByteBuffer buffer, int position, int remaining) throws ErrnoException; 62 public native int read(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException; 63 public native int readv(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException; 64 public native void remove(String path) throws ErrnoException; 65 public native void rename(String oldPath, String newPath) throws ErrnoException; 66 public native long sendfile(FileDescriptor outFd, FileDescriptor inFd, MutableLong inOffset, long byteCount) throws ErrnoException; 67 public native void setsockoptInt(FileDescriptor fd, int level, int option, int value) throws ErrnoException; 68 public native void shutdown(FileDescriptor fd, int how) throws ErrnoException; 69 public native FileDescriptor socket(int domain, int type, int protocol) throws ErrnoException; 70 public native StructStat stat(String path) throws ErrnoException; 71 public native StructStatFs statfs(String path) throws ErrnoException; 72 public native String strerror(int errno); 73 public native void symlink(String oldPath, String newPath) throws ErrnoException; 74 public native long sysconf(int name); 75 public native StructUtsname uname() throws ErrnoException; 76 public int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException { 77 if (buffer.isDirect()) { 78 return writeDirectBuffer(fd, buffer, buffer.position(), buffer.remaining()); 79 } 80 return write(fd, NioUtils.unsafeArray(buffer), NioUtils.unsafeArrayOffset(buffer) + buffer.position(), buffer.remaining()); 81 } 82 private native int writeDirectBuffer(FileDescriptor fd, ByteBuffer buffer, int position, int remaining) throws ErrnoException; 83 public native int write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException; 84 public native int writev(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException; 85} 86