<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.pdfbox.io;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * An implementation of the RandomAccess interface to store a pdf in memory.
 * The data will be stored in chunks organized in an ArrayList.
 *
 */
public class RandomAccessBuffer implements RandomAccess, Closeable, Cloneable
{
    // default chunk size is 1kb
    private static final int DEFAULT_CHUNK_SIZE = 1024;
    // use the default chunk size
    private int chunkSize = DEFAULT_CHUNK_SIZE;
    // list containing all chunks
    private List<byte[]> bufferList = null;
    // current chunk
    private byte[] currentBuffer;
    // current pointer to the whole buffer
    private long pointer;
    // current pointer for the current chunk
    private int currentBufferPointer;
    // size of the whole buffer
    private long size;
    // current chunk list index
    private int bufferListIndex;
    // maximum chunk list index
    private int bufferListMaxIndex;

    /**
     * Default constructor.
     */
    public RandomAccessBuffer()
    {
        // starting with one chunk
        bufferList = new ArrayList<byte[]>();
        currentBuffer = new byte[chunkSize];
        bufferList.add(currentBuffer);
        pointer = 0;
        currentBufferPointer = 0;
        size = 0;
        bufferListIndex = 0;
        bufferListMaxIndex = 0;
    }

    /**
     * Create a random access buffer using the given byte array.
     * 
     * @param input the byte array to be read
     */
    public RandomAccessBuffer(byte[] input)
    {
        // this is a special case. The given byte array is used as the one
        // and only chunk.
        bufferList = new ArrayList<byte[]>(1);
        chunkSize = input.length;
        currentBuffer = input;
        bufferList.add(currentBuffer);
        pointer = 0;
        currentBufferPointer = 0;
        size = chunkSize;
        bufferListIndex = 0;
        bufferListMaxIndex = 0;
    }

    /**
     * Create a random access buffer of the given input stream by copying the data.
     * 
     * @param input the input stream to be read
     * @throws IOException if something went wrong while copying the data
     */
    public RandomAccessBuffer(InputStream input) throws IOException
    {
        this();
        byte[] byteBuffer = new byte[8192];
        int bytesRead = 0;
        while ((bytesRead = input.read(byteBuffer)) > -1)
        {
            write(byteBuffer, 0, bytesRead);
        }
        seek(0);
    }

    @Override
    public RandomAccessBuffer clone()
    {
        RandomAccessBuffer copy = new RandomAccessBuffer();

        copy.bufferList = new ArrayList<byte[]>(bufferList.size());
        for (byte [] buffer : bufferList)
        {
            byte [] newBuffer = new byte [buffer.length];
            System.arraycopy(buffer,0,newBuffer,0,buffer.length);
            copy.bufferList.add(newBuffer);
        }
        if (currentBuffer!=null)
        {
            copy.currentBuffer = copy.bufferList.get(copy.bufferList.size()-1);
        }
        else
        {
            copy.currentBuffer = null;
        }
        copy.pointer = pointer;
        copy.currentBufferPointer = currentBufferPointer;
        copy.size = size;
        copy.bufferListIndex = bufferListIndex;
        copy.bufferListMaxIndex = bufferListMaxIndex;

        return copy;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void close() throws IOException
    {
        currentBuffer = null;
        bufferList.clear();
        pointer = 0;
        currentBufferPointer = 0;
        size = 0;
        bufferListIndex = 0;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void clear()
    {
        bufferList.clear();
        currentBuffer = new byte[chunkSize];
        bufferList.add(currentBuffer);
        pointer = 0;
        currentBufferPointer = 0;
        size = 0;
        bufferListIndex = 0;
        bufferListMaxIndex = 0;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void seek(long position) throws IOException
    {
        checkClosed();
        if (position < 0)
        {
            throw new IOException("Invalid position "+position);
        }
        pointer = position;
        if (pointer <= size)
        {
            // calculate the chunk list index
            bufferListIndex = (int)(position / chunkSize);
            currentBufferPointer = (int)(position % chunkSize);
            currentBuffer = bufferList.get(bufferListIndex);
        }
        else
        {
            // it is allowed to jump beyond the end of the file
            // jump to the end of the buffer
            bufferListIndex = bufferListMaxIndex;
            currentBuffer = bufferList.get(bufferListIndex);
            currentBufferPointer = (int)(size - ((bufferListMaxIndex-1)*chunkSize));
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public long getPosition() throws IOException
    {
       checkClosed();
       return pointer;
    }
    <commit_msg>PDFBOX-2301: seek bugfix<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.pdfbox.io;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * An implementation of the RandomAccess interface to store a pdf in memory.
 * The data will be stored in chunks organized in an ArrayList.
 *
 */
public class RandomAccessBuffer implements RandomAccess, Closeable, Cloneable
{
    // default chunk size is 1kb
    private static final int DEFAULT_CHUNK_SIZE = 1024;
    // use the default chunk size
    private int chunkSize = DEFAULT_CHUNK_SIZE;
    // list containing all chunks
    private List<byte[]> bufferList = null;
    // current chunk
    private byte[] currentBuffer;
    // current pointer to the whole buffer
    private long pointer;
    // current pointer for the current chunk
    private int currentBufferPointer;
    // size of the whole buffer
    private long size;
    // current chunk list index
    private int bufferListIndex;
    // maximum chunk list index
    private int bufferListMaxIndex;

    /**
     * Default constructor.
     */
    public RandomAccessBuffer()
    {
        // starting with one chunk
        bufferList = new ArrayList<byte[]>();
        currentBuffer = new byte[chunkSize];
        bufferList.add(currentBuffer);
        pointer = 0;
        currentBufferPointer = 0;
        size = 0;
        bufferListIndex = 0;
        bufferListMaxIndex = 0;
    }

    /**
     * Create a random access buffer using the given byte array.
     * 
     * @param input the byte array to be read
     */
    public RandomAccessBuffer(byte[] input)
    {
        // this is a special case. The given byte array is used as the one
        // and only chunk.
        bufferList = new ArrayList<byte[]>(1);
        chunkSize = input.length;
        currentBuffer = input;
        bufferList.add(currentBuffer);
        pointer = 0;
        currentBufferPointer = 0;
        size = chunkSize;
        bufferListIndex = 0;
        bufferListMaxIndex = 0;
    }

    /**
     * Create a random access buffer of the given input stream by copying the data.
     * 
     * @param input the input stream to be read
     * @throws IOException if something went wrong while copying the data
     */
    public RandomAccessBuffer(InputStream input) throws IOException
    {
        this();
        byte[] byteBuffer = new byte[8192];
        int bytesRead = 0;
        while ((bytesRead = input.read(byteBuffer)) > -1)
        {
            write(byteBuffer, 0, bytesRead);
        }
        seek(0);
    }

    @Override
    public RandomAccessBuffer clone()
    {
        RandomAccessBuffer copy = new RandomAccessBuffer();

        copy.bufferList = new ArrayList<byte[]>(bufferList.size());
        for (byte [] buffer : bufferList)
        {
            byte [] newBuffer = new byte [buffer.length];
            System.arraycopy(buffer,0,newBuffer,0,buffer.length);
            copy.bufferList.add(newBuffer);
        }
        if (currentBuffer!=null)
        {
            copy.currentBuffer = copy.bufferList.get(copy.bufferList.size()-1);
        }
        else
        {
            copy.currentBuffer = null;
        }
        copy.pointer = pointer;
        copy.currentBufferPointer = currentBufferPointer;
        copy.size = size;
        copy.bufferListIndex = bufferListIndex;
        copy.bufferListMaxIndex = bufferListMaxIndex;

        return copy;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void close() throws IOException
    {
        currentBuffer = null;
        bufferList.clear();
        pointer = 0;
        currentBufferPointer = 0;
        size = 0;
        bufferListIndex = 0;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void clear()
    {
        bufferList.clear();
        currentBuffer = new byte[chunkSize];
        bufferList.add(currentBuffer);
        pointer = 0;
        currentBufferPointer = 0;
        size = 0;
        bufferListIndex = 0;
        bufferListMaxIndex = 0;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void seek(long position) throws IOException
    {
        checkClosed();
        if (position < 0)
        {
            throw new IOException("Invalid position "+position);
        }
        pointer = position;
        if (pointer < size)
        {
            // calculate the chunk list index
            bufferListIndex = (int)(pointer / chunkSize);
            currentBufferPointer = (int)(pointer % chunkSize);
            currentBuffer = bufferList.get(bufferListIndex);
        }
        else
        {
            // it is allowed to jump beyond the end of the file
            // jump to the end of the buffer
            bufferListIndex = bufferListMaxIndex;
            currentBuffer = bufferList.get(bufferListIndex);
            currentBufferPointer = (int)(size % chunkSize);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public long getPosition() throws IOException
    {
       checkClosed();
       return pointer;
    }
    
{'fraction_non_alphanumeric': 0.08509017018034036, 'fraction_numerical': 0.0071966810600287865, 'mean_word_length': 2.124867724867725, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, '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': '7059340', 'n_tokens_mistral': 3087, 'n_tokens_neox': 2998, 'n_words': 1026}