code: public static byte[] serialize(Serializable obj, boolean gzipOnSerialize) {
          try {
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              ObjectOutputStream out;
              if (gzipOnSerialize) { 
                  out = new ObjectOutputStream(new GZIPOutputStream(baos));
              } else {
                  out = new ObjectOutputStream(baos);
              }
              out.writeObject(obj);
              out.flush();
              out.close();
              return baos.toByteArray();
          } catch (IOException e) {
              throw new RuntimeException(e);
          }
      }
docs: Serializes and gzips an object.