<commit_before>/*
 * Copyright 2014-2015 Real Logic Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.co.real_logic.agrona.concurrent;

import uk.co.real_logic.agrona.DirectBuffer;
import uk.co.real_logic.agrona.IoUtil;
import uk.co.real_logic.agrona.MutableDirectBuffer;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

import static uk.co.real_logic.agrona.BitUtil.*;
import static uk.co.real_logic.agrona.concurrent.MemoryAccess.memory;
import static uk.co.real_logic.agrona.concurrent.UnsafeBuffer.*;

/**
 * Supports regular, byte ordered, and atomic (memory ordered) access to an underlying buffer.
 *
 * This buffer is resizable and based upon an underlying
 */
public class MappedResizeableBuffer implements AtomicBuffer, AutoCloseable
{

    private final FileChannel fileChannel;

    private long addressOffset;
    private long capacity;

    /**
     * Attach a view to an off-heap memory region by address.
     *
     * @param fileChannel the file to map
     * @param initialLength  of the buffer from the given address
     */
    public MappedResizeableBuffer(final FileChannel fileChannel, final long initialLength)
    {
        this.fileChannel = fileChannel;
        map(initialLength);
    }

    private void map(final long initialLength)
    {
        capacity = initialLength;
        addressOffset = IoUtil.map(fileChannel, FileChannel.MapMode.READ_WRITE, 0, initialLength);
    }

    private void unmap()
    {
        IoUtil.unmap(fileChannel, addressOffset, capacity);
    }

    public void resize(final long newLength)
    {
        if (newLength <= 0)
        {
            throw new IllegalArgumentException("Length must be a positive long, but is: " + newLength);
        }

        unmap();
        map(newLength);
    }

    public void wrap(final byte[] buffer)
    {
        throw new UnsupportedOperationException();
    }

    public void wrap(final byte[] buffer, final int offset, final int length)
    {
        throw new UnsupportedOperationException();
    }

    public void wrap(final ByteBuffer buffer)
    {
        throw new UnsupportedOperationException();
    }

    public void wrap(final ByteBuffer buffer, final int offset, final int length)
    {
        throw new UnsupportedOperationException();
    }

    public void wrap(final DirectBuffer buffer)
    {
        throw new UnsupportedOperationException();
    }

    public void wrap(final DirectBuffer buffer, final long offset, final int length)
    {
        throw new UnsupportedOperationException();
    }

    public void wrap(final long address, final int length)
    {
        throw new UnsupportedOperationException();
    }

    public long addressOffset()
    {
        return addressOffset;
    }

    public byte[] byteArray()
    {
        throw new UnsupportedOperationException();
    }
<commit_msg>add the necessary wrap methods to reuse this buffer with different filechannel/offset/length etc<commit_after>/*
 * Copyright 2014-2015 Real Logic Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.co.real_logic.agrona.concurrent;

import uk.co.real_logic.agrona.DirectBuffer;
import uk.co.real_logic.agrona.IoUtil;
import uk.co.real_logic.agrona.MutableDirectBuffer;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

import static uk.co.real_logic.agrona.BitUtil.*;
import static uk.co.real_logic.agrona.concurrent.MemoryAccess.memory;
import static uk.co.real_logic.agrona.concurrent.UnsafeBuffer.*;

/**
 * Supports regular, byte ordered, and atomic (memory ordered) access to an underlying buffer.
 *
 * This buffer is resizable and based upon an underlying
 */
public class MappedResizeableBuffer implements AtomicBuffer, AutoCloseable
{

    private FileChannel fileChannel;
    private long addressOffset;
    private long capacity;

    /**
     * Attach a view to an off-heap memory region by address.
     *
     * @param fileChannel the file to map
     * @param offset the offset of the file to start the mapping
     * @param initialLength  of the buffer from the given address
     */
    public MappedResizeableBuffer(final FileChannel fileChannel, final long offset, final long initialLength)
    {
        this.fileChannel = fileChannel;
        map(offset, initialLength);
    }

    private void map(final long offset, final long length)
    {
        capacity = length;
        addressOffset = IoUtil.map(fileChannel, FileChannel.MapMode.READ_WRITE, offset, length);
    }

    private void unmap()
    {
        IoUtil.unmap(fileChannel, addressOffset, capacity);
    }

    public void resize(final long newLength)
    {
        if (newLength <= 0)
        {
            throw new IllegalArgumentException("Length must be a positive long, but is: " + newLength);
        }

        unmap();
        map(0, newLength);
    }

    public void wrap(final byte[] buffer)
    {
        throw new UnsupportedOperationException();
    }

    public void wrap(final byte[] buffer, final int offset, final int length)
    {
        throw new UnsupportedOperationException();
    }

    public void wrap(final ByteBuffer buffer)
    {
        throw new UnsupportedOperationException();
    }

    public void wrap(final ByteBuffer buffer, final int offset, final int length)
    {
        throw new UnsupportedOperationException();
    }

    public void wrap(final DirectBuffer buffer)
    {
        throw new UnsupportedOperationException();
    }

    public void wrap(final DirectBuffer buffer, final long offset, final int length)
    {
        throw new UnsupportedOperationException();
    }

    public void wrap(final long address, final int length)
    {
        throw new UnsupportedOperationException();
    }

    /**
     * Remap the buffer using the existing file based on a new offset and length
     *
     * @param offset the offset of the file to start the mapping
     * @param length  of the buffer from the given address
     */
    public void wrap(final long offset, final long length)
    {
        if (offset == this.addressOffset && length == this.capacity)
        {
            return;
        }

        wrap(fileChannel, offset, length);
    }

    /**
     * Remap the buffer based on a new file, offset and a length
     *
     * @param fileChannel the file to map
     * @param offset the offset of the file to start the mapping
     * @param length  of the buffer from the given address
     */
    public void wrap(final FileChannel fileChannel, final long offset, final long length)
    {
        unmap();
        this.fileChannel = fileChannel;
        map(offset, length);
    }

    public long addressOffset()
    {
        return addressOffset;
    }

    public byte[] byteArray()
    {
        throw new UnsupportedOperationException();
    }

{'fraction_non_alphanumeric': 0.07649723200805233, 'fraction_numerical': 0.0035228988424760945, 'mean_word_length': 2.9488325881768507, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7662865', 'n_tokens_mistral': 2142, 'n_tokens_neox': 2067, 'n_words': 753}