public byte[] encodeNum(long l, int maxBytes) {
    byte[] rv = new byte[maxBytes];
    for (int i = 0; i < rv.length; i++) {
      int pos = rv.length - i - 1;
      rv[pos] = (byte) ((l >> (8 * i)) & 0xff);
    }
    if (packZeros) {
      int firstNon0 = 0;
      // Just looking for what we can reduce
      while (firstNon0 < rv.length && rv[firstNon0] == 0) {
        firstNon0++;
      }
      if (firstNon0 > 0) {
        byte[] tmp = new byte[rv.length - firstNon0];
        System.arraycopy(rv, firstNon0, tmp, 0, rv.length - firstNon0);
        rv = tmp;
      }
    }
    return rv;
  }
Get an instance of TranscoderUtils.

@param pack if true, remove all zero bytes from the MSB of the packed num
src/main/java/net/spy/memcached/transcoders/TranscoderUtils.java