override InputStream.available for FileInputStream and the instance returned by ZipFile.getInputStream

This commit is contained in:
Joel Dice
2012-05-04 19:55:53 -06:00
parent ea4e0a2f5d
commit 1119675b7a
3 changed files with 74 additions and 4 deletions

View File

@ -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 {