1
0
mirror of https://github.com/moparisthebest/k-9 synced 2025-01-31 07:10:14 -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.
*/
public class FixedLengthInputStream extends InputStream {
private InputStream mIn;
private int mLength;
private int mCount;
private final InputStream mIn;
private final int mLength;
private int mCount = 0;
public FixedLengthInputStream(InputStream in, int length) {
this.mIn = in;
@ -26,30 +26,28 @@ public class FixedLengthInputStream extends InputStream {
@Override
public int read() throws IOException {
if (mCount < mLength) {
int d = mIn.read();
if (d != -1) {
mCount++;
}
return d;
} else {
if (mCount >= mLength) {
return -1;
}
int d = mIn.read();
if (d != -1) {
mCount++;
}
return d;
}
@Override
public int read(byte[] b, int offset, int length) throws IOException {
if (mCount < mLength) {
int d = mIn.read(b, offset, Math.min(mLength - mCount, length));
if (d == -1) {
return -1;
} else {
mCount += d;
return d;
}
} else {
if (mCount >= mLength) {
return -1;
}
int d = mIn.read(b, offset, Math.min(mLength - mCount, length));
if (d != -1) {
mCount += d;
}
return d;
}
@Override