diff --git a/classpath/java/io/FileInputStream.java b/classpath/java/io/FileInputStream.java index 9c614e2f7e..29df029529 100644 --- a/classpath/java/io/FileInputStream.java +++ b/classpath/java/io/FileInputStream.java @@ -16,6 +16,7 @@ public class FileInputStream extends InputStream { // } private int fd; + private int remaining; public FileInputStream(FileDescriptor fd) { this.fd = fd.value; @@ -23,12 +24,17 @@ public class FileInputStream extends InputStream { public FileInputStream(String path) throws IOException { fd = open(path); + remaining = (int) new File(path).length(); } public FileInputStream(File file) throws IOException { this(file.getPath()); } + public int available() throws IOException { + return remaining; + } + private static native int open(String path) throws IOException; private static native int read(int fd) throws IOException; @@ -39,7 +45,11 @@ public class FileInputStream extends InputStream { public static native void close(int fd) throws IOException; public int read() throws IOException { - return read(fd); + int c = read(fd); + if (c >= 0 && remaining > 0) { + -- remaining; + } + return c; } public int read(byte[] b, int offset, int length) throws IOException { @@ -51,7 +61,11 @@ public class FileInputStream extends InputStream { throw new ArrayIndexOutOfBoundsException(); } - return read(fd, b, offset, length); + int c = read(fd, b, offset, length); + if (c > 0 && remaining > 0) { + remaining -= c; + } + return c; } public void close() throws IOException { diff --git a/classpath/java/util/zip/ZipFile.java b/classpath/java/util/zip/ZipFile.java index 94d8b99b08..d356bbf5ed 100644 --- a/classpath/java/util/zip/ZipFile.java +++ b/classpath/java/util/zip/ZipFile.java @@ -86,7 +86,7 @@ public class ZipFile { } public InputStream getInputStream(ZipEntry entry) throws IOException { - int pointer = ((MyEntry) entry).pointer(); + final int pointer = ((MyEntry) entry).pointer(); int method = compressionMethod(window, pointer); int size = compressedSize(window, pointer); InputStream in = new MyInputStream(file, fileData(window, pointer), size); @@ -99,7 +99,35 @@ public class ZipFile { return in; case Deflated: - return new InflaterInputStream(in, new Inflater(true)); + return new InflaterInputStream(in, new Inflater(true)) { + int remaining = uncompressedSize(window, pointer); + + public int read() throws IOException { + int c = super.read(); + if (c >= 0) { + -- remaining; + } + return c; + } + + public int read(byte[] buffer) throws IOException { + return read(buffer, 0, buffer.length); + } + + public int read(byte[] buffer, int offset, int length) + throws IOException + { + int c = super.read(buffer, offset, length); + if (c > 0) { + remaining -= c; + } + return c; + } + + public int available() { + return remaining; + } + }; default: throw new IOException(); diff --git a/test/Files.java b/test/Files.java index b5484462cf..7a0170b678 100644 --- a/test/Files.java +++ b/test/Files.java @@ -1,4 +1,6 @@ import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; public class Files { private static void expect(boolean v) { @@ -46,6 +48,32 @@ public class Files { expect(! f.createNewFile()); f.delete(); } + + { File f = new File("test.txt"); + FileOutputStream out = new FileOutputStream(f); + try { + byte[] message = "hello, world!\n".getBytes(); + out.write(message); + out.close(); + + FileInputStream in = new FileInputStream(f); + try { + expect(in.available() == message.length); + + for (int i = 0; i < message.length; ++i) { + in.read(); + expect(in.available() == message.length - i - 1); + } + + expect(in.read() == -1); + expect(in.available() == 0); + } finally { + in.close(); + } + } finally { + f.delete(); + } + } } }