<commit_before>
package com.esotericsoftware.kryo.io;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;

import com.esotericsoftware.kryo.KryoException;
import com.esotericsoftware.kryo.util.UnsafeUtil;

/** An InputStream that reads data from a byte array and optionally fills the byte array from another InputStream as needed.
 * Utility methods are provided for efficiently reading primitive types and strings.
 * 
 * @author <NAME> <<EMAIL>> */
public class ByteBufferInput extends Input {
	protected ByteBuffer niobuffer;

	protected boolean varIntsEnabled = true;

	/* Default byte order is BIG_ENDIAN to be compatible to the base class */
	ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;

	protected final static ByteOrder nativeOrder = ByteOrder.nativeOrder();

	/** Creates an uninitialized Input. {@link #setBuffer(byte[])} must be called before the Input is used. */
	public ByteBufferInput () {
	}

	/** Creates a new Input for reading from a byte array.
	 * @param bufferSize The size of the buffer. An exception is thrown if more bytes than this are read. */
	public ByteBufferInput (int bufferSize) {
		this.capacity = bufferSize;
		niobuffer = ByteBuffer.allocateDirect(bufferSize);
		niobuffer.order(byteOrder);
	}

	/** Creates a new Input for reading from a byte array.
	 * @param buffer An exception is thrown if more bytes than this are read. */
	public ByteBufferInput (ByteBuffer buffer) {
		this(buffer, 0, buffer.position());
	}

	public ByteBufferInput (byte[] buffer) {
		setBuffer(buffer);
	}

	/** Creates a new Input for reading from a byte array.
	 * @param buffer An exception is thrown if more bytes than this are read. */
	public ByteBufferInput (ByteBuffer buffer, int offset, int count) {
		ByteBuffer newBuffer = buffer.duplicate();
		newBuffer.order(byteOrder);
		newBuffer.limit(count);
		newBuffer.rewind();
		setBuffer(newBuffer, offset, count);
	}

	/** Creates a new Input for reading from an InputStream with a buffer size of 4096. */
	public ByteBufferInput (InputStream inputStream) {
		this(4096);
		if (inputStream == null) throw new IllegalArgumentException("inputStream cannot be null.");
		this.inputStream = inputStream;
	}

	/** Creates a new Input for reading from an InputStream. */
	public ByteBufferInput (InputStream inputStream, int bufferSize) {
		this(bufferSize);
		if (inputStream == null) throw new IllegalArgumentException("inputStream cannot be null.");
		this.inputStream = inputStream;
	}

	public ByteOrder order () {
		return byteOrder;
	}

	public void order (ByteOrder byteOrder) {
		this.byteOrder = byteOrder;
	}

	/** Sets a new buffer. The position and total are reset, discarding any buffered bytes. */
	public void setBuffer (byte[] bytes) {
		ByteBuffer directBuffer = ByteBuffer.allocateDirect(bytes.length);
		directBuffer.put(bytes);
		directBuffer.position(0);
		directBuffer.order(byteOrder);
		setBuffer(directBuffer, 0, bytes.length);
	}

	/** Sets a new buffer. The position and total are reset, discarding any buffered bytes. */
	public void setBuffer (ByteBuffer bytes, int offset, int count) {
		if (bytes == null) throw new IllegalArgumentException("bytes cannot be null.");
		niobuffer = bytes;
		position = offset;
		limit = count;
		capacity = bytes.capacity();
		total = 0;
		inputStream = null;
		niobuffer.position(position);
	}

	/*** Release a direct buffer. {@link #setBuffer(ByteBuffer, int, int)} should be called before next write operations can be
	 * called. */
	public void release () {
		close();
		UnsafeUtil.releaseBuffer(niobuffer);
		niobuffer = null;
	}

	/*** This constructor allows for creation of a direct ByteBuffer of a given size at a given address.
	 * 
	 * <p>
	 * Typical usage could look like this snippet:
	 * 
	 * <pre>
	 * // Explicitly allocate memory
	 * long bufAddress = UnsafeUtil.unsafe().allocateMemory(4096);
	 * // Create a ByteBufferInput using the allocated memory region
	 * ByteBufferInput buffer = new ByteBufferInput(bufAddress, 4096);
	 * 
	 * // Do some operations on this buffer here
	 * 
	 * // Say that ByteBuffer won't be used anymore
	 * buffer.release();<commit_msg>One more place that required a fix for the issue #123.<commit_after>
package com.esotericsoftware.kryo.io;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;

import com.esotericsoftware.kryo.KryoException;
import com.esotericsoftware.kryo.util.UnsafeUtil;

/** An InputStream that reads data from a byte array and optionally fills the byte array from another InputStream as needed.
 * Utility methods are provided for efficiently reading primitive types and strings.
 * 
 * @author <NAME> <<EMAIL>> */
public class ByteBufferInput extends Input {
	protected ByteBuffer niobuffer;

	protected boolean varIntsEnabled = true;

	/* Default byte order is BIG_ENDIAN to be compatible to the base class */
	ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;

	protected final static ByteOrder nativeOrder = ByteOrder.nativeOrder();

	/** Creates an uninitialized Input. {@link #setBuffer(byte[])} must be called before the Input is used. */
	public ByteBufferInput () {
	}

	/** Creates a new Input for reading from a byte array.
	 * @param bufferSize The size of the buffer. An exception is thrown if more bytes than this are read. */
	public ByteBufferInput (int bufferSize) {
		this.capacity = bufferSize;
		niobuffer = ByteBuffer.allocateDirect(bufferSize);
		niobuffer.order(byteOrder);
	}

	/** Creates a new Input for reading from a byte array.
	 * @param buffer An exception is thrown if more bytes than this are read. */
	public ByteBufferInput (ByteBuffer buffer) {
		this(buffer, 0, buffer.position());
	}

	public ByteBufferInput (byte[] buffer) {
		setBuffer(buffer);
	}

	/** Creates a new Input for reading from a byte array.
	 * @param buffer An exception is thrown if more bytes than this are read. */
	public ByteBufferInput (ByteBuffer buffer, int offset, int count) {
		ByteBuffer newBuffer = buffer.duplicate();
		newBuffer.order(byteOrder);
		newBuffer.limit(count);
		newBuffer.rewind();
		setBuffer(newBuffer, offset, count);
	}

	/** Creates a new Input for reading from an InputStream with a buffer size of 4096. */
	public ByteBufferInput (InputStream inputStream) {
		this(4096);
		if (inputStream == null) throw new IllegalArgumentException("inputStream cannot be null.");
		this.inputStream = inputStream;
	}

	/** Creates a new Input for reading from an InputStream. */
	public ByteBufferInput (InputStream inputStream, int bufferSize) {
		this(bufferSize);
		if (inputStream == null) throw new IllegalArgumentException("inputStream cannot be null.");
		this.inputStream = inputStream;
	}

	public ByteOrder order () {
		return byteOrder;
	}

	public void order (ByteOrder byteOrder) {
		this.byteOrder = byteOrder;
	}

	/** Sets a new buffer. The position and total are reset, discarding any buffered bytes. */
	public void setBuffer (byte[] bytes) {
		ByteBuffer directBuffer = ByteBuffer.allocateDirect(bytes.length);
		directBuffer.put(bytes);
		directBuffer.position(0);
		directBuffer.order(byteOrder);
		setBuffer(directBuffer, 0, bytes.length);
	}

	/** Sets a new buffer. The position and total are reset, discarding any buffered bytes. */
	public void setBuffer (ByteBuffer bytes, int offset, int count) {
		if (bytes == null) throw new IllegalArgumentException("bytes cannot be null.");
		niobuffer = bytes;
		position = offset;
		limit = offset + count;
		capacity = bytes.capacity();
		total = 0;
		inputStream = null;
		niobuffer.position(position);
	}

	/*** Release a direct buffer. {@link #setBuffer(ByteBuffer, int, int)} should be called before next write operations can be
	 * called. */
	public void release () {
		close();
		UnsafeUtil.releaseBuffer(niobuffer);
		niobuffer = null;
	}

	/*** This constructor allows for creation of a direct ByteBuffer of a given size at a given address.
	 * 
	 * <p>
	 * Typical usage could look like this snippet:
	 * 
	 * <pre>
	 * // Explicitly allocate memory
	 * long bufAddress = UnsafeUtil.unsafe().allocateMemory(4096);
	 * // Create a ByteBufferInput using the allocated memory region
	 * ByteBufferInput buffer = new ByteBufferInput(bufAddress, 4096);
	 * 
	 * // Do some operations on this buffer here
	 * 
	 * // Say that ByteBuffer won't be used anymore
	 * buffer.release();
{'fraction_non_alphanumeric': 0.0957885099565118, 'fraction_numerical': 0.004921034561684596, 'mean_word_length': 4.245498199279712, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6715740', 'n_tokens_mistral': 2622, 'n_tokens_neox': 2413, 'n_words': 916}