1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 19:42:17 +02:00

fixed SliceInputStream

This commit is contained in:
Nikolay Pultsin 2015-01-02 05:04:25 +00:00
parent ce6e052277
commit abd7b1e0f0
2 changed files with 18 additions and 15 deletions

View file

@ -41,12 +41,22 @@ public class InputStreamWithOffset extends InputStream {
if (shift > 0) {
myOffset += (int)shift;
}
while ((shift < n) && (read() != -1)) {
while (shift < n && read() != -1) {
++shift;
}
return shift;
}
// does not call virtual methods
protected final long baseSkip(long n) throws IOException {
long shift = myDecoratedStream.skip(n);
while (shift < n && myDecoratedStream.read() != -1) {
++shift;
}
myOffset += shift;
return shift;
}
@Override
public int read() throws IOException {
int result = myDecoratedStream.read();

View file

@ -25,36 +25,29 @@ import java.io.InputStream;
public class SliceInputStream extends InputStreamWithOffset {
private final int myStart;
private final int myLength;
private boolean myCreated = false;
public SliceInputStream(InputStream base, int start, int length) throws IOException {
super(base);
super.skip(start);
baseSkip(start);
myStart = start;
myLength = length;
myCreated = true;
}
@Override
public int read() throws IOException {
if (myCreated) {
if (offset() >= myLength) {
return -1;
}
if (offset() >= myLength) {
return -1;
}
return super.read();
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
if (myCreated) {
int maxbytes = myLength - offset();
if (maxbytes <= 0) {
return -1;
}
len = Math.min(len, maxbytes);
final int maxbytes = myLength - offset();
if (maxbytes <= 0) {
return -1;
}
return super.read(b, off, len);
return super.read(b, off, Math.min(len, maxbytes));
}
@Override