private void write(final ByteBuffer buffer, final int length)
    {
        buffer.position(0);
        buffer.limit(length);

        if (channel != null)
        {
            try
            {
                channel.write(buffer);
            }
            catch (final IOException ex)
            {
                LangUtil.rethrowUnchecked(ex);
            }
        }
        else if (resultBuffer != null)
        {
            resultBuffer.put(buffer);
        }

        totalLength += length;
    }
Encode the provided {@link Ir} and return the length in bytes encoded.

@return encode the provided {@link Ir} and return the length in bytes encoded.
sbe-tool/src/main/java/uk/co/real_logic/sbe/ir/IrEncoder.java