Add sendfile(2).

This patch marks the end of OSFileSystem.

Having to support a Java "long*" for sendfile(2) gave me an opportunity to go
back and improve my ioctl(2) to use a corresponding "int*" equivalent, instead
of its previous special-case hack.

Bug: 3107501
Change-Id: I9fde4777700552263fab4fe9aeb556174163e3dc
diff --git a/dalvik/src/main/java/dalvik/system/BlockGuard.java b/dalvik/src/main/java/dalvik/system/BlockGuard.java
index 35386c6..3eaa2b6 100644
--- a/dalvik/src/main/java/dalvik/system/BlockGuard.java
+++ b/dalvik/src/main/java/dalvik/system/BlockGuard.java
@@ -24,7 +24,6 @@
 import java.net.SocketException;
 import java.net.SocketImpl;
 import java.net.SocketOptions;
-import org.apache.harmony.luni.platform.IFileSystem;
 import org.apache.harmony.luni.platform.INetworkSystem;
 
 /**
@@ -156,23 +155,6 @@
     private BlockGuard() {}
 
     /**
-     * A filesystem wrapper that calls the policy check functions
-     * on reads and writes.
-     */
-    public static class WrappedFileSystem implements IFileSystem {
-        private final IFileSystem mFileSystem;
-
-        public WrappedFileSystem(IFileSystem fileSystem) {
-            mFileSystem = fileSystem;
-        }
-
-        public long transfer(int fileHandler, FileDescriptor socketDescriptor,
-                             long offset, long count) throws IOException {
-            return mFileSystem.transfer(fileHandler, socketDescriptor, offset, count);
-        }
-    }
-
-    /**
      * A network wrapper that calls the policy check functions.
      */
     public static class WrappedNetworkSystem implements INetworkSystem {
diff --git a/luni/src/main/java/java/io/FileInputStream.java b/luni/src/main/java/java/io/FileInputStream.java
index 2c6ee2b..dfb16e8 100644
--- a/luni/src/main/java/java/io/FileInputStream.java
+++ b/luni/src/main/java/java/io/FileInputStream.java
@@ -26,8 +26,6 @@
 import libcore.io.IoUtils;
 import libcore.io.Libcore;
 import libcore.io.Streams;
-import org.apache.harmony.luni.platform.IFileSystem;
-import org.apache.harmony.luni.platform.Platform;
 import static libcore.io.OsConstants.*;
 
 /**
diff --git a/luni/src/main/java/java/io/FileOutputStream.java b/luni/src/main/java/java/io/FileOutputStream.java
index dd057a2..a000817 100644
--- a/luni/src/main/java/java/io/FileOutputStream.java
+++ b/luni/src/main/java/java/io/FileOutputStream.java
@@ -23,7 +23,6 @@
 import java.util.Arrays;
 import libcore.io.IoUtils;
 import static libcore.io.OsConstants.*;
-import org.apache.harmony.luni.platform.Platform;
 
 /**
  * An output stream that writes bytes to a file. If the output file exists, it
diff --git a/luni/src/main/java/java/io/RandomAccessFile.java b/luni/src/main/java/java/io/RandomAccessFile.java
index 994cde9..faf82f5 100644
--- a/luni/src/main/java/java/io/RandomAccessFile.java
+++ b/luni/src/main/java/java/io/RandomAccessFile.java
@@ -28,7 +28,6 @@
 import libcore.io.Libcore;
 import libcore.io.Memory;
 import libcore.io.SizeOf;
-import org.apache.harmony.luni.platform.Platform;
 import static libcore.io.OsConstants.*;
 
 /**
diff --git a/luni/src/main/java/java/net/SocketImpl.java b/luni/src/main/java/java/net/SocketImpl.java
index 938e71c..3d0507c 100644
--- a/luni/src/main/java/java/net/SocketImpl.java
+++ b/luni/src/main/java/java/net/SocketImpl.java
@@ -21,7 +21,6 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import org.apache.harmony.luni.platform.Platform;
 
 /**
  * This class is the base of all streaming socket implementation classes.
diff --git a/luni/src/main/java/java/nio/FileChannelImpl.java b/luni/src/main/java/java/nio/FileChannelImpl.java
index 2f580e3..acc3725 100644
--- a/luni/src/main/java/java/nio/FileChannelImpl.java
+++ b/luni/src/main/java/java/nio/FileChannelImpl.java
@@ -36,7 +36,7 @@
 import libcore.io.IoUtils;
 import libcore.io.Libcore;
 import libcore.io.StructFlock;
-import org.apache.harmony.luni.platform.Platform;
+import libcore.util.MutableLong;
 import static libcore.io.OsConstants.*;
 
 /**
@@ -404,13 +404,32 @@
         if (count == 0 || position >= size()) {
             return 0;
         }
-        ByteBuffer buffer = null;
         count = Math.min(count, size() - position);
-        if (target instanceof SocketChannelImpl) {
-            // only socket can be transfered by system call
-            return transferToSocket(fd, ((SocketChannelImpl) target).getFD(), position, count);
-        }
 
+        // Try sendfile(2) first...
+        boolean completed = false;
+        if (target instanceof SocketChannelImpl) {
+            FileDescriptor outFd = ((SocketChannelImpl) target).getFD();
+            try {
+                begin();
+                try {
+                    MutableLong offset = new MutableLong(position);
+                    long rc = Libcore.os.sendfile(outFd, fd, offset, count);
+                    completed = true;
+                    return rc;
+                } catch (ErrnoException errnoException) {
+                    // If the OS doesn't support what we asked for, we want to fall through and
+                    // try a different approach. If it does support it, but it failed, we're done.
+                    if (errnoException.errno != ENOSYS && errnoException.errno != EINVAL) {
+                        throw errnoException.rethrowAsIOException();
+                    }
+                }
+            } finally {
+                end(completed);
+            }
+        }
+        // ...fall back to write(2).
+        ByteBuffer buffer = null;
         try {
             buffer = map(MapMode.READ_ONLY, position, count);
             return target.write(buffer);
@@ -419,18 +438,6 @@
         }
     }
 
-    private long transferToSocket(FileDescriptor src, FileDescriptor dst, long position, long count) throws IOException {
-        boolean completed = false;
-        try {
-            begin();
-            long ret = Platform.FILE_SYSTEM.transfer(IoUtils.getFd(src), fd, position, count);
-            completed = true;
-            return ret;
-        } finally {
-            end(completed);
-        }
-    }
-
     public FileChannel truncate(long size) throws IOException {
         checkOpen();
         if (size < 0) {
diff --git a/luni/src/main/java/libcore/io/ForwardingOs.java b/luni/src/main/java/libcore/io/ForwardingOs.java
index 7950e2b..ffd95bb 100644
--- a/luni/src/main/java/libcore/io/ForwardingOs.java
+++ b/luni/src/main/java/libcore/io/ForwardingOs.java
@@ -18,6 +18,8 @@
 
 import java.io.FileDescriptor;
 import java.nio.ByteBuffer;
+import libcore.util.MutableInt;
+import libcore.util.MutableLong;
 
 /**
  * Subclass this if you want to override some {@link Os} methods but otherwise delegate.
@@ -41,7 +43,7 @@
     public void fsync(FileDescriptor fd) throws ErrnoException { os.fsync(fd); }
     public void ftruncate(FileDescriptor fd, long length) throws ErrnoException { os.ftruncate(fd, length); }
     public String getenv(String name) { return os.getenv(name); }
-    public int ioctlInt(FileDescriptor fd, int cmd, int arg) throws ErrnoException { return os.ioctlInt(fd, cmd, arg); }
+    public int ioctlInt(FileDescriptor fd, int cmd, MutableInt arg) throws ErrnoException { return os.ioctlInt(fd, cmd, arg); }
     public boolean isatty(FileDescriptor fd) { return os.isatty(fd); }
     public void listen(FileDescriptor fd, int backlog) throws ErrnoException { os.listen(fd, backlog); }
     public long lseek(FileDescriptor fd, long offset, int whence) throws ErrnoException { return os.lseek(fd, offset, whence); }
@@ -60,6 +62,7 @@
     public int readv(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException { return os.readv(fd, buffers, offsets, byteCounts); }
     public void remove(String path) throws ErrnoException { os.remove(path); }
     public void rename(String oldPath, String newPath) throws ErrnoException { os.rename(oldPath, newPath); }
+    public long sendfile(FileDescriptor outFd, FileDescriptor inFd, MutableLong inOffset, long byteCount) throws ErrnoException { return os.sendfile(outFd, inFd, inOffset, byteCount); }
     public void shutdown(FileDescriptor fd, int how) throws ErrnoException { os.shutdown(fd, how); }
     public StructStat stat(String path) throws ErrnoException { return os.stat(path); }
     public StructStatFs statfs(String path) throws ErrnoException { return os.statfs(path); }
diff --git a/luni/src/main/java/libcore/io/IoUtils.java b/luni/src/main/java/libcore/io/IoUtils.java
index a05c3e9..6170b3a 100644
--- a/luni/src/main/java/libcore/io/IoUtils.java
+++ b/luni/src/main/java/libcore/io/IoUtils.java
@@ -23,6 +23,7 @@
 import java.io.RandomAccessFile;
 import java.net.Socket;
 import java.util.Arrays;
+import libcore.util.MutableInt;
 import static libcore.io.OsConstants.*;
 
 public final class IoUtils {
@@ -34,17 +35,18 @@
      */
     public static int available(FileDescriptor fd) throws IOException {
         try {
-            int available = Libcore.os.ioctlInt(fd, FIONREAD, 0);
-            if (available < 0) {
+            MutableInt available = new MutableInt(0);
+            int rc = Libcore.os.ioctlInt(fd, FIONREAD, available);
+            if (available.value < 0) {
                 // If the fd refers to a regular file, the result is the difference between
                 // the file size and the file position. This may be negative if the position
                 // is past the end of the file. If the fd refers to a special file masquerading
                 // as a regular file, the result may be negative because the special file
                 // may appear to have zero size and yet a previous read call may have
                 // read some amount of data and caused the file position to be advanced.
-                available = 0;
+                available.value = 0;
             }
-            return available;
+            return available.value;
         } catch (ErrnoException errnoException) {
             if (errnoException.errno == ENOTTY) {
                 // The fd is unwilling to opine about its read buffer.
diff --git a/luni/src/main/java/libcore/io/Os.java b/luni/src/main/java/libcore/io/Os.java
index e1bb038..52051a3 100644
--- a/luni/src/main/java/libcore/io/Os.java
+++ b/luni/src/main/java/libcore/io/Os.java
@@ -18,6 +18,8 @@
 
 import java.io.FileDescriptor;
 import java.nio.ByteBuffer;
+import libcore.util.MutableInt;
+import libcore.util.MutableLong;
 
 public interface Os {
     public boolean access(String path, int mode) throws ErrnoException;
@@ -32,8 +34,7 @@
     public void fsync(FileDescriptor fd) throws ErrnoException;
     public void ftruncate(FileDescriptor fd, long length) throws ErrnoException;
     public String getenv(String name);
-    /* TODO: this is a questionable translation of int* ioctls in general. */
-    public int ioctlInt(FileDescriptor fd, int cmd, int arg) throws ErrnoException;
+    public int ioctlInt(FileDescriptor fd, int cmd, MutableInt arg) throws ErrnoException;
     public boolean isatty(FileDescriptor fd);
     public void listen(FileDescriptor fd, int backlog) throws ErrnoException;
     public long lseek(FileDescriptor fd, long offset, int whence) throws ErrnoException;
@@ -52,6 +53,7 @@
     public int readv(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException;
     public void remove(String path) throws ErrnoException;
     public void rename(String oldPath, String newPath) throws ErrnoException;
+    public long sendfile(FileDescriptor outFd, FileDescriptor inFd, MutableLong inOffset, long byteCount) throws ErrnoException;
     public void shutdown(FileDescriptor fd, int how) throws ErrnoException;
     public StructStat stat(String path) throws ErrnoException;
     /* TODO: replace statfs with statvfs. */
diff --git a/luni/src/main/java/libcore/io/Posix.java b/luni/src/main/java/libcore/io/Posix.java
index 8b5efb4..2affc34 100644
--- a/luni/src/main/java/libcore/io/Posix.java
+++ b/luni/src/main/java/libcore/io/Posix.java
@@ -19,6 +19,8 @@
 import java.io.FileDescriptor;
 import java.nio.ByteBuffer;
 import java.nio.NioUtils;
+import libcore.util.MutableInt;
+import libcore.util.MutableLong;
 
 public final class Posix implements Os {
     Posix() { }
@@ -35,7 +37,7 @@
     public native void fsync(FileDescriptor fd) throws ErrnoException;
     public native void ftruncate(FileDescriptor fd, long length) throws ErrnoException;
     public native String getenv(String name);
-    public native int ioctlInt(FileDescriptor fd, int cmd, int arg) throws ErrnoException;
+    public native int ioctlInt(FileDescriptor fd, int cmd, MutableInt arg) throws ErrnoException;
     public native boolean isatty(FileDescriptor fd);
     public native void listen(FileDescriptor fd, int backlog) throws ErrnoException;
     public native long lseek(FileDescriptor fd, long offset, int whence) throws ErrnoException;
@@ -60,6 +62,7 @@
     public native int readv(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException;
     public native void remove(String path) throws ErrnoException;
     public native void rename(String oldPath, String newPath) throws ErrnoException;
+    public native long sendfile(FileDescriptor outFd, FileDescriptor inFd, MutableLong inOffset, long byteCount) throws ErrnoException;
     public native void shutdown(FileDescriptor fd, int how) throws ErrnoException;
     public native StructStat stat(String path) throws ErrnoException;
     public native StructStatFs statfs(String path) throws ErrnoException;
diff --git a/luni/src/main/java/libcore/util/MutableBoolean.java b/luni/src/main/java/libcore/util/MutableBoolean.java
new file mode 100644
index 0000000..359a8f9
--- /dev/null
+++ b/luni/src/main/java/libcore/util/MutableBoolean.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package libcore.util;
+
+public final class MutableBoolean {
+    public boolean value;
+
+    public MutableBoolean(boolean value) {
+        this.value = value;
+    }
+}
diff --git a/luni/src/main/java/libcore/util/MutableByte.java b/luni/src/main/java/libcore/util/MutableByte.java
new file mode 100644
index 0000000..13f780b
--- /dev/null
+++ b/luni/src/main/java/libcore/util/MutableByte.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package libcore.util;
+
+public final class MutableByte {
+    public byte value;
+
+    public MutableByte(byte value) {
+        this.value = value;
+    }
+}
diff --git a/luni/src/main/java/libcore/util/MutableChar.java b/luni/src/main/java/libcore/util/MutableChar.java
new file mode 100644
index 0000000..1cafc3c
--- /dev/null
+++ b/luni/src/main/java/libcore/util/MutableChar.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package libcore.util;
+
+public final class MutableChar {
+    public char value;
+
+    public MutableChar(char value) {
+        this.value = value;
+    }
+}
diff --git a/luni/src/main/java/libcore/util/MutableDouble.java b/luni/src/main/java/libcore/util/MutableDouble.java
new file mode 100644
index 0000000..4473ae6
--- /dev/null
+++ b/luni/src/main/java/libcore/util/MutableDouble.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package libcore.util;
+
+public final class MutableDouble {
+    public double value;
+
+    public MutableDouble(double value) {
+        this.value = value;
+    }
+}
diff --git a/luni/src/main/java/libcore/util/MutableFloat.java b/luni/src/main/java/libcore/util/MutableFloat.java
new file mode 100644
index 0000000..f81fba5
--- /dev/null
+++ b/luni/src/main/java/libcore/util/MutableFloat.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package libcore.util;
+
+public final class MutableFloat {
+    public float value;
+
+    public MutableFloat(float value) {
+        this.value = value;
+    }
+}
diff --git a/luni/src/main/java/libcore/util/MutableInt.java b/luni/src/main/java/libcore/util/MutableInt.java
new file mode 100644
index 0000000..c8feb3a
--- /dev/null
+++ b/luni/src/main/java/libcore/util/MutableInt.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package libcore.util;
+
+public final class MutableInt {
+    public int value;
+
+    public MutableInt(int value) {
+        this.value = value;
+    }
+}
diff --git a/luni/src/main/java/libcore/util/MutableLong.java b/luni/src/main/java/libcore/util/MutableLong.java
new file mode 100644
index 0000000..ad9b78e
--- /dev/null
+++ b/luni/src/main/java/libcore/util/MutableLong.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package libcore.util;
+
+public final class MutableLong {
+    public long value;
+
+    public MutableLong(long value) {
+        this.value = value;
+    }
+}
diff --git a/luni/src/main/java/libcore/util/MutableShort.java b/luni/src/main/java/libcore/util/MutableShort.java
new file mode 100644
index 0000000..78b4c33
--- /dev/null
+++ b/luni/src/main/java/libcore/util/MutableShort.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package libcore.util;
+
+public final class MutableShort {
+    public short value;
+
+    public MutableShort(short value) {
+        this.value = value;
+    }
+}
diff --git a/luni/src/main/java/org/apache/harmony/luni/platform/IFileSystem.java b/luni/src/main/java/org/apache/harmony/luni/platform/IFileSystem.java
deleted file mode 100644
index 073f2e9..0000000
--- a/luni/src/main/java/org/apache/harmony/luni/platform/IFileSystem.java
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- *  Licensed to the Apache Software Foundation (ASF) under one or more
- *  contributor license agreements.  See the NOTICE file distributed with
- *  this work for additional information regarding copyright ownership.
- *  The ASF licenses this file to You under the Apache License, Version 2.0
- *  (the "License"); you may not use this file except in compliance with
- *  the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- */
-
-package org.apache.harmony.luni.platform;
-
-import java.io.FileDescriptor;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-
-public interface IFileSystem {
-    public long transfer(int fileHandler, FileDescriptor socketDescriptor,
-            long offset, long count) throws IOException;
-}
diff --git a/luni/src/main/java/org/apache/harmony/luni/platform/OSFileSystem.java b/luni/src/main/java/org/apache/harmony/luni/platform/OSFileSystem.java
deleted file mode 100644
index 31c0152..0000000
--- a/luni/src/main/java/org/apache/harmony/luni/platform/OSFileSystem.java
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- *  Licensed to the Apache Software Foundation (ASF) under one or more
- *  contributor license agreements.  See the NOTICE file distributed with
- *  this work for additional information regarding copyright ownership.
- *  The ASF licenses this file to You under the Apache License, Version 2.0
- *  (the "License"); you may not use this file except in compliance with
- *  the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- */
-
-package org.apache.harmony.luni.platform;
-
-import java.io.FileDescriptor;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import static libcore.io.OsConstants.*;
-
-class OSFileSystem implements IFileSystem {
-
-    private static final OSFileSystem singleton = new OSFileSystem();
-
-    public static OSFileSystem getOSFileSystem() {
-        return singleton;
-    }
-
-    private OSFileSystem() {
-    }
-
-    public native long transfer(int fd, FileDescriptor sd, long offset, long count)
-            throws IOException;
-}
diff --git a/luni/src/main/java/org/apache/harmony/luni/platform/Platform.java b/luni/src/main/java/org/apache/harmony/luni/platform/Platform.java
index fb9caf9..db79a59 100644
--- a/luni/src/main/java/org/apache/harmony/luni/platform/Platform.java
+++ b/luni/src/main/java/org/apache/harmony/luni/platform/Platform.java
@@ -31,7 +31,6 @@
  * exceptions in the runtime. Access to the OS components is restricted to
  * trusted code running on the system classpath.
  *
- * @see IFileSystem
  * @see INetworkSystem
  */
 public class Platform {
@@ -39,9 +38,6 @@
     // now they do because ThreadLocal lookups will be done on most operations, which
     // should be relatively less than the speed of the operation.
     // TODO: measure & fix if needed.
-    public static final IFileSystem FILE_SYSTEM =
-            new BlockGuard.WrappedFileSystem(OSFileSystem.getOSFileSystem());
-
     public static final INetworkSystem NETWORK =
             new BlockGuard.WrappedNetworkSystem(OSNetworkSystem.getOSNetworkSystem());
 }
diff --git a/luni/src/main/native/JniConstants.cpp b/luni/src/main/native/JniConstants.cpp
index 605a56e..004eb4c 100644
--- a/luni/src/main/native/JniConstants.cpp
+++ b/luni/src/main/native/JniConstants.cpp
@@ -40,6 +40,8 @@
 jclass JniConstants::longClass;
 jclass JniConstants::methodClass;
 jclass JniConstants::multicastGroupRequestClass;
+jclass JniConstants::mutableIntClass;
+jclass JniConstants::mutableLongClass;
 jclass JniConstants::parsePositionClass;
 jclass JniConstants::patternSyntaxExceptionClass;
 jclass JniConstants::realToStringClass;
@@ -84,6 +86,8 @@
     longClass = findClass(env, "java/lang/Long");
     methodClass = findClass(env, "java/lang/reflect/Method");
     multicastGroupRequestClass = findClass(env, "java/net/MulticastGroupRequest");
+    mutableIntClass = findClass(env, "libcore/util/MutableInt");
+    mutableLongClass = findClass(env, "libcore/util/MutableLong");
     parsePositionClass = findClass(env, "java/text/ParsePosition");
     patternSyntaxExceptionClass = findClass(env, "java/util/regex/PatternSyntaxException");
     realToStringClass = findClass(env, "java/lang/RealToString");
diff --git a/luni/src/main/native/JniConstants.h b/luni/src/main/native/JniConstants.h
index 364eb0a..aa78907 100644
--- a/luni/src/main/native/JniConstants.h
+++ b/luni/src/main/native/JniConstants.h
@@ -62,6 +62,8 @@
     static jclass longClass;
     static jclass methodClass;
     static jclass multicastGroupRequestClass;
+    static jclass mutableIntClass;
+    static jclass mutableLongClass;
     static jclass parsePositionClass;
     static jclass patternSyntaxExceptionClass;
     static jclass realToStringClass;
diff --git a/luni/src/main/native/Register.cpp b/luni/src/main/native/Register.cpp
index bf90ab3..d8521a5 100644
--- a/luni/src/main/native/Register.cpp
+++ b/luni/src/main/native/Register.cpp
@@ -57,7 +57,6 @@
 extern int register_libcore_io_Posix(JNIEnv* env);
 extern int register_libcore_net_RawSocket(JNIEnv* env);
 extern int register_org_apache_harmony_dalvik_NativeTestTarget(JNIEnv* env);
-extern int register_org_apache_harmony_luni_platform_OSFileSystem(JNIEnv* env);
 extern int register_org_apache_harmony_luni_platform_OSNetworkSystem(JNIEnv* env);
 extern int register_org_apache_harmony_luni_util_fltparse(JNIEnv* env);
 extern int register_org_apache_harmony_xml_ExpatParser(JNIEnv* env);
@@ -106,7 +105,6 @@
             register_libcore_io_Posix(env) != -1 &&
             register_libcore_net_RawSocket(env) != -1 &&
             register_org_apache_harmony_dalvik_NativeTestTarget(env) != -1 &&
-            register_org_apache_harmony_luni_platform_OSFileSystem(env) != -1 &&
             register_org_apache_harmony_luni_platform_OSNetworkSystem(env) != -1 &&
             register_org_apache_harmony_luni_util_fltparse(env) != -1 &&
             register_org_apache_harmony_xml_ExpatParser(env) != -1 &&
diff --git a/luni/src/main/native/libcore_io_Posix.cpp b/luni/src/main/native/libcore_io_Posix.cpp
index a3b5b67..8bbf6a4 100644
--- a/luni/src/main/native/libcore_io_Posix.cpp
+++ b/luni/src/main/native/libcore_io_Posix.cpp
@@ -29,6 +29,7 @@
 #include <stdlib.h>
 #include <sys/ioctl.h>
 #include <sys/mman.h>
+#include <sys/sendfile.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -307,10 +308,17 @@
     return env->NewStringUTF(getenv(name.c_str()));
 }
 
-static jint Posix_ioctlInt(JNIEnv* env, jobject, jobject javaFd, jint cmd, jint arg) {
+static jint Posix_ioctlInt(JNIEnv* env, jobject, jobject javaFd, jint cmd, jobject javaArg) {
+    // This is complicated because ioctls may return their result by updating their argument
+    // or via their return value, so we need to support both.
     int fd = jniGetFDFromFileDescriptor(env, javaFd);
-    throwIfMinusOne(env, "ioctl", TEMP_FAILURE_RETRY(ioctl(fd, cmd, &arg)));
-    return arg;
+    static jfieldID valueFid = env->GetFieldID(JniConstants::mutableIntClass, "value", "I");
+    jint arg = env->GetIntField(javaArg, valueFid);
+    int rc = throwIfMinusOne(env, "ioctl", TEMP_FAILURE_RETRY(ioctl(fd, cmd, &arg)));
+    if (!env->ExceptionCheck()) {
+        env->SetIntField(javaArg, valueFid, arg);
+    }
+    return rc;
 }
 
 static jboolean Posix_isatty(JNIEnv* env, jobject, jobject javaFd) {
@@ -453,6 +461,24 @@
     throwIfMinusOne(env, "rename", TEMP_FAILURE_RETRY(rename(oldPath.c_str(), newPath.c_str())));
 }
 
+static jlong Posix_sendfile(JNIEnv* env, jobject, jobject javaOutFd, jobject javaInFd, jobject javaOffset, jlong byteCount) {
+    int outFd = jniGetFDFromFileDescriptor(env, javaOutFd);
+    int inFd = jniGetFDFromFileDescriptor(env, javaInFd);
+    static jfieldID valueFid = env->GetFieldID(JniConstants::mutableLongClass, "value", "J");
+    off_t offset = 0;
+    off_t* offsetPtr = NULL;
+    if (javaOffset != NULL) {
+        // TODO: fix bionic so we can have a 64-bit off_t!
+        offset = env->GetLongField(javaOffset, valueFid);
+        offsetPtr = &offset;
+    }
+    jlong result = throwIfMinusOne(env, "sendfile", TEMP_FAILURE_RETRY(sendfile(outFd, inFd, offsetPtr, byteCount)));
+    if (javaOffset != NULL) {
+        env->SetLongField(javaOffset, valueFid, offset);
+    }
+    return result;
+}
+
 static void Posix_shutdown(JNIEnv* env, jobject, jobject javaFd, jint how) {
     int fd = jniGetFDFromFileDescriptor(env, javaFd);
     throwIfMinusOne(env, "shutdown", TEMP_FAILURE_RETRY(shutdown(fd, how)));
@@ -549,7 +575,7 @@
     NATIVE_METHOD(Posix, fsync, "(Ljava/io/FileDescriptor;)V"),
     NATIVE_METHOD(Posix, ftruncate, "(Ljava/io/FileDescriptor;J)V"),
     NATIVE_METHOD(Posix, getenv, "(Ljava/lang/String;)Ljava/lang/String;"),
-    NATIVE_METHOD(Posix, ioctlInt, "(Ljava/io/FileDescriptor;II)I"),
+    NATIVE_METHOD(Posix, ioctlInt, "(Ljava/io/FileDescriptor;ILlibcore/util/MutableInt;)I"),
     NATIVE_METHOD(Posix, isatty, "(Ljava/io/FileDescriptor;)Z"),
     NATIVE_METHOD(Posix, listen, "(Ljava/io/FileDescriptor;I)V"),
     NATIVE_METHOD(Posix, lseek, "(Ljava/io/FileDescriptor;JI)J"),
@@ -568,6 +594,7 @@
     NATIVE_METHOD(Posix, readv, "(Ljava/io/FileDescriptor;[Ljava/lang/Object;[I[I)I"),
     NATIVE_METHOD(Posix, remove, "(Ljava/lang/String;)V"),
     NATIVE_METHOD(Posix, rename, "(Ljava/lang/String;Ljava/lang/String;)V"),
+    NATIVE_METHOD(Posix, sendfile, "(Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Llibcore/util/MutableLong;J)J"),
     NATIVE_METHOD(Posix, shutdown, "(Ljava/io/FileDescriptor;I)V"),
     NATIVE_METHOD(Posix, stat, "(Ljava/lang/String;)Llibcore/io/StructStat;"),
     NATIVE_METHOD(Posix, statfs, "(Ljava/lang/String;)Llibcore/io/StructStatFs;"),
diff --git a/luni/src/main/native/org_apache_harmony_luni_platform_OSFileSystem.cpp b/luni/src/main/native/org_apache_harmony_luni_platform_OSFileSystem.cpp
deleted file mode 100644
index 3baceb8..0000000
--- a/luni/src/main/native/org_apache_harmony_luni_platform_OSFileSystem.cpp
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- *  Licensed to the Apache Software Foundation (ASF) under one or more
- *  contributor license agreements.  See the NOTICE file distributed with
- *  this work for additional information regarding copyright ownership.
- *  The ASF licenses this file to You under the Apache License, Version 2.0
- *  (the "License"); you may not use this file except in compliance with
- *  the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- */
-
-#define LOG_TAG "OSFileSystem"
-
-#include "JNIHelp.h"
-#include "JniConstants.h"
-#include "JniException.h"
-#include "ScopedPrimitiveArray.h"
-#include "ScopedUtfChars.h"
-#include "UniquePtr.h"
-
-#include <errno.h>
-#include <fcntl.h>
-#include <limits.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <sys/uio.h>
-#include <unistd.h>
-
-#if HAVE_SYS_SENDFILE_H
-#include <sys/sendfile.h>
-#else
-/*
- * Define a small adapter function: sendfile() isn't part of a standard,
- * and its definition differs between Linux, BSD, and OS X. This version
- * works for OS X but will probably not work on other BSDish systems.
- * Note: We rely on function overloading here to define a same-named
- * function with different arguments.
- */
-#include <sys/socket.h>
-#include <sys/types.h>
-static inline ssize_t sendfile(int out_fd, int in_fd, off_t* offset, size_t count) {
-    off_t len = count;
-    int result = sendfile(in_fd, out_fd, *offset, &len, NULL, 0);
-    if (result < 0) {
-        return -1;
-    }
-    return len;
-}
-#endif
-
-static jlong OSFileSystem_transfer(JNIEnv* env, jobject, jint fd, jobject sd,
-        jlong offset, jlong count) {
-
-    int socket = jniGetFDFromFileDescriptor(env, sd);
-    if (socket == -1) {
-        return -1;
-    }
-
-    /* Value of offset is checked in jint scope (checked in java layer)
-       The conversion here is to guarantee no value lost when converting offset to off_t
-     */
-    off_t off = offset;
-
-    ssize_t rc = sendfile(socket, fd, &off, count);
-    if (rc == -1) {
-        jniThrowIOException(env, errno);
-    }
-    return rc;
-}
-
-static JNINativeMethod gMethods[] = {
-    NATIVE_METHOD(OSFileSystem, transfer, "(ILjava/io/FileDescriptor;JJ)J"),
-};
-int register_org_apache_harmony_luni_platform_OSFileSystem(JNIEnv* env) {
-    return jniRegisterNativeMethods(env, "org/apache/harmony/luni/platform/OSFileSystem", gMethods,
-            NELEM(gMethods));
-}
diff --git a/luni/src/main/native/sub.mk b/luni/src/main/native/sub.mk
index cee0e16..ea8e715 100644
--- a/luni/src/main/native/sub.mk
+++ b/luni/src/main/native/sub.mk
@@ -47,7 +47,6 @@
 	libcore_io_OsConstants.cpp \
 	libcore_io_Posix.cpp \
 	libcore_net_RawSocket.cpp \
-	org_apache_harmony_luni_platform_OSFileSystem.cpp \
 	org_apache_harmony_luni_platform_OSNetworkSystem.cpp \
 	org_apache_harmony_luni_util_FloatingPointParser.cpp \
 	org_apache_harmony_xml_ExpatParser.cpp \