forked from I2P_Developers/i2p.i2p
DataHelper: Fix read() for nonzero offset, broken since the
beginning (2004) but unused by this repo; Throw EOFException on short read rather than returning a smaller value, since the whole point is to guarantee a complete read
This commit is contained in:
@ -1272,28 +1272,38 @@ public class DataHelper {
|
|||||||
/**
|
/**
|
||||||
* This is different than InputStream.read(target), in that it
|
* This is different than InputStream.read(target), in that it
|
||||||
* does repeated reads until the full data is received.
|
* does repeated reads until the full data is received.
|
||||||
|
*
|
||||||
|
* As of 0.9.27, throws EOFException if the full length is not read.
|
||||||
|
*
|
||||||
|
* @return target.length
|
||||||
|
* @throws EOFException if the full length is not read (since 0.9.27)
|
||||||
*/
|
*/
|
||||||
public static int read(InputStream in, byte target[]) throws IOException {
|
public static int read(InputStream in, byte target[]) throws IOException {
|
||||||
return read(in, target, 0, target.length);
|
return read(in, target, 0, target.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is different than InputStream.read(target, offset, length), in that it
|
* WARNING - This is different than InputStream.read(target, offset, length)
|
||||||
* returns the new offset (== old offset + bytes read).
|
* for a nonzero offset, in that it
|
||||||
|
* returns the new offset (== old offset + length).
|
||||||
* It also does repeated reads until the full data is received.
|
* It also does repeated reads until the full data is received.
|
||||||
* @return the new offset (== old offset + bytes read)
|
*
|
||||||
|
* WARNING - Broken for nonzero offset before 0.9.27.
|
||||||
|
* As of 0.9.27, throws EOFException if the full length is not read.
|
||||||
|
*
|
||||||
|
* @return the new offset (== old offset + length)
|
||||||
|
* @throws EOFException if the full length is not read (since 0.9.27)
|
||||||
*/
|
*/
|
||||||
public static int read(InputStream in, byte target[], int offset, int length) throws IOException {
|
public static int read(InputStream in, byte target[], int offset, int length) throws IOException {
|
||||||
int cur = offset;
|
int cur = 0;
|
||||||
while (cur < length) {
|
while (cur < length) {
|
||||||
int numRead = in.read(target, cur, length - cur);
|
int numRead = in.read(target, offset + cur, length - cur);
|
||||||
if (numRead == -1) {
|
if (numRead == -1) {
|
||||||
if (cur == offset) return -1; // throw new EOFException("EOF Encountered during reading");
|
throw new EOFException("EOF after reading " + cur + " bytes of " + length + " byte value");
|
||||||
return cur;
|
|
||||||
}
|
}
|
||||||
cur += numRead;
|
cur += numRead;
|
||||||
}
|
}
|
||||||
return cur;
|
return offset + cur;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user