class BinaryHeapOutputStream extends BinaryAbstractOutputStream {
    /** Allocator. */
    private final BinaryMemoryAllocatorChunk chunk;

    /** Disable auto close flag. */
    private final boolean disableAutoClose;

    /** Data. */
    private byte[] data;

    /**
     * Constructor.
     *
     * @param cap Initial capacity.
     */
    public BinaryHeapOutputStream(int cap) {
        this(cap, BinaryMemoryAllocator.THREAD_LOCAL.chunk());
    }

    /**
     * Constructor.
     *
     * @param cap Capacity.
     * @param chunk Chunk.
     */
    public BinaryHeapOutputStream(int cap, BinaryMemoryAllocatorChunk chunk) {
        this(cap, chunk, false);
    }

    /**
     * Constructor.
     *
     * @param cap Capacity.
     * @param chunk Chunk.
     * @param disableAutoClose Whether to disable resource release in {@link BinaryHeapOutputStream#close()} method
     *                         so that an explicit {@link BinaryHeapOutputStream#release()} call is required.
     */
    public BinaryHeapOutputStream(int cap, BinaryMemoryAllocatorChunk chunk, boolean disableAutoClose) {
        this.chunk = chunk;
        this.disableAutoClose = disableAutoClose;

        data = chunk.allocate(cap);
    }

    /** {@inheritDoc} */
    @Override public void close() {
        if (!disableAutoClose)
            release();
    }

    /**
     * Releases pooled memory.
     */
    public void release() {
        chunk.release(data, pos);
    }

    /** {@inheritDoc} */
    @Override public void ensureCapacity(int cnt) {<FILL_FUNCTION_BODY>}

    /** {@inheritDoc} */
    @Override public byte[] array() {
        return data;
    }

    /** {@inheritDoc} */
    @Override public byte[] arrayCopy() {
        byte[] res = new byte[pos];

        System.arraycopy(data, 0, res, 0, pos);

        return res;
    }

    /** {@inheritDoc} */
    @Override public boolean hasArray() {
        return true;
    }

    /** {@inheritDoc} */
    @Override protected void writeByteAndShift(byte val) {
        data[pos++] = val;
    }

    /** {@inheritDoc} */
    @Override protected void copyAndShift(Object src, long off, int len) {
        GridUnsafe.copyMemory(src, off, data, GridUnsafe.BYTE_ARR_OFF + pos, len);

        shift(len);
    }

    /** {@inheritDoc} */
    @Override protected void writeShortFast(short val) {
        long off = GridUnsafe.BYTE_ARR_OFF + pos;

        if (BIG_ENDIAN)
            GridUnsafe.putShortLE(data, off, val);
        else
            GridUnsafe.putShort(data, off, val);
    }

    /** {@inheritDoc} */
    @Override protected void writeCharFast(char val) {
        long off = GridUnsafe.BYTE_ARR_OFF + pos;

        if (BIG_ENDIAN)
            GridUnsafe.putCharLE(data, off, val);
        else
            GridUnsafe.putChar(data, off, val);
    }

    /** {@inheritDoc} */
    @Override protected void writeIntFast(int val) {
        long off = GridUnsafe.BYTE_ARR_OFF + pos;

        if (BIG_ENDIAN)
            GridUnsafe.putIntLE(data, off, val);
        else
            GridUnsafe.putInt(data, off, val);
    }

    /** {@inheritDoc} */
    @Override protected void writeLongFast(long val) {
        long off = GridUnsafe.BYTE_ARR_OFF + pos;

        if (BIG_ENDIAN)
            GridUnsafe.putLongLE(data, off, val);
        else
            GridUnsafe.putLong(data, off, val);
    }

    /** {@inheritDoc} */
    @Override public void unsafeWriteByte(byte val) {
        GridUnsafe.putByte(data, GridUnsafe.BYTE_ARR_OFF + pos++, val);
    }

    /** {@inheritDoc} */
    @Override public void unsafeWriteShort(short val) {
        long off = GridUnsafe.BYTE_ARR_OFF + pos;

        if (BIG_ENDIAN)
            GridUnsafe.putShortLE(data, off, val);
        else
            GridUnsafe.putShort(data, off, val);

        shift(2);
    }

    /** {@inheritDoc} */
    @Override public void unsafeWriteShort(int pos, short val) {
        long off = GridUnsafe.BYTE_ARR_OFF + pos;

        if (BIG_ENDIAN)
            GridUnsafe.putShortLE(data, off, val);
        else
            GridUnsafe.putShort(data, off, val);
    }

    /** {@inheritDoc} */
    @Override public void unsafeWriteChar(char val) {
        long off = GridUnsafe.BYTE_ARR_OFF + pos;

        if (BIG_ENDIAN)
            GridUnsafe.putCharLE(data, off, val);
        else
            GridUnsafe.putChar(data, off, val);

        shift(2);
    }

    /** {@inheritDoc} */
    @Override public void unsafeWriteInt(int val) {
        long off = GridUnsafe.BYTE_ARR_OFF + pos;

        if (BIG_ENDIAN)
            GridUnsafe.putIntLE(data, off, val);
        else
            GridUnsafe.putInt(data, off, val);

        shift(4);
    }

    /** {@inheritDoc} */
    @Override public void unsafeWriteInt(int pos, int val) {
        long off = GridUnsafe.BYTE_ARR_OFF + pos;

        if (BIG_ENDIAN)
            GridUnsafe.putIntLE(data, off, val);
        else
            GridUnsafe.putInt(data, off, val);
    }

    /** {@inheritDoc} */
    @Override public void unsafeWriteLong(long val) {
        long off = GridUnsafe.BYTE_ARR_OFF + pos;

        if (BIG_ENDIAN)
            GridUnsafe.putLongLE(data, off, val);
        else
            GridUnsafe.putLong(data, off, val);

        shift(8);
    }

    /** {@inheritDoc} */
    @Override public int capacity() {
        return data.length;
    }
}

        // overflow-conscious code
        if (cnt - data.length > 0) {
            int newCap = capacity(data.length, cnt);

            data = chunk.reallocate(data, newCap);
        }
    
<methods>public non-sealed void <init>() ,public long offheapPointer() ,public void position(int) ,public long rawOffheapPointer() ,public void unsafeEnsure(int) ,public void unsafePosition(int) ,public void unsafeWriteBoolean(boolean) ,public void unsafeWriteDouble(double) ,public void unsafeWriteFloat(float) ,public void write(byte[], int, int) ,public void write(long, int) ,public void writeBoolean(boolean) ,public void writeBooleanArray(boolean[]) ,public void writeByte(byte) ,public void writeByteArray(byte[]) ,public void writeChar(char) ,public void writeCharArray(char[]) ,public void writeDouble(double) ,public void writeDoubleArray(double[]) ,public void writeFloat(float) ,public void writeFloatArray(float[]) ,public void writeInt(int) ,public void writeInt(int, int) ,public void writeIntArray(int[]) ,public void writeLong(long) ,public void writeLongArray(long[]) ,public void writeShort(short) ,public void writeShort(int, short) ,public void writeShortArray(short[]) <variables>protected static final int MAX_ARRAY_SIZE,private static final int MIN_CAP