@Override
  public void serialize(DataOutput out, E value) throws IOException {
    DataOutput2 out2 = new DataOutput2();
    serializer.serialize(out2, value);

    byte[] tmp = new byte[out2.pos + 41];
    int newLen;
    try {
      newLen = LZF.get().compress(out2.buf, out2.pos, tmp, 0);
    } catch (IndexOutOfBoundsException e) {
      newLen = 0; // larger after compression
    }
    if (newLen >= out2.pos) {
      // compression adds size, so do not compress
      DataOutput2.packInt(out, 0);
      out.write(out2.buf, 0, out2.pos);
      return;
    }

    DataOutput2.packInt(out, out2.pos + 1); // unpacked size, zero indicates no compression
    out.write(tmp, 0, newLen);
  }