@Override
    public int encode(byte[] array, int blkptr, int len)
    {
        if ((array == null) || (blkptr + len > array.length) || (blkptr < 0) || (len < 0))
           return -1;

        final int len8 = len & -8;
        final int end8 = blkptr + len8;
        int i = blkptr;

        try
        {
           while (i < end8)
           {            
              if (this.encodeLong(array, i) == false)
                 return i;

              i += 8;
           }
           
           while (i < blkptr + len)
           {            
              this.bitstream.writeBits(array[i], 8);
              i++;
           }
        }
        catch (BitStreamException e)
        {
           return i - blkptr;
        }

        return len;
    }