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 {

View File

@ -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();

View File

@ -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();
}
}
}
}