diff options
-rw-r--r-- | dalvik/src/main/java/dalvik/system/BlockGuard.java | 12 | ||||
-rw-r--r-- | luni/src/main/java/java/io/FileInputStream.java | 16 | ||||
-rw-r--r-- | luni/src/main/java/java/io/FileOutputStream.java | 15 | ||||
-rw-r--r-- | luni/src/main/java/java/io/RandomAccessFile.java | 47 | ||||
-rw-r--r-- | luni/src/main/java/java/nio/FileChannelImpl.java | 32 | ||||
-rw-r--r-- | luni/src/main/java/java/nio/SelectorImpl.java | 4 | ||||
-rw-r--r-- | luni/src/main/java/libcore/io/BlockGuardOs.java | 10 | ||||
-rw-r--r-- | luni/src/main/java/libcore/io/ForwardingOs.java | 2 | ||||
-rw-r--r-- | luni/src/main/java/libcore/io/IoUtils.java | 25 | ||||
-rw-r--r-- | luni/src/main/java/libcore/io/Os.java | 2 | ||||
-rw-r--r-- | luni/src/main/java/libcore/io/Posix.java | 11 | ||||
-rw-r--r-- | luni/src/main/java/org/apache/harmony/luni/platform/IFileSystem.java | 6 | ||||
-rw-r--r-- | luni/src/main/java/org/apache/harmony/luni/platform/OSFileSystem.java | 4 | ||||
-rw-r--r-- | luni/src/main/native/libcore_io_Posix.cpp | 17 | ||||
-rw-r--r-- | luni/src/main/native/org_apache_harmony_luni_platform_OSFileSystem.cpp | 25 |
15 files changed, 89 insertions, 139 deletions
diff --git a/dalvik/src/main/java/dalvik/system/BlockGuard.java b/dalvik/src/main/java/dalvik/system/BlockGuard.java index a0c0d25..136bdc1 100644 --- a/dalvik/src/main/java/dalvik/system/BlockGuard.java +++ b/dalvik/src/main/java/dalvik/system/BlockGuard.java @@ -166,12 +166,6 @@ public final class BlockGuard { mFileSystem = fileSystem; } - public long write(int fileDescriptor, byte[] bytes, int offset, int length) - throws IOException { - BlockGuard.getThreadPolicy().onWriteToDisk(); - return mFileSystem.write(fileDescriptor, bytes, offset, length); - } - public long readv(int fileDescriptor, int[] addresses, int[] offsets, int[] lengths, int size) throws IOException { BlockGuard.getThreadPolicy().onReadFromDisk(); @@ -184,12 +178,6 @@ public final class BlockGuard { return mFileSystem.writev(fileDescriptor, addresses, offsets, lengths, size); } - public long writeDirect(int fileDescriptor, int address, int offset, - int length) throws IOException { - BlockGuard.getThreadPolicy().onWriteToDisk(); - return mFileSystem.writeDirect(fileDescriptor, address, offset, length); - } - public long transfer(int fileHandler, FileDescriptor socketDescriptor, long offset, long count) throws IOException { return mFileSystem.transfer(fileHandler, socketDescriptor, offset, count); diff --git a/luni/src/main/java/java/io/FileInputStream.java b/luni/src/main/java/java/io/FileInputStream.java index 3cff39a..dbdf0ca 100644 --- a/luni/src/main/java/java/io/FileInputStream.java +++ b/luni/src/main/java/java/io/FileInputStream.java @@ -109,7 +109,9 @@ public class FileInputStream extends InputStream implements Closeable { @Override public int available() throws IOException { - checkOpen(); + if (!fd.valid()) { + throw new IOException("stream is closed"); + } return Platform.FILE_SYSTEM.ioctlAvailable(fd); } @@ -175,17 +177,11 @@ public class FileInputStream extends InputStream implements Closeable { } @Override public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException { - Arrays.checkOffsetAndCount(buffer.length, byteOffset, byteCount); - if (byteCount == 0) { - return 0; - } - checkOpen(); return IoUtils.read(fd, buffer, byteOffset, byteCount); } @Override public long skip(long byteCount) throws IOException { - checkOpen(); if (byteCount == 0) { return 0; } @@ -205,10 +201,4 @@ public class FileInputStream extends InputStream implements Closeable { throw errnoException.rethrowAsIOException(); } } - - private synchronized void checkOpen() throws IOException { - if (!fd.valid()) { - throw new IOException("stream is closed"); - } - } } diff --git a/luni/src/main/java/java/io/FileOutputStream.java b/luni/src/main/java/java/io/FileOutputStream.java index d700326..dd057a2 100644 --- a/luni/src/main/java/java/io/FileOutputStream.java +++ b/luni/src/main/java/java/io/FileOutputStream.java @@ -173,23 +173,12 @@ public class FileOutputStream extends OutputStream implements Closeable { } @Override - public void write(byte[] buffer, int offset, int byteCount) throws IOException { - Arrays.checkOffsetAndCount(buffer.length, offset, byteCount); - if (byteCount == 0) { - return; - } - checkOpen(); - Platform.FILE_SYSTEM.write(fd.descriptor, buffer, offset, byteCount); + public void write(byte[] buffer, int byteOffset, int byteCount) throws IOException { + IoUtils.write(fd, buffer, byteOffset, byteCount); } @Override public void write(int oneByte) throws IOException { write(new byte[] { (byte) oneByte }, 0, 1); } - - private synchronized void checkOpen() throws IOException { - if (!fd.valid()) { - throw new IOException("stream is closed"); - } - } } diff --git a/luni/src/main/java/java/io/RandomAccessFile.java b/luni/src/main/java/java/io/RandomAccessFile.java index 630feb1..994cde9 100644 --- a/luni/src/main/java/java/io/RandomAccessFile.java +++ b/luni/src/main/java/java/io/RandomAccessFile.java @@ -221,7 +221,6 @@ public class RandomAccessFile implements DataInput, DataOutput, Closeable { * file. */ public long getFilePointer() throws IOException { - openCheck(); try { return Libcore.os.lseek(fd, 0L, SEEK_CUR); } catch (ErrnoException errnoException) { @@ -230,19 +229,6 @@ public class RandomAccessFile implements DataInput, DataOutput, Closeable { } /** - * Checks to see if the file is currently open. Returns silently if it is, - * and throws an exception if it is not. - * - * @throws IOException - * the receiver is closed. - */ - private synchronized void openCheck() throws IOException { - if (fd.descriptor < 0) { - throw new IOException(); - } - } - - /** * Returns the length of this file in bytes. * * @return the file's length in bytes. @@ -250,7 +236,6 @@ public class RandomAccessFile implements DataInput, DataOutput, Closeable { * if this file is closed or some other I/O error occurs. */ public long length() throws IOException { - openCheck(); try { return Libcore.os.fstat(fd).st_size; } catch (ErrnoException errnoException) { @@ -269,7 +254,6 @@ public class RandomAccessFile implements DataInput, DataOutput, Closeable { * if this file is closed or another I/O error occurs. */ public int read() throws IOException { - openCheck(); return (read(scratch, 0, 1) != -1) ? scratch[0] & 0xff : -1; } @@ -305,11 +289,6 @@ public class RandomAccessFile implements DataInput, DataOutput, Closeable { * if this file is closed or another I/O error occurs. */ public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException { - Arrays.checkOffsetAndCount(buffer.length, byteOffset, byteCount); - if (byteCount == 0) { - return 0; - } - openCheck(); return IoUtils.read(fd, buffer, byteOffset, byteCount); } @@ -623,7 +602,6 @@ public class RandomAccessFile implements DataInput, DataOutput, Closeable { if (offset < 0) { throw new IOException("offset < 0: " + offset); } - openCheck(); try { Libcore.os.lseek(fd, offset, SEEK_SET); } catch (ErrnoException errnoException) { @@ -646,7 +624,6 @@ public class RandomAccessFile implements DataInput, DataOutput, Closeable { * if this file is closed or another I/O error occurs. */ public void setLength(long newLength) throws IOException { - openCheck(); if (newLength < 0) { throw new IllegalArgumentException("newLength < 0"); } @@ -703,30 +680,18 @@ public class RandomAccessFile implements DataInput, DataOutput, Closeable { } /** - * Writes {@code count} bytes from the byte array {@code buffer} to this - * file, starting at the current file pointer and using {@code offset} as + * Writes {@code byteCount} bytes from the byte array {@code buffer} to this + * file, starting at the current file pointer and using {@code byteOffset} as * the first position within {@code buffer} to get bytes. * - * @param buffer - * the buffer to write to this file. - * @param offset - * the index of the first byte in {@code buffer} to write. - * @param count - * the number of bytes from {@code buffer} to write. * @throws IndexOutOfBoundsException - * if {@code count < 0}, {@code offset < 0} or {@code count + - * offset} is greater than the size of {@code buffer}. + * if {@code byteCount < 0}, {@code byteOffset < 0} or {@code byteCount + + * byteOffset} is greater than the size of {@code buffer}. * @throws IOException * if an I/O error occurs while writing to this file. */ - public void write(byte[] buffer, int offset, int count) throws IOException { - Arrays.checkOffsetAndCount(buffer.length, offset, count); - if (count == 0) { - return; - } - openCheck(); - Platform.FILE_SYSTEM.write(fd.descriptor, buffer, offset, count); - + public void write(byte[] buffer, int byteOffset, int byteCount) throws IOException { + IoUtils.write(fd, buffer, byteOffset, byteCount); // if we are in "rws" mode, attempt to sync file+metadata if (syncMetadata) { fd.sync(); diff --git a/luni/src/main/java/java/nio/FileChannelImpl.java b/luni/src/main/java/java/nio/FileChannelImpl.java index 1717d45..f87c7e7 100644 --- a/luni/src/main/java/java/nio/FileChannelImpl.java +++ b/luni/src/main/java/java/nio/FileChannelImpl.java @@ -527,35 +527,25 @@ final class FileChannelImpl extends FileChannel { } private int writeImpl(ByteBuffer buffer) throws IOException { - int bytesWritten; - boolean completed = false; synchronized (repositioningLock) { - if (buffer.isDirect()) { - try { - begin(); - int address = NioUtils.getDirectBufferAddress(buffer); - bytesWritten = (int) Platform.FILE_SYSTEM.writeDirect(IoUtils.getFd(fd), - address, buffer.position(), buffer.remaining()); - completed = true; - } finally { - end(completed); - } - } else { + int bytesWritten = 0; + boolean completed = false; + try { + begin(); try { - begin(); - bytesWritten = (int) Platform.FILE_SYSTEM.write(IoUtils.getFd(fd), buffer - .array(), buffer.arrayOffset() + buffer.position(), - buffer.remaining()); - completed = true; - } finally { - end(completed); + bytesWritten = Libcore.os.write(fd, buffer); + } catch (ErrnoException errnoException) { + throw errnoException.rethrowAsIOException(); } + completed = true; + } finally { + end(completed); } if (bytesWritten > 0) { buffer.position(buffer.position() + bytesWritten); } + return bytesWritten; } - return bytesWritten; } public long write(ByteBuffer[] buffers, int offset, int length) throws IOException { diff --git a/luni/src/main/java/java/nio/SelectorImpl.java b/luni/src/main/java/java/nio/SelectorImpl.java index 3dfdbb2..a79f127 100644 --- a/luni/src/main/java/java/nio/SelectorImpl.java +++ b/luni/src/main/java/java/nio/SelectorImpl.java @@ -378,8 +378,8 @@ final class SelectorImpl extends AbstractSelector { @Override public Selector wakeup() { try { - Platform.FILE_SYSTEM.write(IoUtils.getFd(wakeupOut), new byte[]{1}, 0, 1); - } catch (IOException ignored) { + Libcore.os.write(wakeupOut, new byte[] { 1 }, 0, 1); + } catch (ErrnoException ignored) { } return this; } diff --git a/luni/src/main/java/libcore/io/BlockGuardOs.java b/luni/src/main/java/libcore/io/BlockGuardOs.java index e30096e..38a027d 100644 --- a/luni/src/main/java/libcore/io/BlockGuardOs.java +++ b/luni/src/main/java/libcore/io/BlockGuardOs.java @@ -61,4 +61,14 @@ public class BlockGuardOs extends ForwardingOs { BlockGuard.getThreadPolicy().onReadFromDisk(); return os.read(fd, bytes, byteOffset, byteCount); } + + public int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException { + BlockGuard.getThreadPolicy().onWriteToDisk(); + return os.write(fd, buffer); + } + + public int write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException { + BlockGuard.getThreadPolicy().onWriteToDisk(); + return os.write(fd, bytes, byteOffset, byteCount); + } } diff --git a/luni/src/main/java/libcore/io/ForwardingOs.java b/luni/src/main/java/libcore/io/ForwardingOs.java index bbaa141..a53ac82 100644 --- a/luni/src/main/java/libcore/io/ForwardingOs.java +++ b/luni/src/main/java/libcore/io/ForwardingOs.java @@ -65,4 +65,6 @@ public class ForwardingOs implements Os { public void symlink(String oldPath, String newPath) throws ErrnoException { os.symlink(oldPath, newPath); } public long sysconf(int name) { return os.sysconf(name); } public StructUtsname uname() throws ErrnoException { return os.uname(); } + public int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException { return os.write(fd, buffer); } + public int write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException { return os.write(fd, bytes, byteOffset, byteCount); } } diff --git a/luni/src/main/java/libcore/io/IoUtils.java b/luni/src/main/java/libcore/io/IoUtils.java index 3eb4bf4..9f4946e 100644 --- a/luni/src/main/java/libcore/io/IoUtils.java +++ b/luni/src/main/java/libcore/io/IoUtils.java @@ -22,6 +22,7 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.net.Socket; +import java.util.Arrays; import static libcore.io.OsConstants.*; public final class IoUtils { @@ -67,6 +68,10 @@ public final class IoUtils { * Unix practice where you'd read until you got 0 bytes (and any future read would return -1). */ public static int read(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws IOException { + Arrays.checkOffsetAndCount(bytes.length, byteOffset, byteCount); + if (byteCount == 0) { + return 0; + } try { int readCount = Libcore.os.read(fd, bytes, byteOffset, byteCount); if (readCount == 0) { @@ -83,6 +88,26 @@ public final class IoUtils { } /** + * java.io always writes every byte it's asked to, or fails with an error. (That is, unlike + * Unix it never just writes as many bytes as happens to be convenient.) + */ + public static void write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws IOException { + Arrays.checkOffsetAndCount(bytes.length, byteOffset, byteCount); + if (byteCount == 0) { + return; + } + try { + while (byteCount > 0) { + int bytesWritten = Libcore.os.write(fd, bytes, byteOffset, byteCount); + byteCount -= bytesWritten; + byteOffset += bytesWritten; + } + } catch (ErrnoException errnoException) { + throw errnoException.rethrowAsIOException(); + } + } + + /** * Calls close(2) on 'fd'. Also resets the internal int to -1. */ public static native void close(FileDescriptor fd) throws IOException; diff --git a/luni/src/main/java/libcore/io/Os.java b/luni/src/main/java/libcore/io/Os.java index dce84d7..ba3c4a8 100644 --- a/luni/src/main/java/libcore/io/Os.java +++ b/luni/src/main/java/libcore/io/Os.java @@ -56,4 +56,6 @@ public interface Os { public void symlink(String oldPath, String newPath) throws ErrnoException; public long sysconf(int name); public StructUtsname uname() throws ErrnoException; + public int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException; + public int write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException; } diff --git a/luni/src/main/java/libcore/io/Posix.java b/luni/src/main/java/libcore/io/Posix.java index fd569ac..5afd1a7 100644 --- a/luni/src/main/java/libcore/io/Posix.java +++ b/luni/src/main/java/libcore/io/Posix.java @@ -50,9 +50,8 @@ public final class Posix implements Os { public int read(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException { if (buffer.isDirect()) { return readDirectBuffer(fd, buffer, buffer.position(), buffer.remaining()); - } else { - return read(fd, buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); } + return read(fd, buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); } private native int readDirectBuffer(FileDescriptor fd, ByteBuffer buffer, int position, int remaining) throws ErrnoException; public native int read(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException; @@ -65,4 +64,12 @@ public final class Posix implements Os { public native void symlink(String oldPath, String newPath) throws ErrnoException; public native long sysconf(int name); public native StructUtsname uname() throws ErrnoException; + public int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException { + if (buffer.isDirect()) { + return writeDirectBuffer(fd, buffer, buffer.position(), buffer.remaining()); + } + return write(fd, buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); + } + private native int writeDirectBuffer(FileDescriptor fd, ByteBuffer buffer, int position, int remaining) throws ErrnoException; + public native int write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException; } 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 index 02ed4de..6a3b71c 100644 --- a/luni/src/main/java/org/apache/harmony/luni/platform/IFileSystem.java +++ b/luni/src/main/java/org/apache/harmony/luni/platform/IFileSystem.java @@ -22,18 +22,12 @@ import java.io.FileNotFoundException; import java.io.IOException; public interface IFileSystem { - public long write(int fileDescriptor, byte[] bytes, int offset, int length) - throws IOException; - public long readv(int fileDescriptor, int[] addresses, int[] offsets, int[] lengths, int size) throws IOException; public long writev(int fileDescriptor, int[] addresses, int[] offsets, int[] lengths, int size) throws IOException; - public long writeDirect(int fileDescriptor, int address, int offset, - int length) throws IOException; - 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 index ba5bfe0..8337b0c 100644 --- a/luni/src/main/java/org/apache/harmony/luni/platform/OSFileSystem.java +++ b/luni/src/main/java/org/apache/harmony/luni/platform/OSFileSystem.java @@ -33,10 +33,6 @@ class OSFileSystem implements IFileSystem { private OSFileSystem() { } - public native long writeDirect(int fd, int address, int offset, int length); - - public native long write(int fd, byte[] bytes, int offset, int length) throws IOException; - /* * Scatter/gather calls. */ diff --git a/luni/src/main/native/libcore_io_Posix.cpp b/luni/src/main/native/libcore_io_Posix.cpp index 1205786..e1db002 100644 --- a/luni/src/main/native/libcore_io_Posix.cpp +++ b/luni/src/main/native/libcore_io_Posix.cpp @@ -430,6 +430,21 @@ static jobject Posix_uname(JNIEnv* env, jobject) { return makeStructUtsname(env, buf); } +static jint Posix_write(JNIEnv* env, jobject, jobject javaFd, jbyteArray javaBytes, jint byteOffset, jint byteCount) { + int fd = jniGetFDFromFileDescriptor(env, javaFd); + ScopedByteArrayRO bytes(env, javaBytes); + if (bytes.get() == NULL) { + return -1; + } + return throwIfMinusOne(env, "write", TEMP_FAILURE_RETRY(write(fd, bytes.get() + byteOffset, byteCount))); +} + +static jint Posix_writeDirectBuffer(JNIEnv* env, jobject, jobject javaFd, jobject byteBuffer, jint position, jint remaining) { + int fd = jniGetFDFromFileDescriptor(env, javaFd); + jbyte* ptr = reinterpret_cast<jbyte*>(env->GetDirectBufferAddress(byteBuffer)); + return throwIfMinusOne(env, "write", TEMP_FAILURE_RETRY(write(fd, ptr + position, remaining))); +} + static JNINativeMethod gMethods[] = { NATIVE_METHOD(Posix, access, "(Ljava/lang/String;I)Z"), NATIVE_METHOD(Posix, chmod, "(Ljava/lang/String;I)V"), @@ -467,6 +482,8 @@ static JNINativeMethod gMethods[] = { NATIVE_METHOD(Posix, symlink, "(Ljava/lang/String;Ljava/lang/String;)V"), NATIVE_METHOD(Posix, sysconf, "(I)J"), NATIVE_METHOD(Posix, uname, "()Llibcore/io/StructUtsname;"), + NATIVE_METHOD(Posix, write, "(Ljava/io/FileDescriptor;[BII)I"), + NATIVE_METHOD(Posix, writeDirectBuffer, "(Ljava/io/FileDescriptor;Ljava/nio/ByteBuffer;II)I"), }; int register_libcore_io_Posix(JNIEnv* env) { return jniRegisterNativeMethods(env, "libcore/io/Posix", gMethods, NELEM(gMethods)); 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 index cf4e49f..1b4dcc9 100644 --- a/luni/src/main/native/org_apache_harmony_luni_platform_OSFileSystem.cpp +++ b/luni/src/main/native/org_apache_harmony_luni_platform_OSFileSystem.cpp @@ -134,29 +134,6 @@ static jlong OSFileSystem_transfer(JNIEnv* env, jobject, jint fd, jobject sd, return rc; } -static jlong OSFileSystem_writeDirect(JNIEnv* env, jobject, jint fd, - jint buf, jint offset, jint byteCount) { - if (byteCount == 0) { - return 0; - } - jbyte* src = reinterpret_cast<jbyte*>(buf + offset); - jlong rc = TEMP_FAILURE_RETRY(write(fd, src, byteCount)); - if (rc == -1) { - jniThrowIOException(env, errno); - } - return rc; -} - -static jlong OSFileSystem_write(JNIEnv* env, jobject, jint fd, - jbyteArray byteArray, jint offset, jint byteCount) { - ScopedByteArrayRO bytes(env, byteArray); - if (bytes.get() == NULL) { - return 0; - } - jint buf = static_cast<jint>(reinterpret_cast<uintptr_t>(bytes.get())); - return OSFileSystem_writeDirect(env, NULL, fd, buf, offset, byteCount); -} - static jint OSFileSystem_ioctlAvailable(JNIEnv*env, jobject, jobject fileDescriptor) { /* * On underlying platforms Android cares about (read "Linux"), @@ -212,8 +189,6 @@ static JNINativeMethod gMethods[] = { NATIVE_METHOD(OSFileSystem, ioctlAvailable, "(Ljava/io/FileDescriptor;)I"), NATIVE_METHOD(OSFileSystem, readv, "(I[I[I[II)J"), NATIVE_METHOD(OSFileSystem, transfer, "(ILjava/io/FileDescriptor;JJ)J"), - NATIVE_METHOD(OSFileSystem, write, "(I[BII)J"), - NATIVE_METHOD(OSFileSystem, writeDirect, "(IIII)J"), NATIVE_METHOD(OSFileSystem, writev, "(I[I[I[II)J"), }; int register_org_apache_harmony_luni_platform_OSFileSystem(JNIEnv* env) { |