code: public byte[] toByteArray() {
      try {
        final byte[] result = new byte[getSerializedSize()];
        final CodedOutputStream output = CodedOutputStream.newInstance(result);
        writeTo(output);
        output.checkNoSpaceLeft();
        return result;
      } catch (final IOException e) {
        throw new RuntimeException(
          "Serializing to a byte array threw an IOException " +
          "(should never happen).", e);
      }
    }
docs: Serializes the message to a {@code byte} array and returns it.  This is
  just a trivial wrapper around {@link #writeTo(CodedOutputStream)}.