public static <T> byte[] toByteArray(T message, Schema<T> schema,
            boolean encodeNestedMessageAsGroup)
    {
        try
        {
            final ComputedSizeOutput sizeCount = new ComputedSizeOutput(encodeNestedMessageAsGroup);
            schema.writeTo(sizeCount, message);
            final byte[] result = new byte[sizeCount.getSize()];
            final CodedOutput output = new CodedOutput(result, 0, result.length, sizeCount,
                    encodeNestedMessageAsGroup);
            schema.writeTo(output, message);
            output.checkNoSpaceLeft();
            return result;
        }
        catch (IOException e)
        {
            throw new RuntimeException("Serializing to a byte array threw an IOException " +
                    "(should never happen).", e);
        }
    }
Computes the buffer size and serializes the {@code message} tied to a schema into a byte array.
protostuff-core/src/test/java/io/protostuff/CodedOutput.java