summaryrefslogtreecommitdiffstats
path: root/luni/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'luni/src/main/java')
-rw-r--r--luni/src/main/java/java/io/FileInputStream.java16
-rw-r--r--luni/src/main/java/java/io/FileOutputStream.java15
-rw-r--r--luni/src/main/java/java/io/RandomAccessFile.java47
-rw-r--r--luni/src/main/java/java/nio/FileChannelImpl.java32
-rw-r--r--luni/src/main/java/java/nio/SelectorImpl.java4
-rw-r--r--luni/src/main/java/libcore/io/BlockGuardOs.java10
-rw-r--r--luni/src/main/java/libcore/io/ForwardingOs.java2
-rw-r--r--luni/src/main/java/libcore/io/IoUtils.java25
-rw-r--r--luni/src/main/java/libcore/io/Os.java2
-rw-r--r--luni/src/main/java/libcore/io/Posix.java11
-rw-r--r--luni/src/main/java/org/apache/harmony/luni/platform/IFileSystem.java6
-rw-r--r--luni/src/main/java/org/apache/harmony/luni/platform/OSFileSystem.java4
12 files changed, 72 insertions, 102 deletions
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.
*/