Changeset cc179b4


Ignore:
Timestamp:
Sep 22, 2016 5:01:32 PM (4 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
fb323ce
Parents:
2fd0ed1
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/data/DataHelper.java

    r2fd0ed1 rcc179b4  
    12731273     *  This is different than InputStream.read(target), in that it
    12741274     *  does repeated reads until the full data is received.
     1275     *
     1276     *  As of 0.9.27, throws EOFException if the full length is not read.
     1277     *
     1278     *  @return target.length
     1279     *  @throws EOFException if the full length is not read (since 0.9.27)
    12751280     */
    12761281    public static int read(InputStream in, byte target[]) throws IOException {
     
    12791284
    12801285    /**
    1281      *  This is different than InputStream.read(target, offset, length), in that it
    1282      *  returns the new offset (== old offset + bytes read).
     1286     *  WARNING - This is different than InputStream.read(target, offset, length)
     1287     *  for a nonzero offset, in that it
     1288     *  returns the new offset (== old offset + length).
    12831289     *  It also does repeated reads until the full data is received.
    1284      *  @return the new offset (== old offset + bytes read)
     1290     *
     1291     *  WARNING - Broken for nonzero offset before 0.9.27.
     1292     *  As of 0.9.27, throws EOFException if the full length is not read.
     1293     *
     1294     *  @return the new offset (== old offset + length)
     1295     *  @throws EOFException if the full length is not read (since 0.9.27)
    12851296     */
    12861297    public static int read(InputStream in, byte target[], int offset, int length) throws IOException {
    1287         int cur = offset;
     1298        int cur = 0;
    12881299        while (cur < length) {
    1289             int numRead = in.read(target, cur, length - cur);
     1300            int numRead = in.read(target, offset + cur, length - cur);
    12901301            if (numRead == -1) {
    1291                 if (cur == offset) return -1; // throw new EOFException("EOF Encountered during reading");
    1292                 return cur;
     1302                throw new EOFException("EOF after reading " + cur + " bytes of " + length + " byte value");
    12931303            }
    12941304            cur += numRead;
    12951305        }
    1296         return cur;
     1306        return offset + cur;
    12971307    }
    12981308   
Note: See TracChangeset for help on using the changeset viewer.