summaryrefslogtreecommitdiffstats
path: root/luni
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2009-09-30 15:25:29 -0700
committerElliott Hughes <enh@google.com>2009-09-30 15:39:31 -0700
commit109fc1115e7afd2907544b805eaa2cc8a0e2635f (patch)
tree6dfef07a245a94931ba4fc58073e9e013714d540 /luni
parent74b78ff194b4bfa98c319dfd75e18b42c6780d0f (diff)
downloadlibcore-109fc1115e7afd2907544b805eaa2cc8a0e2635f.zip
libcore-109fc1115e7afd2907544b805eaa2cc8a0e2635f.tar.gz
libcore-109fc1115e7afd2907544b805eaa2cc8a0e2635f.tar.bz2
Simplify FileInputStream.skip to match the RI.
The RI throws IOException on any non-seekable stream, including stdin. This patch removes harmony's special-case hack for stdin, and the native cruft that wasn't even necessary if you did want a special hack for stdin. Bug: 1542253
Diffstat (limited to 'luni')
-rw-r--r--luni/src/main/java/java/io/FileInputStream.java46
-rw-r--r--luni/src/main/java/org/apache/harmony/luni/platform/IFileSystem.java3
-rw-r--r--luni/src/main/java/org/apache/harmony/luni/platform/OSFileSystem.java19
-rw-r--r--luni/src/main/native/org_apache_harmony_luni_platform_OSFileSystem.cpp6
4 files changed, 27 insertions, 47 deletions
diff --git a/luni/src/main/java/java/io/FileInputStream.java b/luni/src/main/java/java/io/FileInputStream.java
index 1243262..c236888 100644
--- a/luni/src/main/java/java/io/FileInputStream.java
+++ b/luni/src/main/java/java/io/FileInputStream.java
@@ -312,25 +312,24 @@ public class FileInputStream extends InputStream implements Closeable {
}
openCheck();
synchronized (repositioningLock) {
- // stdin requires special handling
- if (fd == FileDescriptor.in) {
- return (int) fileSystem.ttyRead(buffer, offset, count);
- }
+ // BEGIN android-changed
+ // If you only support Linux, there's nothing special about stdin.
return (int) fileSystem.read(fd.descriptor, buffer, offset, count);
+ // END android-changed
}
}
/**
* Skips {@code count} number of bytes in this stream. Subsequent
- * {@code read()}'s will not return these bytes unless {@code reset()} is
- * used. This method may perform multiple reads to read {@code count} bytes.
+ * {@code read()}s will not return these bytes unless {@code reset()} is
+ * used. If the underlying stream is unseekable, an IOException is thrown.
*
* @param count
* the number of bytes to skip.
* @return the number of bytes actually skipped.
* @throws IOException
- * if {@code count < 0}, this stream is closed or another
- * IOException occurs.
+ * if {@code count < 0}, this stream is closed or unseekable,
+ * or another IOException occurs.
*/
@Override
public long skip(long count) throws IOException {
@@ -344,29 +343,18 @@ public class FileInputStream extends InputStream implements Closeable {
throw new IOException(Msg.getString("KA013")); //$NON-NLS-1$
}
- // stdin requires special handling
- if (fd == FileDescriptor.in) {
- // Read and discard count bytes in 8k chunks
- long skipped = 0, numRead;
- int chunk = count < 8192 ? (int) count : 8192;
- byte[] buffer = new byte[chunk];
- for (long i = count / chunk; i >= 0; i--) {
- numRead = fileSystem.ttyRead(buffer, 0, chunk);
- skipped += numRead;
- if (numRead < chunk) {
- return skipped;
- }
- }
- return skipped;
- }
-
+ // BEGIN android-changed
+ // The RI doesn't treat stdin as special. It throws IOException for
+ // all non-seekable streams, so we do too. If you did want to support
+ // non-seekable streams, the best way to do it would be to recognize
+ // when lseek(2) fails with ESPIPE and call super.skip(count).
synchronized (repositioningLock) {
- final long currentPosition = fileSystem.seek(fd.descriptor, 0L,
- IFileSystem.SEEK_CUR);
- final long newPosition = fileSystem.seek(fd.descriptor,
- currentPosition + count, IFileSystem.SEEK_SET);
- return newPosition - currentPosition;
+ // Our seek returns the new offset, but we know it will throw an
+ // exception if it couldn't perform exactly the seek we asked for.
+ fileSystem.seek(fd.descriptor, count, IFileSystem.SEEK_CUR);
+ return count;
}
+ // END android-changed
}
private synchronized void openCheck() throws IOException {
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 7613f0e..bee1557 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
@@ -108,10 +108,9 @@ public interface IFileSystem {
// BEGIN android-deleted
// public long ttyAvailable() throws IOException;
+ // public long ttyRead(byte[] bytes, int offset, int length) throws IOException;
// END android-deleted
- public long ttyRead(byte[] bytes, int offset, int length) throws IOException;
-
// BEGIN android-added
public int ioctlAvailable(int fileDescriptor) throws IOException;
// END android-added
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 0338a52..b7a62e2 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
@@ -182,16 +182,15 @@ class OSFileSystem implements IFileSystem {
// private native long ttyAvailableImpl();
// END android-deleted
- // BEGIN android-changed
- public long ttyRead(byte[] bytes, int offset, int length) throws IOException {
- if (bytes == null) {
- throw new NullPointerException();
- }
- return ttyReadImpl(bytes, offset, length);
- }
-
- private native long ttyReadImpl(byte[] bytes, int offset, int length) throws IOException;
- // END android-changed
+ // BEGIN android-deleted
+ // public long ttyRead(byte[] bytes, int offset, int length) throws IOException {
+ // if (bytes == null) {
+ // throw new NullPointerException();
+ // }
+ // return ttyReadImpl(bytes, offset, length);
+ // }
+ // private native long ttyReadImpl(byte[] bytes, int offset, int length) throws IOException;
+ // END android-deleted
// BEGIN android-added
public native int ioctlAvailable(int fileDescriptor) throws IOException;
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 14fabbe..3e229d0 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
@@ -495,11 +495,6 @@ static jint harmony_io_ioctlAvailable(JNIEnv*env, jobject, jint fd) {
return (jint) avail;
}
-static jlong harmony_io_ttyReadImpl(JNIEnv* env, jobject thiz,
- jbyteArray byteArray, jint offset, jint nbytes) {
- return harmony_io_readImpl(env, thiz, STDIN_FILENO, byteArray, offset, nbytes);
-}
-
/*
* JNI registration
*/
@@ -518,7 +513,6 @@ static JNINativeMethod gMethods[] = {
{ "transfer", "(ILjava/io/FileDescriptor;JJ)J",
(void*) harmony_io_transfer },
{ "truncate", "(IJ)V", (void*) harmony_io_truncate },
- { "ttyReadImpl", "([BII)J", (void*) harmony_io_ttyReadImpl },
{ "unlockImpl", "(IJJ)V", (void*) harmony_io_unlockImpl },
{ "writeDirect", "(IIII)J", (void*) harmony_io_writeDirect },
{ "writeImpl", "(I[BII)J", (void*) harmony_io_writeImpl },