class BinaryOffheapOutputStream extends BinaryAbstractOutputStream {
    /** Pointer. */
    private long ptr;

    /** Length of bytes that cen be used before resize is necessary. */
    private int cap;

    /**
     * Constructor.
     *
     * @param cap Capacity.
     */
    public BinaryOffheapOutputStream(int cap) {
        this(0, cap);
    }

    /**
     * Constructor.
     *
     * @param ptr Pointer to existing address.
     * @param cap Capacity.
     */
    public BinaryOffheapOutputStream(long ptr, int cap) {
        this.ptr = ptr == 0 ? allocate(cap) : ptr;

        this.cap = cap;
    }

    /** {@inheritDoc} */
    @Override public void close() {
        release(ptr);
    }

    /** {@inheritDoc} */
    @Override public void ensureCapacity(int cnt) {
        // overflow-conscious code
        if (cnt - cap > 0) {
            int newCap = capacity(cap, cnt);

            ptr = reallocate(ptr, newCap);

            cap = newCap;
        }
    }

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

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

        GridUnsafe.copyOffheapHeap(ptr, res, GridUnsafe.BYTE_ARR_OFF, pos);

        return res;
    }

    /**
     * @return Pointer.
     */
    public long pointer() {
        return ptr;
    }

    /** {@inheritDoc} */
    @Override public int capacity() {
        return cap;
    }

    /** {@inheritDoc} */
    @Override protected void writeByteAndShift(byte val) {
        GridUnsafe.putByte(ptr + pos++, val);
    }

    /** {@inheritDoc} */
    @Override protected void copyAndShift(Object src, long offset, int len) {
        GridUnsafe.copyHeapOffheap(src, offset, ptr + pos, len);

        shift(len);
    }

    /** {@inheritDoc} */
    @Override protected void writeShortFast(short val) {
        long addr = ptr + pos;

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

    /** {@inheritDoc} */
    @Override protected void writeCharFast(char val) {
        long addr = ptr + pos;

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

    /** {@inheritDoc} */
    @Override protected void writeIntFast(int val) {<FILL_FUNCTION_BODY>}

    /** {@inheritDoc} */
    @Override protected void writeLongFast(long val) {
        long addr = ptr + pos;

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

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

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

    /** {@inheritDoc} */
    @Override public void unsafeWriteShort(short val) {
        long addr = ptr + pos;

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

        shift(2);
    }

    /** {@inheritDoc} */
    @Override public void unsafeWriteShort(int pos, short val) {
        long addr = ptr + pos;

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

    /** {@inheritDoc} */
    @Override public void unsafeWriteChar(char val) {
        long addr = ptr + pos;

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

        shift(2);
    }

    /** {@inheritDoc} */
    @Override public void unsafeWriteInt(int val) {
        long addr = ptr + pos;

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

        shift(4);
    }

    /** {@inheritDoc} */
    @Override public void unsafeWriteInt(int pos, int val) {
        long addr = ptr + pos;

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

    /** {@inheritDoc} */
    @Override public void unsafeWriteLong(long val) {
        long addr = ptr + pos;

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

        shift(8);
    }

    /**
     * Allocate memory.
     *
     * @param cap Capacity.
     * @return Pointer.
     */
    protected long allocate(int cap) {
        return GridUnsafe.allocateMemory(cap);
    }

    /**
     * Reallocate memory.
     *
     * @param ptr Old pointer.
     * @param cap Capacity.
     * @return New pointer.
     */
    protected long reallocate(long ptr, int cap) {
        return GridUnsafe.reallocateMemory(ptr, cap);
    }

    /**
     * Release memory.
     *
     * @param ptr Pointer.
     */
    protected void release(long ptr) {
        GridUnsafe.freeMemory(ptr);
    }
}

        long addr = ptr + pos;

        if (BIG_ENDIAN)
            GridUnsafe.putIntLE(addr, val);
        else
            GridUnsafe.putInt(addr, val);
    
<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