1
0
mirror of https://github.com/moparisthebest/k-9 synced 2025-02-07 10:40:11 -05:00

FixedLengthInputStream - code cleanup

Inspired by andrewgaul
This commit is contained in:
cketti 2011-11-28 01:21:50 +01:00
parent 798d6753dd
commit 23c72cd181

View File

@ -10,9 +10,9 @@ import java.io.InputStream;
* past where the protocol handler intended the client to read. * past where the protocol handler intended the client to read.
*/ */
public class FixedLengthInputStream extends InputStream { public class FixedLengthInputStream extends InputStream {
private InputStream mIn; private final InputStream mIn;
private int mLength; private final int mLength;
private int mCount; private int mCount = 0;
public FixedLengthInputStream(InputStream in, int length) { public FixedLengthInputStream(InputStream in, int length) {
this.mIn = in; this.mIn = in;
@ -26,31 +26,29 @@ public class FixedLengthInputStream extends InputStream {
@Override @Override
public int read() throws IOException { public int read() throws IOException {
if (mCount < mLength) { if (mCount >= mLength) {
return -1;
}
int d = mIn.read(); int d = mIn.read();
if (d != -1) { if (d != -1) {
mCount++; mCount++;
} }
return d; return d;
} else {
return -1;
}
} }
@Override @Override
public int read(byte[] b, int offset, int length) throws IOException { public int read(byte[] b, int offset, int length) throws IOException {
if (mCount < mLength) { if (mCount >= mLength) {
return -1;
}
int d = mIn.read(b, offset, Math.min(mLength - mCount, length)); int d = mIn.read(b, offset, Math.min(mLength - mCount, length));
if (d == -1) { if (d != -1) {
return -1;
} else {
mCount += d; mCount += d;
}
return d; return d;
} }
} else {
return -1;
}
}
@Override @Override
public int read(byte[] b) throws IOException { public int read(byte[] b) throws IOException {