public void encode(ByteSink out)
	throws IOException
 {
  // determine encoded size minus prefix size
  int size = getEncodedSize() - 1;
  if (MAX_ENCODED_SIZE <= size)
   throw new RuntimeException("Invalid stored length: size " + size + " too large");
  // prepare the prefix
  int base = 0;
  int rlen = 0;
  if (0 < size)
  {
   base = 1;
   for (int i = size;;)
   {
    if (0 == (i >>>= 1))
     break;
    base <<= 1;
    rlen++;
   }
  }
  // base is now 2^(prefix size - 1) or 0 for 0 prefix
  int temp = 0 == size ? 0 : (base << 1) - 1;
  // append 0 delimiter and make room for remainder 
  temp <<= 1 + rlen;
  // append the remainder
  temp |= size - base;
  // make room for value bits
  base = 0 == size ? 7 : 6 - rlen - rlen;
  temp <<= base;
  // append value bits if applicable
  if (8 > size)
  {
   // sanity check
   if ((1L << base + (size << 3)) <= value)
    throw new RuntimeException("Internal error encoding length " + value);
   temp |= (int)(value >>> (size << 3));
  }
  // write prefix, remainder and high-order bits
  out.writeByte(temp);
  // write the rest of value
  while (0 < size--)
  {
   temp = (int)(value >>> (size << 3)) & 0xFF;
   out.writeByte(temp);
  }
 }