What's wrong with the following code?

public void fillByteArray(InputStream s, byte[] b, int offset, Integer numReads, int readAmount){
    while (numReads > 0){
        s.read(b, offset, readAmount);
        offset += readAmount;
        numReads--;
    }
}

Recommended Answers

All 2 Replies

Just read the compiler error message(s)

s.read(b, offset, readAmount);
offset += readAmount;

readAmount is the maximum amount to read, not the amount that is actually read. The return value of s.read(b, offset, readAmount) is the amount actually read. You should also be checking for a return value of -1 which indicates the end of the stream. Naturally, whether these things are wrong depends upon your goals.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.