/**
     * Serializes a long to a binary stream with zero-compressed encoding. For -112 <= i <= 127,
     * only one byte is used with the actual value. For other values of i, the first byte value
     * indicates whether the long is positive or negative, and the number of bytes that follow. If
     * the first byte value v is between -113 and -120, the following long is positive, with number
     * of bytes that follow are -(v+112). If the first byte value v is between -121 and -128, the
     * following long is negative, with number of bytes that follow are -(v+120). Bytes are stored
     * in the high-non-zero-byte-first order.
     *
     * @param buffer DrillBuf to write to
     * @param i Long to be serialized
     */
    public static void writeVLong(DrillBuf buffer, int start, int end, long i) {
      int availableBytes = (end - start);
      if (availableBytes < getVIntSize(i)) {
        throw new NumberFormatException(
            "Expected "
                + getVIntSize(i)
                + " bytes but the buffer '"
                + DrillStringUtils.toBinaryString(buffer, start, end)
                + "' has only "
                + availableBytes
                + " bytes.");
      }
      buffer.writerIndex(start);

      if (i >= -112 && i <= 127) {
        buffer.writeByte((byte) i);
        return;
      }

      int len = -112;
      if (i < 0) {
        i ^= -1L; // take one's complement'
        len = -120;
      }

      long tmp = i;
      while (tmp != 0) {
        tmp = tmp >> 8;
        len--;
      }

      buffer.writeByte((byte) len);

      len = (len < -120) ? -(len + 120) : -(len + 112);

      for (int idx = len; idx != 0; idx--) {
        int shiftbits = (idx - 1) * 8;
        long mask = 0xFFL << shiftbits;
        buffer.writeByte((byte) ((i & mask) >> shiftbits));
      }
    }
Beispiel #2
0
 @Override
 public DrillBuf slice(int index, int length) {
   DrillBuf buf = new DrillBuf(this, index, length);
   buf.writerIndex = length;
   return buf;
 }