<commit_before>/*
 * Copyright (c) 2016 MCPhoton <http://mcphoton.org> and contributors.
 *
 * This file is part of the Photon Server Implementation <https://github.com/mcphoton/Photon-Server>.
 *
 * The Photon Server Implementation is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * The Photon Server Implementation is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.mcphoton.impl.network;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import org.mcphoton.impl.server.Main;
import org.slf4j.Logger;

/**
 * A PacketReader reads packets from a SocketChannel. In the minecraft protocol, a packet is a block of data
 * (bytes) sent over the network, preceded by its size (encoded as a VarInt). With a SocketChannel's read, the
 * server may get multiple packets in once, or only a part of a packet. The PacketReader solves this problem:
 * it can separate different messages and regroup all the different parts of a message.
 * <p>
 * This class isn't thread-safe. A client's PacketReader should only be used in the NIO network thread.
 * </p>
 *
 * @author TheElectronWill
 */
public final class PacketReader {

	private final SocketChannel channel;
	private ByteBuffer buffer;
	private final int maxBufferSize;
	private int messageLength = -1, writePos = 0, readPos = 0;
	private boolean eos;
	private boolean readVarIntSucceed = false;
	private final Logger logger = Main.serverInstance.logger;

	/**
	 * Creates a new PacketReader with the given parameters.
	 *
	 * @param sc the SocketChannel to read the data from.
	 * @param intialBufferSize the initial size of the internal buffer, which contains the read packet(s)
	 * @param maxBufferSize the maximum size of the internal buffer. This limit the size of the incoming
	 * packets.
	 */
	public PacketReader(SocketChannel sc, int intialBufferSize, int maxBufferSize) {
		this.channel = sc;
		buffer = ByteBuffer.allocateDirect(intialBufferSize);
		this.maxBufferSize = maxBufferSize;
	}

	/**
	 * Tries to read the next packet or to continue reading the current incomplete packet. Returns<commit_msg>Use a dedicated logger for PacketReader<commit_after>/*
 * Copyright (c) 2016 MCPhoton <http://mcphoton.org> and contributors.
 *
 * This file is part of the Photon Server Implementation <https://github.com/mcphoton/Photon-Server>.
 *
 * The Photon Server Implementation is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * The Photon Server Implementation is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.mcphoton.impl.network;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import org.mcphoton.impl.server.Main;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.impl.PhotonLogger;

/**
 * A PacketReader reads packets from a SocketChannel. In the minecraft protocol, a packet is a block of data
 * (bytes) sent over the network, preceded by its size (encoded as a VarInt). With a SocketChannel's read, the
 * server may get multiple packets in once, or only a part of a packet. The PacketReader solves this problem:
 * it can separate different messages and regroup all the different parts of a message.
 * <p>
 * This class isn't thread-safe. A client's PacketReader should only be used in the NIO network thread.
 * </p>
 *
 * @author TheElectronWill
 */
public final class PacketReader {

	private final SocketChannel channel;
	private ByteBuffer buffer;
	private final int maxBufferSize;
	private int messageLength = -1, writePos = 0, readPos = 0;
	private boolean eos;
	private boolean readVarIntSucceed = false;
	private static final Logger logger = LoggerFactory.getLogger("PacketReader");

	/**
	 * Creates a new PacketReader with the given parameters.
	 *
	 * @param sc the SocketChannel to read the data from.
	 * @param intialBufferSize the initial size of the internal buffer, which contains the read packet(s)
	 * @param maxBufferSize the maximum size of the internal buffer. This limit the size of the incoming
	 * packets.
	 */
	public PacketReader(SocketChannel sc, int intialBufferSize, int maxBufferSize) {
		this.channel = sc;
		this.buffer = ByteBuffer.allocateDirect(intialBufferSize);
		this.maxBufferSize = maxBufferSize;
		((PhotonLogger) logger).setLevel(Main.serverInstance.logger.getLevel());
	}

	/**
	 * Tries to read the next packet or to continue reading the current incomplete packet. Returns
{'fraction_non_alphanumeric': 0.07228478023973847, 'fraction_numerical': 0.003632401017072285, 'mean_word_length': 4.234790874524715, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 2, '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': '1459759', 'n_tokens_mistral': 1525, 'n_tokens_neox': 1436, 'n_words': 683}