code: protected long validateAndAdjustLenght(long length, long position, long dataLength) throws IOException
     {
        if (position < 0)
        {
           throw new IOException("Position must be higher or equals 0. But given " + position);
        }
  
        if (length < 0)
        {
           throw new IOException("Length must be higher or equals 0. But given " + length);
        }
  
        if (position >= dataLength && position > 0)
        {
           throw new IOException("Position " + position + " out of value size " + dataLength);
        }
  
        if (position + length >= dataLength)
        {
           return dataLength - position;
        }
  
        return length;
     }
docs: Validate parameters. <code>Length</code> and <code>position</code> should not be negative and
  <code>length</code> should not be greater than <code>dataLength</code>
  
  @return adjusted length of byte to read. Should not be possible to exceed array border.