<commit_before>package gameutils.networking;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * A Non-Blocking TCP wrapper.
 * @author <NAME>
 */
public class NBSocketPacketIO extends PacketIO {
	private SocketChannel channel;
	private ByteBuffer in, out;
	
	public NBSocketPacketIO(String address, int port, boolean isBlocking) throws IOException {
		this(new InetSocketAddress(address,port),isBlocking);
	}
	
	public NBSocketPacketIO(String address, int port, boolean isBlocking, int bufferSize) throws IOException {
		this(new InetSocketAddress(address,port),isBlocking,bufferSize);
	}
	
	public NBSocketPacketIO(SocketAddress address, boolean isBlocking) throws IOException {
		this(address,isBlocking,8192);
	}
	
	public NBSocketPacketIO(SocketAddress address, boolean isBlocking, int bufferSize) throws IOException {
		this(SocketChannel.open(address),bufferSize);
		
		channel.configureBlocking(isBlocking);
	}
	
	public NBSocketPacketIO(SocketChannel channel) {
		this(channel,8192);
	}
	
	public NBSocketPacketIO(SocketChannel channel, int bufferSize) {
		if(!channel.isOpen())
			throw new IllegalStateException("channel is not open.");
		this.channel = channel;
		
		setBufferSize(bufferSize);
	}
	
	public Packet read() throws IOException {
		in.clear();
		
		if(channel.read(in) <= 0)
			return null;
		
		in.flip();
		
		byte array[] = new byte[in.limit()];
		for(int a = 0; a < array.length; a++)
			array[a] = in.get();
		
		ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(array));<commit_msg>Tweaked a few things in NBSocketPacketIO.<commit_after>package gameutils.networking;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * A Non-Blocking TCP wrapper.
 * @author <NAME>
 */
public class NBSocketPacketIO extends PacketIO {
	private SocketChannel channel;
	private ByteBuffer in, out;
	
	public NBSocketPacketIO(String address, int port, boolean isBlocking) throws IOException {
		this(new InetSocketAddress(address,port),isBlocking);
	}
	
	public NBSocketPacketIO(String address, int port, boolean isBlocking, int bufferSize) throws IOException {
		this(new InetSocketAddress(address,port),isBlocking,bufferSize);
	}
	
	public NBSocketPacketIO(SocketAddress address, boolean isBlocking) throws IOException {
		this(address,isBlocking,8192);
	}
	
	public NBSocketPacketIO(SocketAddress address, boolean isBlocking, int bufferSize) throws IOException {
		this(SocketChannel.open(address),bufferSize);
		
		channel.configureBlocking(isBlocking);
	}
	
	public NBSocketPacketIO(SocketChannel channel) {
		this(channel,8192);
	}
	
	public NBSocketPacketIO(SocketChannel channel, int bufferSize) {
		if(!channel.isOpen())
			throw new IllegalStateException("channel is not open.");
		this.channel = channel;
		
		setBufferSize(bufferSize);
	}
	
	public Packet read() throws IOException {
		in.clear();
		
		if(channel.read(in) <= 0)
			return null;
		
		in.flip();
		
		ObjectInputStream oin = new ObjectInputStream(new InputStream() {
			public int read() throws IOException {
				if(!in.hasRemaining())
					return -1;
				
				int b = in.get();
				if(b < 0) {
					b &= 255;
					b |= 128;
				}
				return b;
			}
		});
		
{'fraction_non_alphanumeric': 0.10236220472440945, 'fraction_numerical': 0.007330980179201738, 'mean_word_length': 4.9708265802269045, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '3217705', 'n_tokens_mistral': 1246, 'n_tokens_neox': 1084, 'n_words': 267}