private void writeMessage(Descriptors.FieldDescriptor fd, Object value, Class clazz) throws IOException {
if (MessageLite.class.isAssignableFrom(clazz)) {
messageContext.out.writeMessage(fd.getNumber(), (MessageLite) value);
} else {
BaseMarshaller marshaller = ctx.getMarshaller(clazz);
ByteArrayOutputStream baos = new ByteArrayOutputStream(); //todo here we should use a better buffer allocation strategy
CodedOutputStream out = CodedOutputStream.newInstance(baos);
enterContext(fd.getName(), fd.getMessageType(), out);
marshall(value, marshaller, out);
out.flush();