Should switch to IOUtils.read() when we update to the latest version of
commons-io

@param in
@param b
@param off
@param len
@return
@throws IOException
558936
private static int readFully(InputStream in, byte[] b, int off, int len) throws IOException {
        int total = 0;
        for (;;) {
            int got = in.read(b, off + total, len - total);
            if (got < 0) {
                return (total == 0) ? -1 : total;
            }
            else {
                total += got;
                if (total == len)
                    return total;
            }
        }
    }
741221
@Override
    public int read (byte[] b, int off, int len) throws IOException {
        if (b == null) {
            throw new NullPointerException();
        } else if (off < 0 || len < 0 || len > b.length - off) {
            throw new IndexOutOfBoundsException();
        } else if (len == 0) {
            return 0;
        }

        int c;
        if (nonBlockingEnabled)
            c = this.read(0L);
        else
            c = in.read();

        if (c == -1) {
            return -1;
        }
        b[off] = (byte)c;
        return 1;
    }
959421
@Override
  public void readFully(byte[] b, int off, int len) throws IOException {
    while (len > 0) {
      int read = inner.read(b, off, len);
      if (read <= 0) {
        throw new EOFException();
      }
      off += read;
      len -= read;
    }
  }
1086526
public void write(byte[] b, int off, int len) throws IOException {
	if (b != null) {
	    if (off + len > b.length) {
		len = b.length - off;
	    }
	    if (len <= 0) {
		return;
	    }
	    pos += blob.write(b, off, pos, len);
	}
    }
431407
public int read(byte[] b, int off, int len) throws IOException
    {
        if (_closed)
            return -1;

        if (_packet.unconsumedData()==0)
        {
            fillPacket();
            if (_packet.unconsumedData()==0)
            {
                _closed=true;
                return -1;
            }
        }

        return _packet.getBytes(b,off,len);
    }
991178
public int read(long pos, byte[] b, int off, int len) {
    // since max is len (an int), result is guaranteed to be an int
    int bytesToRead = (int) bytesToRead(pos, len);

    if (bytesToRead > 0) {
      int remaining = bytesToRead;

      int blockIndex = blockIndex(pos);
      byte[] block = blocks[blockIndex];
      int offsetInBlock = offsetInBlock(pos);

      int read = get(block, offsetInBlock, b, off, length(offsetInBlock, remaining));
      remaining -= read;
      off += read;

      while (remaining > 0) {
        int index = ++blockIndex;
        block = blocks[index];

        read = get(block, 0, b, off, length(remaining));
        remaining -= read;
        off += read;
      }
    }

    return bytesToRead;
  }
1254637
public void write(byte[] b, int off, int len) throws IOException
    {
        if(_outHelper.isOutputStream_closed()|| _outHelper.isOutputStream_close_initiated_but_not_Flush_ready()){
            Tr.error(tc, "stream.is.closed.no.read.write");                         
            throw new IOException(Tr.formatMessage(tc, "stream.is.closed.no.read.write"));           
        }
        if(b == null){
            Tr.error(tc, "read.write.bytearray.null"); 
            throw new NullPointerException(Tr.formatMessage(tc, "read.write.bytearray.null"));
        }
        if (off <0 || len < 0 || off+len > b.length)
        {            
            Tr.error(tc, "read.write.offset.length.bytearraylength", new Object[] { off,  len, b.length} );
            throw new IndexOutOfBoundsException(Tr.formatMessage(tc, "read.write.offset.length.bytearraylength", new Object[] { off,  len, b.length}));            
        }
        if(this._listener!= null &&  !checkIfCalledFromWLonError()){
            if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled())    
                Tr.debug(tc, "write : non blocking write requested, WriteListener enabled: " + this._listener);
            synchronized(this){                 
              _outHelper.write_NonBlocking(b, off, len);                
            }
        }           
        else {
            _outHelper.writeWork(b, off, len);
            
        }

    }
562598
public int read(byte b[], int off, int len) throws IOException {
	if (off + len > b.length) {
	    len = b.length - off;
	}
	if (len < 0) {
	    return -1;
	}
	if (len == 0) {
	    return 0;
	}
	int n = blob.read(b, off, pos, len);
	if (n > 0) {
	    pos += n;
	    return n;
	}
	return -1;
    }
296350
@Override
  public int read(byte[] b, int off, int len) throws IOException {
    int bytes = inner.read(b, off, Math.min(len, remaining));
    if (bytes > 0) {
      remaining -= bytes;
    }
    return bytes;
  }
889212
@Override
	public int read(byte[] b, int off, int len) throws IOException {
		if (currentEntry != null) {
			if (currentFileSize == currentEntry.getSize()) {
				return -1;
			} else if ((currentEntry.getSize() - currentFileSize) < len) {
				len = (int) (currentEntry.getSize() - currentFileSize);
			}
		}

		int br = super.read(b, off, len);

		if (br != -1) {
			if (currentEntry != null) {
				currentFileSize += br;
			}

			bytesRead += br;
		}

		return br;
	}
1055961
@Override
  public int read(byte[] b, int off, int len) throws IOException {
    if (b == null) {
      throw new NullPointerException();
    } else if (off < 0 || len < 0 || len > b.length - off) {
      throw new IndexOutOfBoundsException();
    } else if (len == 0) {
      return 0;
    }
    if (endOfInput && !bbuf.hasRemaining()) {
      return -1;
    }

    int totalRead = 0;
    while (len > 0 && !endOfInput) {
      if (bbuf.hasRemaining()) {
        int remaining = Math.min(len, bbuf.remaining());
        bbuf.get(b, off, remaining);
        totalRead += remaining;
        off += remaining;
        len -= remaining;
        if (len == 0) {
          return totalRead;
        }
      }
      advance();
    }
    if (endOfInput && !bbuf.hasRemaining() && totalRead == 0) {
      return -1;
    }
    return totalRead;
  }
652842
public int read(char []cbuf, int off, int len)
    throws IOException
  {
    int i = 0;

    for (i = 0; i < len; i++) {
      int ch1 = is.read();
      int ch2 = is.read();

      if (ch1 < 0)
        return i == 0 ? -1 : i;

      cbuf[off + i] = (char) ((ch2 << 8) + ch1);
    }

    return i;
  }
766054
protected void writeDirectBytes(byte[] b, int off, int len)
			throws IOException {
		ostream.write(b, off, len);
		len += len;
	}
1302519
@Override
    public synchronized void write(byte[] b, int off, int len) throws IOException {
        super.write(b, off, len);
        this.branch.write(b, off, len);
    }
85467
public synchronized void readFully(byte[] b, int off, int len) throws IOException {
		dataInputStream.readFully(b, off, len);
	}
796853
public synchronized void write(byte[] b, int off, int len) throws IOException {
		dataOutputStream.write(b, off, len);
		dataOutputStream.flush();
	}
964224
@Override
    public void write(@Sensitive byte[] b, int off, int len) throws IOException {
        this.output.write(b, off, len);
    }